0

despite having developed applications in C++ for about two to three years, I never had to setup a project on my own. Most of the project were preconfigured thus I never learned to do it myself. Having lecture-free time at the moment, I thought to myself: "I am going to create my first own CMake C++ project".

Because I knew I wanted to store information in a database,I set off to create a simple CMake project with one executable linked to the MySQL Connector for C++, and instantly failed...

Since I - oddly - could not find helpful information anywhere else, I hope one of you could be my savior.

My project is setup as follows:

Root
- CMakeLists.txt
- build
- src
- tests
-- main.cpp
- include
-- mysql_connection.h
-- cppconn
--- driver.h
--- exception.h
--- resultset.h
--- statement.h
--- ...
-- ...
- libs
-- libmysqlcppconn.dylib -> libmysqlcppconn.7.8.0.12.dylib (symlink)
-- libmysqlcppconn.7.8.0.12.dylib
-- libmysqlcppconn8.1.8.0.12.dylib
-- libmysqlcppconn8.1.dylib  -> libmysqlcppconn8.1.8.0.12.dylib (symlink)
-- libmysqlcppconn8.dylib -> libmysqlcppconn8.1.8.0.12.dylib (symlink)
-- libssl.dylib
-- libcrypto.dylib
-- ...

My main.cpp contains:

#include "mysql_connection.h"
#include <cppconn/driver.h>
#include <cppconn/exception.h> 
#include <cppconn/resultset.h>
#include <cppconn/statement.h>

int main(int argc, char const *argv[])
{
    sql::Driver *driver;
    sql::Connection *con;

    driver = get_driver_instance();
    con = driver->connect("tcp://127.0.0.1:3306","root","securepw");
    return 0;
}

And my CMakeLists.txt:

cmake_minimum_required(VERSION 3.0)
project(EconSim)

add_executable(EconSim ${PROJECT_SOURCE_DIR}/tests/main.cpp)
target_include_directories(EconSim PUBLIC ${PROJECT_SOURCE_DIR}/include)
target_link_libraries(EconSim ${PROJECT_SOURCE_DIR}/libs/libmysqlcppconn.dylib)
target_compile_features(EconSim PUBLIC cxx_std_17)

I am able to compile the application but I get the following error when executing it:

dyld: Library not loaded: libmysqlcppconn.7.dylib
  Referenced from: /Users/aosterthun/Documents/Programming/EconSim/build/./EconSim
  Reason: image not found
Abort trap: 6

When instead using libmysqlcppconn8.dylib in the CMakeLists.txt: And my CMakeLists.txt:

cmake_minimum_required(VERSION 3.0)
project(EconSim)

add_executable(EconSim ${PROJECT_SOURCE_DIR}/tests/main.cpp)
target_include_directories(EconSim PUBLIC ${PROJECT_SOURCE_DIR}/include)
target_link_libraries(EconSim ${PROJECT_SOURCE_DIR}/libs/libmysqlcppconn8.dylib)
target_compile_features(EconSim PUBLIC cxx_std_17)

I get a compile error:

[build] Starting build
[proc] Executing command: /usr/local/bin/cmake --build /Users/aosterthun/Documents/Programming/EconSim/build --config Debug --target all -- -j 6
[build] [ 50%] Linking CXX executable EconSim
[build] Undefined symbols for architecture x86_64:
[build]   "_get_driver_instance", referenced from:
[build]       _main in main.cpp.o
[build] ld: symbol(s) not found for architecture x86_64
[build] clang: error: linker command failed with exit code 1 (use -v to see invocation)
[build] make[2]: *** [EconSim] Error 1
[build] make[1]: *** [CMakeFiles/EconSim.dir/all] Error 2
[build] make: *** [all] Error 2
[build] Build finished with exit code 2

I found this: How do I link C++ MySQL Connector Libraries to Cmake? but that sadly did not solve my problem.

I also found CLion: undefined “_get_driver_instance” which resulted in this CMakeLists.txt:

cmake_minimum_required(VERSION 3.0)
project(EconSim)

include_directories(${PROJECT_SOURCE_DIR}/include)
add_library(libmysqlcppconn STATIC IMPORTED)

add_executable(EconSim ${PROJECT_SOURCE_DIR}/tests/main.cpp)
set_property(TARGET libmysqlcppconn PROPERTY IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/libs/libmysqlcppconn-static.a)
target_compile_features(EconSim PUBLIC cxx_std_17)
target_link_libraries(EconSim libmysqlcppconn)

Which then results in this error:

[build] Starting build
[proc] Executing command: /usr/local/bin/cmake --build /Users/aosterthun/Documents/Programming/EconSim/build --config Debug --target all -- -j 6
[build] [ 50%] Linking CXX executable EconSim
[build] Undefined symbols for architecture x86_64:
[build]   "_BIO_free", referenced from:
[build]       sha256_password_auth_client(MYSQL_PLUGIN_VIO*, MYSQL*) in libmysqlcppconn-static.a(client_authentication.cc.o)
[build]       caching_sha2_password_auth_client(MYSQL_PLUGIN_VIO*, MYSQL*) in libmysqlcppconn-static.a(client_authentication.cc.o)
[build]   "_BIO_new_bio_pair", referenced from:
[build]       dummy_function_needed_by_xplugin() in libmysqlcppconn-static.a(my_aes_openssl.cc.o)
[build]   "_BIO_new_mem_buf", referenced from:
[build]       sha256_password_auth_client(MYSQL_PLUGIN_VIO*, MYSQL*) in libmysqlcppconn-static.a(client_authentication.cc.o)
[build]       caching_sha2_password_auth_client(MYSQL_PLUGIN_VIO*, MYSQL*) in libmysqlcppconn-static.a(client_authentication.cc.o)
[build]   "_BN_bin2bn", refer...

I would appreciate any help. Even a hint to an obvious solution I have not found. I am still a bit confused why I did not find helpful information on this topic. Cause there should be lots of people developing with this technology.

Just ask for further information if I missed any.

A. Osterthun
  • 175
  • 1
  • 3
  • 18
  • Depending on your needs, [Sqlite](https://sqlite.org/) might be easier to get off the ground. – Jesper Juhl Aug 21 '18 at 20:21
  • That would be a sufficient solution. But I would still like to solve this problem due to personal interest ;) – A. Osterthun Aug 21 '18 at 20:39
  • Just a typo or are you referencing the wrong dylib? Should it be `libmysqlcppconn8.dylib` (note the `8`)? – vre Aug 21 '18 at 21:03
  • I did try that and forgot to mention it in the post. Thanks for bringing that up. I added the problems I had with that approach in the main post. – A. Osterthun Aug 21 '18 at 21:15

1 Answers1

1

You need install openssl and link libraries libcrypto, libssl, libresolv. I solve this problem as follows:

target_link_libraries(${PROJECT_NAME}
PUBLIC
    mysqlclient
    mysqlcppconn-static
    crypto
    ssl
    resolv
)
Valex
  • 509
  • 4
  • 8