I've been trying to get MySQL Connector C++ 8.0 to work with CMake on windows vscode but I keep encountering this error when initializing get_driver_instance(). I have seen other posts and they mention a linking problem but because they are on Linux, it's not too helpful to me.
The problem occurs as soon I call "get_driver_instance()".
load_database.cpp
load_database::load_database()
{
try
{
this->sql_driver = get_driver_instance();
this->sql_connection = sql_driver->connect(Host, User, Password);
}
catch (const std::exception &e)
{
std::cerr << e.what() << '\n';
exit(1);
}
}
load_database.h
#pragma once
#include "mysql_connection.h"
#include "cppconn/driver.h"
#include "cppconn/exception.h"
#include "cppconn/resultset.h"
#include "cppconn/prepared_statement.h"
class load_database
{
public:
load_database();
~load_database();
private:
sql::Driver *sql_driver;
sql::Connection *sql_connection;
sql::PreparedStatement *sql_statement;
sql::ResultSet *sql_result;
};
CMakeList.txt
cmake_minimum_required(VERSION 3.0.0)
project(wallstreet_trader VERSION 0.1.0)
# Set the path to the MySQL Connector C++ directory
set(FULL_PATH_TO_MYSQL_CONNECTOR_CPP_DIR "C:\\Program Files\\MySQL\\Connector C++ 8.0")
# Add the include directory for the MySQL Connector C++ headers
include_directories(${FULL_PATH_TO_MYSQL_CONNECTOR_CPP_DIR}/include/jdbc)
# Add the library directory for the MySQL Connector C++ libraries
link_directories(${FULL_PATH_TO_MYSQL_CONNECTOR_CPP_DIR}/lib64/vs14)
# Add preprocessor definitions for the MySQL Connector C++ libraries
add_definitions(-DSTATIC_CONCPP)
# Add the executable for the project
add_executable(wallstreet_trader main.cpp timekeeper.cpp load_database.cpp)
# Link the project with the MySQL Connector C++ libraries
target_link_libraries(wallstreet_trader mysqlcppconn-static)
# Enable testing and include CPack for packaging
include(CTest)
enable_testing()
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
Not sure if this matters but I assume vscode uses this to know what compiler to use so ill include the kits.json
cmake-tools-kits.json
[
{
"name": "Mingw64 GCC 9.3.0",
"compilers": {
"C": "C:\\msys64\\mingw64\\bin\\gcc.exe",
"CXX": "C:\\msys64\\mingw64\\bin\\g++.exe"
},
"preferredGenerator": {
"name": "MinGW Makefiles"
},
"environmentVariables": {
"PATH": "C:/msys64/mingw64/bin/"
}
}
]
Simplified version of the error output since I get thousands of "[build] Warning: corrupt .drectve at end of def file" and "undefined reference to"
Error
[build] Starting build
[proc] Executing command: "C:/Program Files/CMake/bin/cmake.exe" --build d:/_Programs/WallStreet_Trader/build --config Debug --target all -j 14 --
[build] [ 25%] Building CXX object CMakeFiles/wallstreet_trader.dir/main.cpp.obj
[build] [ 50%] Building CXX object CMakeFiles/wallstreet_trader.dir/timekeeper.cpp.obj
[build] [ 75%] Building CXX object CMakeFiles/wallstreet_trader.dir/load_database.cpp.obj
[build] [100%] Linking CXX executable wallstreet_trader.exe
[build] Warning: corrupt .drectve at end of def file
[build] Warning: corrupt .drectve at end of def file
[build] Warning: corrupt .drectve at end of def file
[build] Warning: corrupt .drectve at end of def file
[build] Warning: corrupt .drectve at end of def file
undefined reference to
undefined reference to
undefined reference to
[build] collect2.exe: error: ld returned 1 exit status
[build] mingw32-make[2]: *** [CMakeFiles\wallstreet_trader.dir\build.make:133: wallstreet_trader.exe] Error 1
[build] mingw32-make[1]: *** [CMakeFiles\Makefile2:110: CMakeFiles/wallstreet_trader.dir/all] Error 2
[build] mingw32-make: *** [Makefile:120: all] Error 2
[proc] The command: "C:/Program Files/CMake/bin/cmake.exe" --build d:/_Programs/WallStreet_Trader/build --config Debug --target all -j 14 -- exited with code: 2
[build] Build finished with exit code 2
I've gotten this to work on Microsoft VS but I am trying to learn CMake now so it's a bit more different.