I successfully configured MYSQL with VS2008, i executed the following code, and no errors were thrown.

#include <mysql.h>
#include "stdafx.h"

void main()
{

   
}

Now i need to write a C++ code to retrieve some values from a Database, therefore i used the following code;

#include <mysql.h>
#include "stdafx.h"
#include <stdio.h>
#include <windows.h> 


void main()
{
MYSQL *conn;
   
}

As soon as i type MYSQL *conn; and run the application i get the following error message;

error C2065: 'MYSQL' : undeclared identifier

There's just something missing here, and google couldn't give a good solution, Please help.

Dani AI

Generated

As diagnosed, the immediate compile error came from how Visual C++ handles precompiled headers: when a source file is set to use a PCH the compiler ignores everything up to the configured PCH include, so a type declared in a header that never gets parsed will appear “undeclared.” The long-term choices are to keep PCH and make every .cpp include the project PCH first, or disable PCH for that file — both are documented in the Visual Studio precompiled-header guide. (learn.microsoft.com)

After the include-order/PCH issue is solved, the usual next steps for a working MySQL client are configuration of include and linker settings: add the MySQL include folder to the C/C++ Additional Include Directories, add the MySQL lib folder to Linker → Additional Library Directories, and put the appropriate import/static library (the dynamic import is typically libmysql.lib; static client library names vary) into Linker → Additional Dependencies. At run time the corresponding DLL must be reachable (PATH or next to the EXE), and the library’s architecture (x86 vs x64) and CRT settings must match the build. The MySQL client docs describe these options. (dev.mysql.com)

The C API usage pattern is straightforward: initialize the library/handler, call the connect function, check the library error reporting on failure, then close the connection when done. The reference pages for mysql_init() and mysql_real_connect() show the recommended sequence and common error codes to check. If using threads, follow the library’s initialization rules. (dev.mysql.com)

Troubleshooting notes: unresolved linker errors (LNK2019/LNK1181) usually mean the linker isn’t finding the right .lib; a missing-DLL at launch means the DLL isn’t on PATH or next to the EXE. Historically Windows builds also mentioned my_global.h before mysql.h, but recent MySQL header cleanup means that header may not exist in newer Connector/C distributions — inspect the include folder and follow the version-specific docs. ’s remark about MYSQL being the typedef name is correct: it’s defined by the client headers. (dev.mysql.com)

Recommended Answers

All 3 Replies

Are you sure its supposed to be capitalized? The name of the header is 'mysql', so I would try mysql *conn;

Quite likely your project is configured to use precompiled headers through "stdafx.h".
In other words, you'll need to change the order of your includes, in order for the compiler to see <mysql.h>.

You could try ..

#include "stdafx.h" // First stdafx.h

#include <cstdio>
#include <windows.h>
#include <mysql.h>

int main()
{
  MYSQL *conn;
}
commented: Nice going. +5

Whoa ! it worked, Thank you mitrmkar.

daviddoria > Yes, it should be capitalized.

Thank you both of you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.