CMake C++ 将静态系统库包含到项目中 - 如何
CMake C++ include static system library to project - how to
我是 C++ 和 CMake 项目的新手。我(使用 CLion)编写了使用 mysql 连接器的非常简单的应用程序。
这需要在系统 libmysqlcppconn-dev
上安装。这在我构建它的系统上没有问题,但如果我想在其他系统上执行它,它会失败,因为它需要共享库。
我想将该库包含到我的项目中。
为此,在我的项目中,我创建了文件夹 lib/static
并复制了文件。
$ tree lib/static
lib/static
├── libmysqlcppconn.a
└── libmysqlcppconn-static.a
安装开发库后的这些文件位于:
/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
现在在我的 CMalke.lists 我添加了:
#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})
但这不会编译
[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
如果我取消注释 find_library()
函数并注释 add_library() set_property()
我会收到该错误
[ 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'
一切都与共享系统库一起工作,但我想将它添加到可执行文件中,以便在其他系统上不依赖。
我应该如何正确地将静态库包含到我的项目中?
提前感谢您的帮助。
使用系统库构建 ii 后,ldd 显示:
$ 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)
这解决了我的问题
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)
我是 C++ 和 CMake 项目的新手。我(使用 CLion)编写了使用 mysql 连接器的非常简单的应用程序。
这需要在系统 libmysqlcppconn-dev
上安装。这在我构建它的系统上没有问题,但如果我想在其他系统上执行它,它会失败,因为它需要共享库。
我想将该库包含到我的项目中。
为此,在我的项目中,我创建了文件夹 lib/static
并复制了文件。
$ tree lib/static
lib/static
├── libmysqlcppconn.a
└── libmysqlcppconn-static.a
安装开发库后的这些文件位于:
/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
现在在我的 CMalke.lists 我添加了:
#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})
但这不会编译
[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
如果我取消注释 find_library()
函数并注释 add_library() set_property()
我会收到该错误
[ 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'
一切都与共享系统库一起工作,但我想将它添加到可执行文件中,以便在其他系统上不依赖。
我应该如何正确地将静态库包含到我的项目中? 提前感谢您的帮助。
使用系统库构建 ii 后,ldd 显示:
$ 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)
这解决了我的问题
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)