静态链接 ActiveMQ-cpp

Posted

技术标签:

【中文标题】静态链接 ActiveMQ-cpp【英文标题】:Static linking ActiveMQ-cpp 【发布时间】:2018-04-25 10:54:35 【问题描述】:

我正在创建一个 c++ 项目,我希望它在没有任何依赖关系的情况下以独立模式运行,因为它将部署在多个服务器上。

这是我的 CMakeLists 文件:

project(CMS-test)
cmake_minimum_required(VERSION 2.6)

add_executable(main main.cpp)

set(CMAKE_CXX_FLAGS "$CMAKE_CXX_FLAGS -std=c++11")

INCLUDE_DIRECTORIES(/usr/local/apr/include/apr-1)
INCLUDE_DIRECTORIES(/usr/local/include/activemq-cpp-3.9.4)

target_link_libraries(main $CMAKE_SOURCE_DIR/libapr-1.a)
target_link_libraries(main $CMAKE_SOURCE_DIR/libactivemq-cpp.a)

libactivemq-cpp.a 和 libapr-1.a 在我的项目目录中找到

但我在链接过程中make main 之后遇到的错误是

/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/internal/DecafRuntime.cpp:72: undefined reference to `apr_initialize'
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/internal/DecafRuntime.cpp:73: undefined reference to `apr_pool_create_ex'
libactivemq-cpp.a(libactivemq_cpp_la-DecafRuntime.o): In function `decaf::lang::Runtime::shutdownRuntime()':
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/internal/DecafRuntime.cpp:109: undefined reference to `apr_pool_destroy'
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/internal/DecafRuntime.cpp:110: undefined reference to `apr_terminate'
libactivemq-cpp.a(libactivemq_cpp_la-Network.o): In function `decaf::internal::net::Network::initializeNetworking()':
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/internal/net/Network.cpp:116: undefined reference to `apr_signal_block'
libactivemq-cpp.a(libactivemq_cpp_la-SecureRandomImpl.o): In function `decaf::internal::security::SecureRandomImpl::providerNextBytes(unsigned char*, int)':
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/internal/security/unix/SecureRandomImpl.cpp:128: undefined reference to `apr_file_read_full'
libactivemq-cpp.a(libactivemq_cpp_la-SecureRandomImpl.o): In function `decaf::internal::security::SecureRandomImpl::SecureRandomImpl()':
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/internal/security/unix/SecureRandomImpl.cpp:76: undefined reference to `apr_file_open'
libactivemq-cpp.a(libactivemq_cpp_la-Exception.o): In function `decaf::lang::Exception::buildMessage(char const*, __va_list_tag (&) [1])':
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/lang/Exception.cpp:124: undefined reference to `apr_pvsprintf'
libactivemq-cpp.a(libactivemq_cpp_la-System.o): In function `decaf::lang::System::unsetenv(std::string const&)':
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/lang/System.cpp:276: undefined reference to `apr_env_delete'
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/lang/System.cpp:286: undefined reference to `apr_strerror'
libactivemq-cpp.a(libactivemq_cpp_la-System.o): In function `decaf::lang::System::getenv(std::string const&)':
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/lang/System.cpp:297: undefined reference to `apr_env_get'
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/lang/System.cpp:306: undefined reference to `apr_strerror'
libactivemq-cpp.a(libactivemq_cpp_la-System.o): In function `decaf::lang::System::setenv(std::string const&, std::string const&)':
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/lang/System.cpp:326: undefined reference to `apr_env_set'
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/lang/System.cpp:336: undefined reference to `apr_strerror'
libactivemq-cpp.a(libactivemq_cpp_la-InetAddress.o): In function `decaf::net::InetAddress::getLocalHost()':
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/net/InetAddress.cpp:158: undefined reference to `apr_gethostname'
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/net/InetAddress.cpp:168: undefined reference to `apr_sockaddr_info_get'
libactivemq-cpp.a(libactivemq_cpp_la-AprPool.o): In function `decaf::internal::AprPool::destroyPool()':
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/internal/AprPool.cpp:50: undefined reference to `apr_pool_destroy'
libactivemq-cpp.a(libactivemq_cpp_la-AprPool.o): In function `decaf::internal::AprPool::allocatePool() const':
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/internal/AprPool.cpp:42: undefined reference to `apr_pool_create_unmanaged_ex'
libactivemq-cpp.a(libactivemq_cpp_la-AprPool.o): In function `decaf::internal::AprPool::cleanup()':
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/internal/AprPool.cpp:60: undefined reference to `apr_pool_clear'
libactivemq-cpp.a(libactivemq_cpp_la-TcpSocket.o): In function `decaf::internal::net::tcp::TcpSocket::connect(std::string const&, int, int)':
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/internal/net/tcp/TcpSocket.cpp:312: undefined reference to `apr_sockaddr_info_get'
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/internal/net/tcp/TcpSocket.cpp:318: undefined reference to `apr_socket_opt_get'
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/internal/net/tcp/TcpSocket.cpp:319: undefined reference to `apr_socket_timeout_get'
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/internal/net/tcp/TcpSocket.cpp:322: undefined reference to `apr_socket_opt_set'
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/internal/net/tcp/TcpSocket.cpp:328: undefined reference to `apr_socket_timeout_set'
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/internal/net/tcp/TcpSocket.cpp:332: undefined reference to `apr_socket_connect'
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/internal/net/tcp/TcpSocket.cpp:335: undefined reference to `apr_socket_opt_set'
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/internal/net/tcp/TcpSocket.cpp:336: undefined reference to `apr_socket_timeout_set'
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/internal/net/tcp/TcpSocket.cpp:326: undefined reference to `apr_socket_timeout_set'
libactivemq-cpp.a(libactivemq_cpp_la-TcpSocket.o): In function `decaf::internal::net::tcp::TcpSocket::available()':
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/internal/net/tcp/TcpSocket.cpp:417: undefined reference to `apr_os_sock_get'
libactivemq-cpp.a(libactivemq_cpp_la-TcpSocket.o): In function `decaf::internal::net::tcp::TcpSocket::getLocalAddress() const':
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/internal/net/tcp/TcpSocket.cpp:376: undefined reference to `apr_socket_addr_get'
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/internal/net/tcp/TcpSocket.cpp:378: undefined reference to `apr_sockaddr_ip_getbuf'
libactivemq-cpp.a(libactivemq_cpp_la-TcpSocket.o): In function `decaf::internal::net::tcp::TcpSocket::getOption(int) const':
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/internal/net/tcp/TcpSocket.cpp:571: undefined reference to `apr_socket_opt_get'
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/internal/net/tcp/TcpSocket.cpp:541: undefined reference to `apr_socket_timeout_get'
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/internal/net/tcp/TcpSocket.cpp:545: undefined reference to `apr_socket_opt_get'
libactivemq-cpp.a(libactivemq_cpp_la-TcpSocket.o): In function `decaf::internal::net::tcp::TcpSocket::setOption(int, int)':
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/internal/net/tcp/TcpSocket.cpp:621: undefined reference to `apr_socket_opt_set'
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/internal/net/tcp/TcpSocket.cpp:591: undefined reference to `apr_socket_opt_set'
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/internal/net/tcp/TcpSocket.cpp:593: undefined reference to `apr_socket_timeout_set'
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/internal/net/tcp/TcpSocket.cpp:603: undefined reference to `apr_socket_opt_set'
libactivemq-cpp.a(libactivemq_cpp_la-TcpSocket.o): In function `decaf::internal::net::tcp::TcpSocket::read(unsigned char*, int, int, int)':
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/internal/net/tcp/TcpSocket.cpp:675: undefined reference to `apr_socket_recv'
libactivemq-cpp.a(libactivemq_cpp_la-TcpSocket.o): In function `decaf::internal::net::tcp::TcpSocket::write(unsigned char const*, int, int, int)':
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/internal/net/tcp/TcpSocket.cpp:745: undefined reference to `apr_socket_send'
libactivemq-cpp.a(libactivemq_cpp_la-TcpSocket.o): In function `decaf::internal::net::tcp::TcpSocket::create()':
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/internal/net/tcp/TcpSocket.cpp:157: undefined reference to `apr_socket_create'
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/internal/net/tcp/TcpSocket.cpp:161: undefined reference to `apr_os_sock_get'
libactivemq-cpp.a(libactivemq_cpp_la-TcpSocket.o): In function `decaf::internal::net::tcp::TcpSocket::accept(decaf::net::SocketImpl*)':

/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/net/SocketError.cpp:42: undefined reference to `apr_strerror'
libactivemq-cpp.a(libactivemq_cpp_la-Date.o): In function `decaf::util::Date::toString() const':
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/util/Date.cpp:112: undefined reference to `apr_time_exp_lt'
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/util/Date.cpp:117: undefined reference to `apr_strftime'
libactivemq-cpp.a(libactivemq_cpp_la-OpenSSLContextSpi.o): In function `decaf::internal::net::ssl::openssl::OpenSSLContextSpi::providerInit(decaf::security::SecureRandom*)':
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/internal/net/ssl/openssl/OpenSSLContextSpi.cpp:188: undefined reference to `SSL_load_error_strings'
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/internal/net/ssl/openssl/OpenSSLContextSpi.cpp:189: undefined reference to `SSL_library_init'
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/internal/net/ssl/openssl/OpenSSLContextSpi.cpp:190: undefined reference to `OPENSSL_add_all_algorithms_noconf'
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/internal/net/ssl/openssl/OpenSSLContextSpi.cpp:226: undefined reference to `SSL_CTX_use_PrivateKey_file'
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/internal/net/ssl/openssl/OpenSSLContextSpi.cpp:245: undefined reference to `SSL_CTX_load_verify_locations'
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/internal/net/ssl/openssl/OpenSSLContextSpi.cpp:253: undefined reference to `RAND_seed'
libactivemq-cpp.a(libactivemq_cpp_la-OpenSSLContextSpi.o): In function `decaf::internal::net::ssl::openssl::OpenSSLContextSpi::~OpenSSLContextSpi()':
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/internal/net/ssl/openssl/OpenSSLContextSpi.cpp:162: undefined reference to `CRYPTO_set_locking_callback'
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/internal/net/ssl/openssl/OpenSSLContextSpi.cpp:163: undefined reference to `EVP_cleanup'
libactivemq-cpp.a(libactivemq_cpp_la-OpenSSLContextSpi.o): In function `~ContextData':
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/internal/net/ssl/openssl/OpenSSLContextSpi.cpp:110: undefined reference to `SSL_CTX_free'
libactivemq-cpp.a(libactivemq_cpp_la-OpenSSLSocketException.o): In function `decaf::internal::net::ssl::openssl::OpenSSLSocketException::getErrorString() const':
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/internal/net/ssl/openssl/OpenSSLSocketException.cpp:102: undefined reference to `ERR_get_error'
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/internal/net/ssl/openssl/OpenSSLSocketException.cpp:104: undefined reference to `ERR_error_string_n'
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/internal/net/ssl/openssl/OpenSSLSocketException.cpp:102: undefined reference to `ERR_get_error'
libactivemq-cpp.a(libactivemq_cpp_la-OpenSSLParameters.o): In function `decaf::internal::net::ssl::openssl::OpenSSLParameters::OpenSSLParameters(ssl_ctx_st*)':
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/internal/net/ssl/openssl/OpenSSLSocket.cpp:573: undefined reference to `SSL_write'
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/internal/net/ssl/openssl/OpenSSLSocket.cpp:575: undefined reference to `SSL_get_error'
libactivemq-cpp.a(libactivemq_cpp_la-OpenSSLSocket.o): In function `decaf::internal::net::ssl::openssl::OpenSSLSocket::available()':
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/internal/net/ssl/openssl/OpenSSLSocket.cpp:641: undefined reference to `X509_free'
libactivemq-cpp.a(libactivemq_cpp_la-OpenSSLSocket.o): In function `decaf::internal::net::ssl::openssl::OpenSSLSocket::verifyServerCert(std::string const&)':
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/internal/net/ssl/openssl/OpenSSLSocket.cpp:686: undefined reference to `X509_get_subject_name'
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/internal/net/ssl/openssl/OpenSSLSocket.cpp:692: undefined reference to `X509_NAME_get_index_by_NID'
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/internal/net/ssl/openssl/OpenSSLSocket.cpp:367: undefined reference to `SSL_ctrl'
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/internal/net/ssl/openssl/OpenSSLSocket.cpp:370: undefined reference to `SSL_connect'
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/internal/net/ssl/openssl/OpenSSLSocket.cpp:376: undefined reference to `SSL_get_error'
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/internal/net/ssl/openssl/OpenSSLSocket.cpp:406: undefined reference to `SSL_set_verify'
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/internal/net/ssl/openssl/OpenSSLSocket.cpp:408: undefined reference to `SSL_accept'
/home/isammour/Desktop/smart/activemq-cpp-library-3.9.4/src/main/decaf/internal/net/ssl/openssl/OpenSSLSocket.cpp:361: undefined reference to `SSL_set_verify'

还有更多..

我不知道如何修复这些错误

有什么建议吗?

【问题讨论】:

对于pthread 函数,请使用FindThreads 模块。 好的 pthread 错误消失了,其他的呢? 【参考方案1】:

我假设 apr 是 activemq 的依赖项。如果是这种情况,则需要切换apr和activemq的链接顺序。

链接器只会使用每个库一次。而当它处理 libactivemq-cpp.a 时,它看到它依赖于 apr,但 libapr-1.a 已经被处理了。

【讨论】:

感谢它指出我做错了什么的答案,我会发布完整的修复。【参考方案2】:

发现我以错误的顺序执行target_link_libraries,正如Finn 指出的那样,我更改了库的顺序

完整的工作 CMakeLists 文件现在是

project(CMS-test C CXX)
cmake_minimum_required(VERSION 2.6)

add_executable(main main.cpp)

set(CMAKE_CXX_FLAGS "$CMAKE_CXX_FLAGS -std=c++11")

INCLUDE_DIRECTORIES(/usr/local/apr/include/apr-1)
INCLUDE_DIRECTORIES(/usr/local/include/activemq-cpp-3.9.4)
INCLUDE_DIRECTORIES(/usr/lib/x86_64-linux-gnu/)
find_package (Threads)
target_link_libraries(main $CMAKE_THREAD_LIBS_INIT)


target_link_libraries(main $CMAKE_SOURCE_DIR/libactivemq-cpp.a)
target_link_libraries(main $CMAKE_SOURCE_DIR/libapr-1.a)
target_link_libraries(main ssl)
target_link_libraries(main crypto)
target_link_libraries(main dl)

【讨论】:

以上是关于静态链接 ActiveMQ-cpp的主要内容,如果未能解决你的问题,请参考以下文章

Linux动态链接和静态链接简析

opencv静态链接库有啥用

如何静态链接到 libstdc++.喜欢升级的GCC的朋友快来看看

静态链接与动态链接

静态链接与动态链接

静态链接与动态链接