我在项目中链接libmongocxx时遇到问题。
我正在使用CMAKE链接库,我正在运行Ubuntu 17.10。
这是我的CMakeLists.txt:
cmake_minimum_required(VERSION 3.9) project(web_service_cpp) SET(CMAKE_CXX_STANDARD 14) set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin) set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/lib) find_package(Boost REQUIRED) find_package(Hana REQUIRED) find_package(libmongocxx REQUIRED) find_package(libbsoncxx REQUIRED) add_library(libmongocxx INTERFACE IMPORTED) set_property(TARGET libmongocxx PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${libmongocxx_INCLUDE_DIR}) add_library(boost INTERFACE IMPORTED) set_property(TARGET boost PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${Boost_INCLUDE_DIR}) include_directories(${LIBMONGOCXX_INCLUDE_DIR}) include_directories(${LIBBSONCXX_INCLUDE_DIR}) include_directories("/usr/local/include/mongocxx/v_noabi") include_directories("/usr/local/include/bsoncxx/v_noabi") include_directories("/usr/local/include/libmongoc-1.0") include_directories("/usr/local/include/libbson-1.0") include_directories("/usr/local/lib") include_directories("${PROJECT_SOURCE_DIR}") set(SOURCE_FILES src/main.cpp src/cMqttService.cpp src/cMongoDB.cpp src/cRepository.cpp) set(HEADER_FILES include/cRepository.h include/cMqttService.h include/cMongoDB.h include/main.h) add_executable(web_service_cpp ${HEADER_FILES} ${SOURCE_FILES}) target_link_libraries(web_service_cpp ${LIBMONGOCXX_LIBRARIES}) target_link_libraries(web_service_cpp ${LIBBSONXX_LiBRARIES})
我发现我收到此错误很奇怪,因为它用于处理macOS:
[ 20%] Building CXX object CMakeFiles/web_service_cpp.dir/src/main.cpp.o [ 40%] Building CXX object CMakeFiles/web_service_cpp.dir/src/cMqttService.cpp.o [ 60%] Building CXX object CMakeFiles/web_service_cpp.dir/src/cMongoDB.cpp.o [ 80%] Building CXX object CMakeFiles/web_service_cpp.dir/src/cRepository.cpp.o [100%] Linking CXX executable bin/bin/web_service_cpp CMakeFiles/web_service_cpp.dir/src/cMqttService.cpp.o: In function `cMqttService::initCommunication()': cMqttService.cpp:(.text+0x1da): undefined reference to `mqtt::async_client::async_client(std::__cxx11::basic_string, std::allocator > const&, std::__cxx11::basic_string, std::allocator > const&, mqtt::iclient_persistence*)' cMqttService.cpp:(.text+0x225): undefined reference to `mqtt::connect_options::connect_options()' cMqttService.cpp:(.text+0x266): undefined reference to `mqtt::connect_options::connect_options(mqtt::connect_options const&)' cMqttService.cpp:(.text+0x286): undefined reference to `mqtt::async_client::connect(mqtt::connect_options)' cMqttService.cpp:(.text+0x3ca): undefined reference to `mqtt::async_client::publish(std::shared_ptr)' cMqttService.cpp:(.text+0x464): undefined reference to `mqtt::async_client::publish(mqtt::buffer_ref, void const*, unsigned long, int, bool)' cMqttService.cpp:(.text+0x4ea): undefined reference to `mqtt::async_client::~async_client()' cMqttService.cpp:(.text+0x63f): undefined reference to `mqtt::async_client::~async_client()' CMakeFiles/web_service_cpp.dir/src/cMqttService.cpp.o: In function `mqtt::async_client::disconnect()': cMqttService.cpp:(.text._ZN4mqtt12async_client10disconnectEv[_ZN4mqtt12async_client10disconnectEv]+0x36): undefined reference to `mqtt::disconnect_options::disconnect_options()' CMakeFiles/web_service_cpp.dir/src/cMqttService.cpp.o: In function `void __gnu_cxx::new_allocator::construct, mqtt::buffer_ref, int&, bool&>(mqtt::message*, mqtt::buffer_ref&&, mqtt::buffer_ref&&, int&, bool&)': cMqttService.cpp:(.text._ZN9__gnu_cxx13new_allocatorIN4mqtt7messageEE9constructIS2_JNS1_10buffer_refIcEES6_RiRbEEEvPT_DpOT0_[_ZN9__gnu_cxx13new_allocatorIN4mqtt7messageEE9constructIS2_JNS1_10buffer_refIcEES6_RiRbEEEvPT_DpOT0_]+0xdc): undefined reference to `mqtt::message::message(mqtt::buffer_ref, mqtt::buffer_ref, int, bool)' CMakeFiles/web_service_cpp.dir/src/cMongoDB.cpp.o: In function `cMongoDB::cMongoDB(std::__cxx11::basic_string, std::allocator >, short)': cMongoDB.cpp:(.text+0x63): undefined reference to `mongocxx::v_noabi::instance::instance()' cMongoDB.cpp:(.text+0x7c): undefined reference to `mongocxx::v_noabi::uri::k_default_uri[abi:cxx11]' cMongoDB.cpp:(.text+0x96): undefined reference to `mongocxx::v_noabi::uri::uri(bsoncxx::v_noabi::string::view_or_value)' cMongoDB.cpp:(.text+0xb8): undefined reference to `mongocxx::v_noabi::client::client()' cMongoDB.cpp:(.text+0x190): undefined reference to `mongocxx::v_noabi::uri::uri(bsoncxx::v_noabi::string::view_or_value)' cMongoDB.cpp:(.text+0x1ad): undefined reference to `mongocxx::v_noabi::uri::operator=(mongocxx::v_noabi::uri&&)' cMongoDB.cpp:(.text+0x1bc): undefined reference to `mongocxx::v_noabi::uri::~uri()' cMongoDB.cpp:(.text+0x215): undefined reference to `mongocxx::v_noabi::client::client(mongocxx::v_noabi::uri const&, mongocxx::v_noabi::options::client const&)' cMongoDB.cpp:(.text+0x232): undefined reference to `mongocxx::v_noabi::client::operator=(mongocxx::v_noabi::client&&)' cMongoDB.cpp:(.text+0x241): undefined reference to `mongocxx::v_noabi::client::~client()' cMongoDB.cpp:(.text+0x2e3): undefined reference to `mongocxx::v_noabi::client::~client()' cMongoDB.cpp:(.text+0x2f6): undefined reference to `mongocxx::v_noabi::uri::~uri()' cMongoDB.cpp:(.text+0x309): undefined reference to `mongocxx::v_noabi::instance::~instance()' collect2: error: ld returned 1 exit status CMakeFiles/web_service_cpp.dir/build.make:172: recipe for target 'bin/bin/web_service_cpp' failed make[2]: *** [bin/bin/web_service_cpp] Error 1 CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/web_service_cpp.dir/all' failed make[1]: *** [CMakeFiles/web_service_cpp.dir/all] Error 2 Makefile:83: recipe for target 'all' failed make: *** [all] Error 2
我愿意接受建议! 感谢您的帮助。
答案 0 :(得分:2)
我找到了解决问题的方法。它是由我在macOS上使用的所有硬编码字符串引起的。
cmake_minimum_required(VERSION 3.9)
project(web_service_cpp)
SET(CMAKE_CXX_STANDARD 14)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/lib)
find_package(Boost REQUIRED)
find_package(Hana REQUIRED)
find_package(OpenSSL REQUIRED)
find_library(paho-mqttpp3 NAMES libpaho-mqttpp3.so REQUIRED)
find_library(paho-mqtt3a NAMES libpaho-mqtt3a.so REQUIRED)
find_package(libmongocxx REQUIRED)
find_package(libbsoncxx REQUIRED)
add_library(libmongocxx INTERFACE IMPORTED)
add_library(libbsoncxx INTERFACE IMPORTED)
set_property(TARGET libmongocxx PROPERTY
INTERFACE_INCLUDE_DIRECTORIES ${LIBMONGOCXX_INCLUDE_DIRS})
set_property(TARGET libbsoncxx PROPERTY
INTERFACE_INCLUDE_DIRECTORIES ${LIBBSONCXX_INCLUDE_DIRS})
add_library(boost INTERFACE IMPORTED)
set_property(TARGET boost PROPERTY
INTERFACE_INCLUDE_DIRECTORIES ${Boost_INCLUDE_DIR})
add_library(pahottpp STATIC IMPORTED)
set_target_properties(pahottpp PROPERTIES IMPORTED_LOCATION ${paho-mqttpp3})
add_library(pahomqtt3a SHARED IMPORTED)
set_property(TARGET pahomqtt3a PROPERTY IMPORTED_LOCATION ${paho-mqtt3a})
include_directories(${LIBMONGOCXX_INCLUDE_DIRS} ${LIBBSONCXX_INCLUDE_DIRS})
link_directories(${LIBMONGOCXX_INCLUDE_DIRS})
link_directories(${LIBBSONCXX_INCLUDE_DIR})
include_directories("${PROJECT_SOURCE_DIR}")
set(SOURCE_FILES src/main.cpp
src/cMqttService.cpp
src/cMongoDB.cpp
src/cRepository.cpp)
set(HEADER_FILES include/cRepository.h
include/cMqttService.h
include/cMongoDB.h
include/main.h)
add_executable(web_service_cpp
${HEADER_FILES}
${SOURCE_FILES})
target_link_libraries(web_service_cpp ${LIBMONGOCXX_LIBRARIES} ${lIBBSONCXX_LIBRARIES} pahottpp pahomqtt3a)