【发布时间】:2021-09-07 08:38:31
【问题描述】:
我正在开发一个 C++ Web 框架 oatpp 来创建 REST API。使用oatpp-starter 项目,其中CMakeLists.txt 看起来像:
cmake_minimum_required(VERSION 3.1)
set(project_name my-project) ## rename your project here
project(${project_name})
set(CMAKE_CXX_STANDARD 11)
add_library(${project_name}-lib
src/AppComponent.hpp
src/controller/MyController.cpp
src/controller/MyController.hpp
src/dto/DTOs.hpp
)
## link libs
find_package(oatpp 1.2.5 REQUIRED)
target_link_libraries(${project_name}-lib
PUBLIC oatpp::oatpp
PUBLIC oatpp::oatpp-test
)
target_include_directories(${project_name}-lib PUBLIC src)
## add executables
add_executable(${project_name}-exe
src/App.cpp
test/app/MyApiTestClient.hpp)
target_link_libraries(${project_name}-exe ${project_name}-lib)
add_dependencies(${project_name}-exe ${project_name}-lib)
add_executable(${project_name}-test
test/tests.cpp
test/app/TestComponent.hpp
test/app/MyApiTestClient.hpp
test/MyControllerTest.cpp
test/MyControllerTest.hpp
)
target_link_libraries(${project_name}-test ${project_name}-lib)
add_dependencies(${project_name}-test ${project_name}-lib)
set_target_properties(${project_name}-lib ${project_name}-exe ${project_name}-test PROPERTIES
CXX_STANDARD 11
CXX_EXTENSIONS OFF
CXX_STANDARD_REQUIRED ON
)
enable_testing()
add_test(project-tests ${project_name}-test)
编译此代码后我得到的可执行文件在具有相同架构的容器中成功运行。但是当我将paho-mqtt 库添加到项目中时,可执行文件不会在容器上运行,因为the shared library libpaho-mqttcpp3.so.1 not found: No such file or directory 错误。
我正在使用此配置添加库:
更新CMakeLists.txt:
cmake_minimum_required(VERSION 3.1)
set(project_name my-project) ## rename your project here
project(${project_name})
set(CMAKE_CXX_STANDARD 11)
add_library(${project_name}-lib
src/AppComponent.hpp
src/controller/MyController.cpp
src/controller/MyController.hpp
src/dto/DTOs.hpp
)
## link libs
find_package(oatpp 1.2.5 REQUIRED)
target_link_libraries(${project_name}-lib
PUBLIC oatpp::oatpp
PUBLIC oatpp::oatpp-test
)
## conan
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup(TARGETS)
target_include_directories(${project_name}-lib PUBLIC src)
## add executables
add_executable(${project_name}-exe
src/App.cpp
test/app/MyApiTestClient.hpp)
target_link_libraries(${project_name}-exe ${project_name}-lib ${CONAN_LIBS})
add_dependencies(${project_name}-exe ${project_name}-lib)
add_executable(${project_name}-test
test/tests.cpp
test/app/TestComponent.hpp
test/app/MyApiTestClient.hpp
test/MyControllerTest.cpp
test/MyControllerTest.hpp
)
target_link_libraries(${project_name}-test ${project_name}-lib ${CONAN_LIBS})
add_dependencies(${project_name}-test ${project_name}-lib)
set_target_properties(${project_name}-lib ${project_name}-exe ${project_name}-test PROPERTIES
CXX_STANDARD 11
CXX_EXTENSIONS OFF
CXX_STANDARD_REQUIRED ON
)
enable_testing()
add_test(project-tests ${project_name}-test)
这是conanfile.txt
[requires]
paho-mqtt-cpp/1.2.0 # MQTT Client
[generators]
cmake
如何配置 CMake 以使用柯南下载的 PahoMqttCpp 库来构建独立项目可执行文件(使用静态库)
【问题讨论】:
-
请分享您的 conanfile 和整个日志。仅阅读您的症状,似乎已安装了 oatpp,但无法确定它是从系统还是从柯南获取库目录。
-
我正在发布我的 conanfile 并回答我的解决方案,因为我想通了。 TLDR;而不是安装 oatpp,并为 mqtt 客户端使用 conan,而是将 cmake 配置为使用 conan,然后使用 conan 提供的库。