Common Errors and Resolutions for Building your own MySQL or MariaDB C/C++ Program on Windows

In my previous post, Creating a basic C/C++ Program to Interact with MySQL and MariaDB, I ran into some errors along the way, so I wanted to share those and their resolutions.

1. fatal error C1083: Cannot open include file: ‘mysql.h’: No such file or directory

mysql1.c
mysql1.c(2) : fatal error C1083: Cannot open include file: 'mysql.h': 
No such file or directory

At this point, my cl command was:

cl /I "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include" 
mysql1.c

So I needed to include the directory that contained mysql.h as well. So the above changed to:

cl /I "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include" 
/I "C:\Program Files\MySQL\MySQL Server 5.5\include" mysql1.c

2. error C2061: syntax error : identifier ‘SOCKET’

This was actually a small slew of simple syntax errors. I could tell it wasn’t the code, so I must have left something else out. Here was the full output/error:

mysql1.c
c:\..\mysql_com.h(291) : error C2061: syntax error : identifier 'SOCKET'
c:\..\mysql_com.h(337) : error C2059: syntax error : '}'
c:\..\mysql_com.h(451) : error C2143: syntax error : missing ')' before '*'
c:\..\mysql_com.h(451) : error C2143: syntax error : missing '{' before '*'
c:\..\mysql_com.h(451) : error C2371: 'Vio' : redefinition; different basic types
...
C:\..\mysql.h(374) : error C2143: syntax error : missing ')' before '*'
C:\..\mysql.h(374) : error C2143: syntax error : missing '{' before '*'
C:\..\mysql.h(374) : error C2059: syntax error : ')'
C:\..\mysql.h(375) : error C2143: syntax error : missing ')' before '*'
C:\..\mysql.h(375) : error C2143: syntax error : missing '{' before '*'
C:\..\mysql.h(375) : fatal error C1003: error count exceeds 100; stopping compilation

This plethora of errors indicates we’re missing the windows.h header file:

To fix, add the following to the program (which is already included in the program I provided), and then see #3 below:

#include <windows.h>

3. fatal error C1083: Cannot open include file: ‘windows.h’: No such file or directory

mysql1.c
mysql1.c(2) : fatal error C1083: Cannot open include file: 'windows.h': 
No such file or directory

Similar to #1, we need to add another include path, the one that contains windows.h. You can search your system for windows.h with:

dir windows.h /s

For me, this returned:

c:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Include

So to fix it, I added it with another /I, so my command became:

cl /I "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include" 
/I "C:\Program Files\MySQL\MySQL Server 5.5\include" 
/I "C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Include" 
/MD "C:\Program Files\MySQL\MySQL Server 5.5\lib\libmysql.lib" mysql1.c

4. mysql1.obj : error LNK2019: unresolved external symbol _mysql_close@4 referenced in function _main

In fact, there were 4 similar altogether:

mysql1.obj : error LNK2019: unresolved external symbol 
_mysql_close@4 referenced in function _main
mysql1.obj : error LNK2019: unresolved external symbol 
_mysql_real_connect@32 referenced in function _main
mysql1.obj : error LNK2019: unresolved external symbol 
_mysql_init@4 referenced in function _main
mysql1.exe : fatal error LNK1120: 3 unresolved externals

Turns out this indicates some 32-bit and 64-bit mis-matches.

I had forgot that while I’m on a 64-bit machine, and have 64-bit MySQL already installed (I’m using that header file in my include path), the VS Express is 32-bit. So I need to use a 32-bit header (and different include path), which I downloaded and extracted. So my new command became:

cl /I "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include" 
/I "C:\Program Files (x86)\MySQL\mysql-5.5.30-win32\include" 
/I "C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Include" 
/MD "C:\Program Files (x86)\MySQL\mysql-5.5.30-win32\lib\libmysql.lib" 
mysql1.c

5. The program can’t start because libmysql.dll is missing from your computer

mysql1

mysql1.exe - System Error
The program can't start because libmysql.dll is missing from your 
computer. Try reinstalling the program to fix this problem.
[OK]

In this case, I just wanted to get it built, so I simply copied libmysql.dll and libmysql.lib to this working directory. As I mentioned in my previous post, there is probably a better fix than this, but this worked quickly for now anyway.

6. error LNK2019: unresolved external symbol __imp__RegCloseKey@4 referenced in function _win_init_registry

Again, in this case, there were several similar unresolved external errors, so let me post them all:

mysqlclient.lib(my_init.obj) : error LNK2019: unresolved external symbol __imp__RegCloseKey@4 referenced in function _win_init_registry
mysqlclient.lib(my_init.obj) : error LNK2019: unresolved external symbol __imp__RegEnumValueA@32 referenced in function _win_init_registry
mysqlclient.lib(my_init.obj) : error LNK2019: unresolved external symbol __imp__RegOpenKeyExA@20 referenced in function _win_init_registry
mysqlclient.lib(common.obj) : error LNK2019: unresolved external symbol __imp__IsValidSid@4 referenced in function ...
mysqlclient.lib(common.obj) : error LNK2019: unresolved external symbol __imp__EqualSid@8 referenced in function ...
mysqlclient.lib(common.obj) : error LNK2019: unresolved external symbol __imp__LookupAccountNameW@28 referenced in function ...
mysqlclient.lib(common.obj) : error LNK2019: unresolved external symbol __imp__GetTokenInformation@20 referenced in function ...
mysqlclient.lib(random.obj) : error LNK2019: unresolved external symbol __imp__CryptAcquireContextA@20 referenced in function ...
mysqlclient.lib(random.obj) : error LNK2019: unresolved external symbol __imp__CryptReleaseContext@8 referenced in function ...
mysqlclient.lib(random.obj) : error LNK2019: unresolved external symbol __imp__CryptGenRandom@12 referenced in function ...
mysql1.exe : fatal error LNK1120: 10 unresolved externals

Fix: Switched from static (/MT) compilation to dynamic (/MD) compilation seemed to fix this:

So the command was (note “/MT …\mysqlclient.lib” to “/MD …\libmysql.dll”):

cl /I "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include" 
/I "C:\Program Files (x86)\MySQL\mysql-5.5.30-win32\include" 
/I "C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Include" 
/MT "C:\Program Files (x86)\MySQL\mysql-5.5.30-win32\lib\mysqlclient.lib" 
mysql1.c

And changed it to:

cl /I "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include" 
/I "C:\Program Files (x86)\MySQL\mysql-5.5.30-win32\include" 
/I "C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Include" 
/MD "C:\Program Files (x86)\MySQL\mysql-5.5.30-win32\lib\libmysql.lib" 
mysql1.c

7. cl : Command line warning D9024 : unrecognized source file type ‘C:\…\libmysql.dll’, object file assumed

c:\chris\mysql1>
cl /I "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include" 
/I "C:\Program Files (x86)\MySQL\mysql-5.5.30-win32\include" 
/I "C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Include" 
/MD "C:\Program Files (x86)\MySQL\mysql-5.5.30-win32\lib\libmysql.dll" 
mysql1.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 
16.00.30319.01 for 80x86
Copyright (C) Microsoft Corporation.  All rights reserved.

cl : Command line warning D9024 : unrecognized source file type 
'C:\Program Files (x86)\MySQL\mysql-5.5.30-win32\lib\libmysql.dll', 
object file assumed
mysql1.c
Microsoft (R) Incremental Linker Version 10.00.30319.01
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:libmysql.exe
"C:\Program Files (x86)\MySQL\mysql-5.5.30-win32\lib\libmysql.dll"
mysql1.obj
C:\Program Files (x86)\MySQL\mysql-5.5.30-win32\lib\libmysql.dll : 
fatal error LNK1107: invalid or corrupt file: cannot read at 0x368

The problem is trying to use the .dll instead of the .lib, so replace libmysql.dll with libmysql.lib.

That’s all for now.

Hope this helps.

 

Creating a basic C/C++ Program to Interact with MySQL and MariaDB

In this post, I’ll cover how to create a simple C/C++ program that interacts with MySQL using the C API (and Connector/C). I discussed in a previous post how to install the C Connector (both SkySQL and MySQL’s), so please refer to that if you are looking for more specifics on that.

So once you have that set up, then there are just a few more things you need to have in order – at least this was the case in my environment.

1. Create a program (this one simply connects to the server and retrieves the server version):

#include <stdio.h>
#include <windows.h>
#include <mysql.h>
 
MYSQL *conn;
int version = 1;

int main ( int argc, char *argv[] )
{
    conn = mysql_init ( NULL );
    mysql_real_connect ( conn, "localhost", "root",
            "password", "test", 3308, NULL, 0 );
	version = mysql_get_server_version( conn );
	printf("\nMySQL Version = %d\n",version);
    mysql_close ( conn );
    return 0;
}

I saved it as c:\chris\mysql1\mysql1.c.

2. I copied libmysql.dll and libmysql.lib to c:\chris\mysql1
     (There’s probably a better way than this, but this works for now.)

3. cd c:\chris\mysql1\

4. Compile your program (I’m compiling from the command line, using cl.exe):

cl /I "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include" 
/I "C:\Program Files (x86)\MySQL\mysql-5.5.30-win32\include" 
/I "C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Include" 
/MD "C:\Program Files (x86)\MySQL\mysql-5.5.30-win32\lib\libmysql.lib" 
mysql1.c

You need the various includes specified by the /I, and the /MD indicates we’re compiling it dynamically (as opposed to statically – /MT).

5. Run Program, by just typing the name (from the same location) – “mysql1” in this case:

mysql1

Here is the full session output:

c:\chris\mysql1>
cl /I "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include" 
/I "C:\Program Files (x86)\MySQL\mysql-5.5.30-win32\include" 
/I "C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Include" 
/MD "C:\Program Files (x86)\MySQL\mysql-5.5.30-win32\lib\libmysql.lib" 
mysql1.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 
16.00.30319.01 for 80x86
Copyright (C) Microsoft Corporation.  All rights reserved.

mysql1.c
Microsoft (R) Incremental Linker Version 10.00.30319.01
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:mysql1.exe
"C:\Program Files (x86)\MySQL\mysql-5.5.30-win32\lib\libmysql.lib"
mysql1.obj

c:\chris\mysql1>mysql1

MySQL Version = 50532

c:\chris\mysql1>

Fwiw, I did run into a number of errors, but I’ve posted that in a follow-up, so as to keep this post more focused.

Hope this helps.

 

Setting Up Connector/C and SkySQL C Connector for MySQL and MariaDB

I’m writing a post on how to create your first C/C++ program for MySQL (using Windows, and from the command line). A prerequisite for that is to have a C Connector, such as MySQL Connector/C or SkySQL C Connector, so the program can communicate with mysqld.

I didn’t want that post to be too scattered, so I decided to split it into two, more focused posts. That said, this first post will focus on the Connector, and the next post will actually cover the program itself.

Installing and using Connector/C with MySQL is quite simple, so I wanted to show how easy it is. I also wanted to show examples with both SkySQL C Connector with MariaDB (which also works with MySQL) and Connector/C with MySQL, since both are widely used. I also wanted to show some common errors one might encounter and their resolutions, so hopefully this will help anyone who might have issues along the way.

SkySQL C Connector & MariaDB:

1. Download SkySQL C Connector and extract to:

C:\Program Files\MySQL\mariadb_client-1.0.0

2. Test the Connector per the Docs:

cd C:\Program Files\MariaDB 5.5\bin
mysql_client_test -uroot -p -P3314

or:

"C:\Program Files\MariaDB 5.5\bin\mysql_client_test" -uroot -p -P3314

Connector/C & MySQL:

1. Download Connector/C extract to:

C:\Program Files\MySQL\MySQL Connector C 6.1

2. Test the Connector per the Docs:

cd C:\Program Files\MySQL\MySQL Server 5.5\bin
mysql_client_test -uroot -p -P3308

or:

"C:\Program Files\MySQL\MySQL Server 5.5\bin\mysql_client_test" -uroot -p -P3308

Outputs:

SkySQL C Connector & MariaDB:

#####################################
client_connect  
#####################################

 Establishing a connection to '' ...OK
Connected to MySQL server version: 5.5.31-MariaDB (50531)

 Creating a test database 'client_test_db' ...OK

#####################################
1 of (1/1): test_view_sp_list_fields  
#####################################
...
tons of test output
...

Connector/C & MySQL:

#####################################
client_connect  
#####################################

 Establishing a connection to '' ...OK
Connected to MySQL server version: 5.5.32-log (50532)

 Creating a test database 'client_test_db' ...OK

#####################################
1 of (1/1): test_view_sp_list_fields  
#####################################
...
tons of test output
...

Errors & Fixes:

This was very straight-forward overall, and I only ran into 2 quite basic issues, but want to share them here for completeness and in case anyone else runs into the same:

1. When I first invoked mysql_client_test, I received the following error:

mysql_client_test: unknown variable 'default-character-set=utf8'

This just means I had ‘default-character-set=utf8’ set under the [client] section in my config file, and apparently this option cannot be read by mysql_client_test. The simple fix is to add the “loose-” prefix to this option in the config file:

[client]
loose-default-character-set=utf8

Now, you can run the file without encountering this error. Instead, you’ll just see a “warning” now about “unknown variable ‘loose-default-character-set=utf8′”. ๐Ÿ™‚

Note there is *no* need to restart mysqld after making this change, since the change only applies ‘clients’ and not the server (i.e., mysqld).

2. The only other error I could stumble into is:

Establishing a connection to '' ... [MySQL] connection failed

And that was just because I invoked mysql_client_test without –user and –password options.

Hope this helps. ๐Ÿ™‚

 

Resolving Error: master and slave have equal MySQL server UUIDs

If you’ve run into the following error, then there area couple quick fixes:

Errno: 1593
Fatal error: The slave I/O thread stops because master and slave 
have equal MySQL server UUIDs; these UUIDs must be different for 
replication to work.

To give some background first, starting in MySQL 5.6, the server generates a true UUID in addition to the –server-id supplied by the user. This is ultimately to help with new replication features.

From a under-the-hood view, when starting, the MySQL server automatically obtains a UUID as follows (per the manual):

  1. Attempt to read and use the UUID written in the file data_dir/auto.cnf (where data_dir is the server’s data directory); exit on success.
  2. Otherwise, generate a new UUID and save it to this file, creating the file if necessary.

First, this means there is a new file (in 5.6+) located in the datadir named auto.cnf. In this file is a unique uuid for the server.

This is great, but if you are unaware of it, and simply copy the datadir to set up a slave, and do not remove this file, then you’re in for some errors. So just be cautious/aware of this. Some of these errors are easier to track down, such as in this case, a master and slave with the same uuid throws an error right away in the SHOW SLAVE STATUS and the error log. However, if your master is different from the slave, but you have 2 slaves with the same uuid, you’re in for trouble, and the errors are not very descriptive. [More on this soon, once I can test it a bit more, but I saw some strange behaviors in my first tests.]

So, to fix it, simply delete the auto.cnf file, and restart that instance. A new auto.cnf file / and uuid will be generated. For reference, you’d see something like the following in your error log:

2013-05-20 23:02:54 14552 [Warning] No existing UUID has been found, so we
assume that this is the first time that this server has been started.
Generating a new UUID: ee603f24-c1c2-11e2-ad03-089e01009f6a.

The other way is to generate a new UUID (SELECT UUID(); from any MySQL/MariaDB command line) and replace it with the old one in the auto.cnf file.

In either case, you must restart mysqld after the delete or edit for the change to the uuid to take effect.

After that, you’ll be good to go.

For reference, here is the SHOW SLAVE STATUS output:

mysql> show slave status\G
*************************** 1. row ***************************
...
                Last_IO_Errno: 1593
                Last_IO_Error: Fatal error: The slave I/O thread stops because 
                               master and slave have equal MySQL server UUIDs;
                               these UUIDs must be different for replication to work.
...

And here is the error logged to the error log:

[ERROR] Slave I/O: Fatal error: The slave I/O thread stops because master
and slave have equal MySQL server UUIDs; these UUIDs must be different for 
replication to work. Error_code: 1593

 

Resolving Missing sys Include Files: devpoll.h, epoll.h, event.h, and queue.h during CMake when Building MySQL on Windows

As I mentioned in a previous post that I’d mention how I resolved some additional missing include files when building MySQL on Windows.

In this post I cover 4 missing header files, as they are all related and have to do with polling, and headers (and functions) from libevent, and in the folder /sys. I’m not sure if one should even use these functions from libevent on Windows, as these are not needed for Windows, and may not be beneficial either (that last part I’ve just read briefly about, so I’d need to that a bit more myself to be 100%), but including the headers won’t hurt, as it doesn’t hurt if they don’t exist.

At any rate, here were the 4 related missing header file warnings:

-- Looking for include file sys/devpoll.h
-- Looking for include file sys/devpoll.h - not found
-- Looking for include file sys/devpoll.h
-- Looking for include file sys/devpoll.h - not found
-- Looking for include file sys/epoll.h
-- Looking for include file sys/epoll.h - not found
-- Looking for include file sys/event.h
-- Looking for include file sys/event.h - not found
-- Looking for include file sys/queue.h
-- Looking for include file sys/queue.h - not found

[I do see it looks for devpoll.h twice, not sure if that is necessary, so I filed a feature request.]

Anyway, we’re missing devpoll.h, epoll.h, event.h, and queue.h.

There are probably better ways to accomplish this, but this is what worked for me:

To start, I downloaded libevent-2.0.21-stable.tar.gz for queue.h and event.h.

I then downloaded devpoll.h from here.

And then I got a working Windows version of epoll.h from here.

I copied all of the header files to (the location of the Visual Studio I’m using):

C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\sys

Now, when building, the above looks like:

c:\mysql\mysql-5.6.12\bld>cmake ..
-- Building for: Visual Studio 10
-- Running cmake version 2.8.11.1
...
-- Looking for include file sys/devpoll.h
-- Looking for include file sys/devpoll.h - found
-- Looking for include file sys/devpoll.h
-- Looking for include file sys/devpoll.h - found
-- Looking for include file sys/epoll.h
-- Looking for include file sys/epoll.h - found
-- Looking for include file sys/event.h
-- Looking for include file sys/event.h - found
-- Looking for include file sys/queue.h
-- Looking for include file sys/queue.h - found

Again, these may not even be necessary, and I want to look more into libevent for Windows, but at least this helps explain these missing header files (and how to eliminate the “not found” messages if you so desire).

Hope this helps. ๐Ÿ™‚

 

Related Build Links:

 

Another Idea to Help Foster a Vibrant Community: Please make all bugs public

Since MySQL recently implemented the following feature request (and discussed here and here) to help foster communication among public bugs and feature requests, and to provide a way to allow the Community to voice whether or not a bug affects them, I decided to file another feature request that I think would even further encourage communication amongst an already vibrant community, and that is to simply:

“Please make all bugs public”

http://bugs.mysql.com/bug.php?id=69642

And thanks to the new bugs feature, if you would like to see this implemented as well, please click on the bug link, and then on the “Affects Me” button. Please let your voice be heard. And who knows, perhaps if enough of us would like to see this, then maybe it will happen. ๐Ÿ™‚

[Edit]: I just realized Erkan already filed the same feature request about a week ago. So please give his bug the “Affects Me” click, to let your voice fully be heard. ๐Ÿ™‚

http://bugs.mysql.com/bug.php?id=69558