MySQL C++ Connector linking issues in CLion IDE

I’m having trouble getting the MySQL C++ connector to work properly in my CLion project. Every time I try to build my application, I get undefined reference errors during the linking phase.

Here’s the build error I’m seeing:

[3/3] Linking CXX executable database_app.exe
FAILED: database_app.exe 
CMakeFiles/database_app.dir/database.cpp.obj: In function `verify_connection':
C:/Program Files/MySQL/MySQL Connector C++ 8.0/include/jdbc/cppconn/driver.h:82: undefined reference to `verify(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
C:/Program Files/MySQL/MySQL Connector C++ 8.0/include/jdbc/mysql_driver.h:116: undefined reference to `sql::mysql::_get_driver_instance_by_name(char const*)'
collect2.exe: error: ld returned 1 exit status

My CMake configuration looks like this:

cmake_minimum_required(VERSION 3.20)
project(database_app)

set(CMAKE_CXX_STANDARD 17)

set(MYSQL_CPP_CONNECTOR_INCLUDE "C:\\Program Files\\MySQL\\MySQL Connector C++ 8.0\\include\\jdbc")
set(MYSQL_CPP_CONNECTOR_LIBS "C:\\Program Files\\MySQL\\MySQL Connector C++ 8.0\\lib64\\vs14")

include_directories(${MYSQL_CPP_CONNECTOR_INCLUDE})
link_directories(${MYSQL_CPP_CONNECTOR_LIBS})

add_executable(database_app database.cpp)
target_link_libraries(database_app mysqlcppconn)

And here’s my simple test code:

#include <iostream>
#include <mysql_driver.h>
#include <mysql_connection.h>
#include "cppconn/statement.h"

int main() {
    sql::mysql::MySQL_Driver *db_driver;
    sql::Connection *db_conn;
    db_driver = sql::mysql::get_mysql_driver_instance();
    return 0;
}

What am I missing in my setup to make this work correctly?

Windows path handling breaks CMake linking even when everything looks right. Those spaces in “Program Files” mess up CMake’s path resolution. Wrap your paths in quotes or use the 8.3 format like PROGRA~1 instead of “Program Files”. Also caught me off guard - MySQL Connector needs specific runtime libraries. Add ws2_32 and crypt32 to your target_link_libraries since MySQL connector uses Windows socket and crypto APIs. That std::__cxx11::basic_string error screams ABI compatibility issues between your compiler and the prebuilt MySQL libraries. If you’re running MinGW with CLion, the Visual Studio-built MySQL connector won’t work - different C++ runtimes.

I ran into the same linking issues when I started using MySQL connector with CLion. You’re probably not linking the right library variant or missing dependencies. Try this in your target_link_libraries: target_link_libraries(database_app mysqlcppconn-static) instead of just mysqlcppconn. Check your lib directory for both debug and release versions - the linker can be picky about naming conventions. What fixed it for me was adding the MySQL Connector C libraries too, since the C++ connector needs them. Make sure libmysql.dll or libmysql.lib is in your system path or linked directly. Those undefined reference errors mean the symbols exist but can’t be found during linking - it’s a library name or dependency problem, not your include paths.

Check if you’re mixing 32/64 bit libraries - you’re using the lib64 folder but CLion might be compiling for 32-bit. The VS14 version might not match your compiler either. I had the same issue and switching to mysqlcppconn8-static fixed it. Sometimes you need to link both mysqlcppconn AND mysqlclient together.

Your CMake setup has some common issues causing these linking problems. Don’t use link_directories - it’s outdated and unreliable. CMake can’t guarantee the linker will find the right libraries this way. Use find_library to locate exact library files or specify full paths in target_link_libraries instead. You’re only including the jdbc subdirectory, but your code includes mysql_driver.h which is probably in the parent directory. Set your include path to the main folder: C:\Program Files\MySQL\MySQL Connector C++ 8.0\include. I’ve seen this exact error when the compiler finds headers but the linker can’t locate the object code. Those undefined references mean the library isn’t being found during linking.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.