I am using CLion to make a C++ program that uses a MySQL database to handle data. I have downloaded the MySQL Connector and Boost and linked it to my cmake file. However, when I tried to build my project, an error appeared saying "undefined reference to `_imp__get_driver_instance'". Someone told me to link the MySQL Connector libraries, but since I am quite new to C++ and to using this connector, I have no idea on how to do this.
At the moment, this is my Cmake file:
cmake_minimum_required(VERSION 3.8)
project(Learn_Cpp)
set(CMAKE_CXX_STANDARD 17)
include_directories("C:/mysql-connector-c++-noinstall-1.1.9-win32/include" "C:/mysql-connector-c++-noinstall-1.1.9-win32/include/cppconn" "C:/boost_1_66_0")
set(SOURCE_FILES "C++ Tutorials/ClassFile.cpp" "C++ Tutorials/ClassFile.h" "C++ Tutorials/Learn.cpp")
add_executable(Learn_Cpp ${SOURCE_FILES})
Does anyone know how do I resolve this issue?
The MySql docs describe this extensively for Visual Studio and Netbeans. You need to do the respective thing for CMake.
The quick and dirty way is to just hardcode the path to the library in your CMakeLists.txt
:
target_link_libraries(Learn_Cpp c:/path/to/mysql/lib/mysqlcppconn.lib)
Note that this will link against the dll version of the connector, so you need to place the dll in a directory where the exe can find it if you want to run your program after building. Other than that, this should work, but will really only work on your machine for a specific build configuration. To make it portable and more robust, you could use find_library
to locate the correct library file for your configuration on disk. Similarly, you can use find_path
to locate the include directories instead of hardcoding them and use find_package
to locate Boost.
In modern CMake, it is also considered good style to wrap the results from your find_path
and find_library
calls for MySql in an imported target, similar to how FindBoost
does it for Boost.