使用MySQL Connector/C时链接器报错求助(CMake配置)
Fixing MySQL Connector/C Linker Errors for C Beginners
Hey there, as a C beginner, linker errors with MySQL Connector/C can feel pretty frustrating—but let’s break this down step by step to get your program working.
First: Check Architecture Match (32-bit vs 64-bit)
One of the most common causes of linker errors is a mismatch between your compiler’s architecture and the Connector/C version:
- If you’re using a 64-bit compiler (like VS’s x64 build mode, or 64-bit GCC), you need the 64-bit version of MySQL Connector/C. For 32-bit compilers, grab the 32-bit Connector.
- Double-check your
C:/mysql-connector-cfolder: 64-bit builds usually have alib64directory, while 32-bit useslib. Make sure you’re pointing to the right one.
Second: Fix Your CMakeLists.txt Configuration
Your current CMake setup has a few tweaks needed to help the linker find the MySQL library properly:
- First declare the library directory explicitly, then link using the library name (CMake will handle the rest):
cmake_minimum_required(VERSION 3.10) set(MYSQL_CONNECTOR "C:/mysql-connector-c") # Add MySQL header files include_directories(${MYSQL_CONNECTOR}/include) # Add MySQL library directory (use lib64 for 64-bit) link_directories(${MYSQL_CONNECTOR}/lib) set(SOURCE_FILES main.c) add_executable(untitled ${SOURCE_FILES}) # Link the MySQL library (use "mysqld" for Debug builds) target_link_libraries(untitled mysql)
- If you’re using Visual Studio, you can add a condition to use the debug library for Debug builds:
if(CMAKE_BUILD_TYPE STREQUAL "Debug") target_link_libraries(untitled mysqld) # Debug version of the library else() target_link_libraries(untitled mysql) # Release version endif()
Third: Complete Your Test Code
While your current code doesn’t directly cause the linker error, it’s missing critical MySQL initialization code that you’ll need to actually connect to the database. Here’s a fixed version:
#include <stdio.h> #include <mysql.h> int main(void) { MYSQL *conn; MYSQL_RES *res; MYSQL_ROW row; char *server = "localhost"; char *user = "root"; char *password = ""; char *database = "core_loc"; // Initialize MySQL connection handler conn = mysql_init(NULL); if (conn == NULL) { fprintf(stderr, "mysql_init failed: %s\n", mysql_error(conn)); return 1; } // Connect to the database if (!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0)) { fprintf(stderr, "Connection failed: %s\n", mysql_error(conn)); mysql_close(conn); return 1; } printf("Successfully connected to MySQL!\n"); // Add your query code here (example): // if (mysql_query(conn, "SELECT * FROM your_table")) { // fprintf(stderr, "Query failed: %s\n", mysql_error(conn)); // } // Clean up resources mysql_close(conn); return 0; }
Fourth: Verify System Dependencies
- On Windows: Make sure the
binfolder of your Connector/C (likeC:/mysql-connector-c/bin) is added to your system’sPATHenvironment variable. This lets your program find thelibmysql.dlldynamic library when it runs. - On Linux/macOS: Ensure the Connector’s library directory is in your
LD_LIBRARY_PATH(Linux) orDYLD_LIBRARY_PATH(macOS) so the system can locate the shared library.
Extra Troubleshooting Tips
- Double-check that your Connector/C installation is complete—make sure
libmysql.lib(andlibmysql.dllon Windows) exist in the correct folders. - If you’re using a newer Connector/C version (8.0+), confirm the library name hasn’t changed (most versions still use
mysql/mysqld). - Try deleting your CMake build directory and re-running CMake from scratch—sometimes old cached settings cause issues.
内容的提问来源于stack exchange,提问作者Developer
相关产品推荐
相关产品推荐

