0

I'm new in C++ and CMake projects. I write (with CLion) very simple application that uses mysql connector. This require to install on system libmysqlcppconn-dev. That is no problem on system where I build this but if I want to execute it on other system it fails because it needs shared library. I want to include that library into my project. For this in my project I created folder lib/static and I copied files.

$ tree lib/static
lib/static
├── libmysqlcppconn.a
└── libmysqlcppconn-static.a

these files after instaling dev library are located:

/usr/lib/x86_64-linux-gnu/libmysqlcppconn-static.a
/usr/lib/x86_64-linux-gnu/libmysqlcppconn.a
/usr/lib/x86_64-linux-gnu/libmysqlcppconn.so

now in my CMalke.lists I added:

#find_library(MYSQLCPP mysqlcppconn-static lib/static/)

add_library(MYSQLCPP STATIC IMPORTED)
set_property(TARGET MYSQLCPP PROPERTY IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/lib/static/libmysqlcppconn-static.a)

link_directories(lib/static)

target_link_libraries(${project_name} oatpp pthread curl ${MYSQLCPP})

but this won't compile

[100%] Linking CXX executable ../dist/bin/smsapiclient
/usr/bin/ld: CMakeFiles/smsapiclient.dir/src/App.cpp.o: in function `MyController::rootPOST(oatpp::data::mapping::type::String const&)':
/home/marcin/projects/smsapiclient/src/controller/MyController.hpp:61: undefined reference to `get_driver_instance'
collect2: error: ld returned 1 exit status

If I uncomment find_library() function and comment add_library() set_property() I'm getting that error

[ 98%] Linking CXX executable ../dist/bin/smsapiclient
/usr/bin/ld: /home/marcin/projects/smsapiclient/lib/static/libmysqlcppconn-static.a(mysql_client_api.cpp.o): in function `sql::mysql::NativeAPI::LibmysqlStaticProxy::options(MYSQL*, mysql_option, void const*)':
(.text+0x4c4): undefined reference to `mysql_options'

everything works with shared system library but I want to add it into executable to be non depediency on other systems.

How should I include static library into my project correctly? Thanks for you help in advance.

after build ii with system's libraries, ldd shows:

$ ldd smsapiclient 
    linux-vdso.so.1 (0x00007ffcd4d8a000)
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f85b2200000)
    libcurl-gnutls.so.4 => /lib/x86_64-linux-gnu/libcurl-gnutls.so.4 (0x00007f85b2171000)
    libmysqlcppconn.so.7 => /lib/x86_64-linux-gnu/libmysqlcppconn.so.7 (0x00007f85b20b7000)
    libstdc++.so.6 => /lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f85b1ed6000)
    libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f85b1ebb000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f85b1cc9000)
    /lib64/ld-linux-x86-64.so.2 (0x00007f85b2382000)
    libnghttp2.so.14 => /lib/x86_64-linux-gnu/libnghttp2.so.14 (0x00007f85b1c9e000)
    libidn2.so.0 => /lib/x86_64-linux-gnu/libidn2.so.0 (0x00007f85b1c7c000)
    librtmp.so.1 => /lib/x86_64-linux-gnu/librtmp.so.1 (0x00007f85b1c5c000)
    libssh.so.4 => /lib/x86_64-linux-gnu/libssh.so.4 (0x00007f85b1bee000)
    libpsl.so.5 => /lib/x86_64-linux-gnu/libpsl.so.5 (0x00007f85b1bdb000)
    libnettle.so.7 => /lib/x86_64-linux-gnu/libnettle.so.7 (0x00007f85b1ba1000)
    libgnutls.so.30 => /lib/x86_64-linux-gnu/libgnutls.so.30 (0x00007f85b19c9000)
    libgssapi_krb5.so.2 => /lib/x86_64-linux-gnu/libgssapi_krb5.so.2 (0x00007f85b197c000)
    libldap_r-2.4.so.2 => /lib/x86_64-linux-gnu/libldap_r-2.4.so.2 (0x00007f85b1926000)
    liblber-2.4.so.2 => /lib/x86_64-linux-gnu/liblber-2.4.so.2 (0x00007f85b1915000)
    libbrotlidec.so.1 => /lib/x86_64-linux-gnu/libbrotlidec.so.1 (0x00007f85b1906000)
    libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007f85b18ea000)
    libmysqlclient.so.21 => /lib/x86_64-linux-gnu/libmysqlclient.so.21 (0x00007f85b11f4000)
    libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f85b10a5000)
    libunistring.so.2 => /lib/x86_64-linux-gnu/libunistring.so.2 (0x00007f85b0f23000)
    libhogweed.so.5 => /lib/x86_64-linux-gnu/libhogweed.so.5 (0x00007f85b0eeb000)
    libgmp.so.10 => /lib/x86_64-linux-gnu/libgmp.so.10 (0x00007f85b0e67000)
    libcrypto.so.1.1 => /lib/x86_64-linux-gnu/libcrypto.so.1.1 (0x00007f85b0b8f000)
    libp11-kit.so.0 => /lib/x86_64-linux-gnu/libp11-kit.so.0 (0x00007f85b0a59000)
    libtasn1.so.6 => /lib/x86_64-linux-gnu/libtasn1.so.6 (0x00007f85b0a43000)
    libkrb5.so.3 => /lib/x86_64-linux-gnu/libkrb5.so.3 (0x00007f85b0966000)
    libk5crypto.so.3 => /lib/x86_64-linux-gnu/libk5crypto.so.3 (0x00007f85b0935000)
    libcom_err.so.2 => /lib/x86_64-linux-gnu/libcom_err.so.2 (0x00007f85b092e000)
    libkrb5support.so.0 => /lib/x86_64-linux-gnu/libkrb5support.so.0 (0x00007f85b091d000)
    libresolv.so.2 => /lib/x86_64-linux-gnu/libresolv.so.2 (0x00007f85b0901000)
    libsasl2.so.2 => /lib/x86_64-linux-gnu/libsasl2.so.2 (0x00007f85b08e4000)
    libgssapi.so.3 => /lib/x86_64-linux-gnu/libgssapi.so.3 (0x00007f85b089f000)
    libbrotlicommon.so.1 => /lib/x86_64-linux-gnu/libbrotlicommon.so.1 (0x00007f85b087c000)
    libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f85b0874000)
    libssl.so.1.1 => /lib/x86_64-linux-gnu/libssl.so.1.1 (0x00007f85b07e1000)
    libffi.so.7 => /lib/x86_64-linux-gnu/libffi.so.7 (0x00007f85b07d5000)
    libkeyutils.so.1 => /lib/x86_64-linux-gnu/libkeyutils.so.1 (0x00007f85b07ce000)
    libheimntlm.so.0 => /lib/x86_64-linux-gnu/libheimntlm.so.0 (0x00007f85b07c2000)
    libkrb5.so.26 => /lib/x86_64-linux-gnu/libkrb5.so.26 (0x00007f85b072f000)
    libasn1.so.8 => /lib/x86_64-linux-gnu/libasn1.so.8 (0x00007f85b0686000)
    libhcrypto.so.4 => /lib/x86_64-linux-gnu/libhcrypto.so.4 (0x00007f85b064e000)
    libroken.so.18 => /lib/x86_64-linux-gnu/libroken.so.18 (0x00007f85b0635000)
    libwind.so.0 => /lib/x86_64-linux-gnu/libwind.so.0 (0x00007f85b060b000)
    libheimbase.so.1 => /lib/x86_64-linux-gnu/libheimbase.so.1 (0x00007f85b05f9000)
    libhx509.so.5 => /lib/x86_64-linux-gnu/libhx509.so.5 (0x00007f85b05a9000)
    libsqlite3.so.0 => /lib/x86_64-linux-gnu/libsqlite3.so.0 (0x00007f85b0480000)
    libcrypt.so.1 => /lib/x86_64-linux-gnu/libcrypt.so.1 (0x00007f85b0445000)
nicram
  • 353
  • 3
  • 7
  • 24
  • 1
    When specify a **shared** library for the linker, the linker **automatically** links with all libraries that the shared one **depends** from. This is not the case when use a **static** library, so you need to specify other libraries **manually**. Just look into function name listed in "undefined reference" error, and link to the library which provides this symbol. Of course, for being able to link you need to ship this library with your project too. E.g. `mysql_options` function is provided by MySQL library. – Tsyvarev Sep 05 '20 at 08:51
  • @Tsyvarev Or just link statically with that library (probably mysql_client in this case, don't know which CMake package provides it). – n. m. could be an AI Sep 05 '20 at 09:10
  • `libmysqlclient-dev` wasn't installed in system. I installed it now, linked as above and the same result - won't compile. How to check, which libraries I should include? and in which way include it, with find_library() or mannualy name and include it? – nicram Sep 05 '20 at 09:39
  • this `undefined reference to 'get_driver_instance'` comes from `/usr/include/cppconn/driver.h` header file – nicram Sep 05 '20 at 09:48
  • I have found that new cmake has `static` option with libraries `target_link_libraries(your_target_name -static)` This would be something as I want to build all dependieces into executable. So I return to systems libraries when everythig was compiling. I added static option `target_link_libraries(${project_name} -static oatpp pthread curl mysqlcppconn) and errors when copiling `undefined reference` – nicram Sep 05 '20 at 10:03
  • according to [this](https://stackoverflow.com/questions/45325562/mysql-connector-cpp-in-centos-6-undefined-reference-to) I Idded libmysqlclient. `find_library(MYSQLCLIENT libmysqlclient.a) find_library(MYSQLCPP libmysqlcppconn-static.a) ` and in target `target_link_libraries(${project_name} ${MYSQLCLIENT} ${MYSQLCPP} pthread curl oatpp) ` but problem still extists :( – nicram Sep 05 '20 at 11:44

1 Answers1

1

This solved my problem

target_link_libraries(${project_name} pthread oatpp curl mysqlcppconn-static.a mysqlclient.a ssh.a ssl.a crypto.a dl m.a z.a curl-gnutls.a resolv.a stdc++.a)
nicram
  • 353
  • 3
  • 7
  • 24