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.

 

Tags: , , , , , , , , , , , , , , , , , , , , , ,

2 Responses to “Common Errors and Resolutions for Building your own MySQL or MariaDB C/C++ Program on Windows”

  1. Igor Solodovnikov says:

    Why do you think “_mysql_close@4 references 64-bit” ?
    Number after @ is just size of the stack allocated for function’s parameters. In this case stack size is 4 that is single 32-bit parameter.

  2. chris says:

    Hi Igor, thank you for the clarification. (Thought I’d read that somewhere.) I’ve removed that line from the post, so as to not cause any confusion. Thanks again. :)


Period Panties by Period Panteez Menstrual Underwear Menstruation PMS Panty