mercredi 25 novembre 2020

Issue Integrating MySQL Connector C++ with another Library: Segmentation Fault on get_driver_instance()

I use MySQL-Connector-C++ along with a third party library whose executable causes a segmentation fault on sql::Driver* driver = get_driver_instance();.

My main.cpp has two parts.

  • It calls into my_MySQL_utilities class that can read and write to a local MySQL database.
  • main.cpp also calls into a third party library through my_MySQL_utilities that connects to a server from which some external data is retrieved and is printed to stdout.

To track down the issue, I tried to de-couple the two parts and see if I can run them separately.

I can compile, link, and execute the MySQL part in my main.cpp (commenting out the third party part of main.cpp and excluding its source/header files). Reading and writing to the MySQL database is done successfully. I use the following CMakeLists.txt to create an executable to read from a local MySQL database:

# For MySQL-Connector-C++ Only, which compiles, links, and runs with no problem
cmake_minimum_required(VERSION 3.17)
project(myproject)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread")
add_library(libmysqlcppconn STATIC IMPORTED)
set_property(TARGET libmysqlcppconn PROPERTY IMPORTED_LOCATION /usr/lib/x86_64-linux-gnu/libmysqlcppconn.so)
set(SOURCE_FILES main.cpp my_MySQL_utilities.cpp my_MySQL_utilities.h)
add_executable(myproject ${SOURCE_FILES})
target_link_libraries (myproject libmysqlcppconn)

Similarly, I can compile, link, and execute the third party library in my main.cpp (commenting out the MySQL part of main.cpp and excluding its source/header files). Using this third party library, I can successfully read data from a server and print to stdout:

# For Third Party Library Only, which compiles, links, and runs with no problem
cmake_minimum_required(VERSION 3.17)
project(myproject)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread -O3 -DLINUX -D_REENTRANT -Wall -Wno-sign-compare -Wno-write-strings -Wpointer-arith -Winline -Wno-deprecated -fno-strict-aliasing")
INCLUDE_DIRECTORIES(/home/thirdparty/include/)
link_directories(/home/thirdparty/lib/)
link_directories(/usr/lib/x86_64-linux-gnu/)
add_library(libthirdparty1 STATIC IMPORTED)
add_library(libthirdparty2 STATIC IMPORTED)
add_library(libthirdparty3 STATIC IMPORTED)
add_library(libthirdparty4 STATIC IMPORTED)
add_library(libssl STATIC IMPORTED)
add_library(libz STATIC IMPORTED)
add_library(libpthread STATIC IMPORTED)
add_library(librt STATIC IMPORTED)
add_library(libdl STATIC IMPORTED)
set(SOURCE_FILES main.cpp my_thirdpartylib_utilities.cpp my_thirdpartylib_utilities.h)
add_executable(myproject ${SOURCE_FILES})
target_link_libraries (myproject thirdparty1 thirdparty2 thirdparty3 thirdparty4 ssl z pthread rt dl)

However, when I have MySQL-Connector-C++ and the third party library both uncommented in main.cpp, MySQL-Connector-C++ will terminate with a segmentation fault on instantiating the driver:

sql::Driver* driver = get_driver_instance();

...

Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)

Could someone kindly help me out?

Aucun commentaire:

Enregistrer un commentaire