Link PahoMqttCpp 作为 CMake 中使用 Conan 的静态库
Link PahoMqttCpp as a static library in CMake using Conan
我正在开发 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)
我在具有相同体系结构的容器中成功编译此代码后获得的可执行文件 运行s。但是当我将 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 以使用 Conan 下载的 PahoMqttCpp 库构建独立项目可执行文件(使用静态库)
事情是这样的:
我错误地配置了 CMakeLists.txt
以与 conan
集成。由于我事先安装了 paho-mqtt-cpp
,该程序链接到已安装的库,而不是柯南提供的库。
这个 CMakeLists.txt
适合我:
cmake_minimum_required(VERSION 3.1)
set(PROJECT_NAME my-awesome-project)
project(${PROJECT_NAME})
set(CMAKE_CXX_STANDARD 14)
############################### CONAN SETUP BEGIN ##########################################
if(NOT EXISTS "${CMAKE_BINARY_DIR}/conan.cmake")
message(STATUS "Downloading conan.cmake from https://github.com/conan-io/cmake-conan")
file(DOWNLOAD "https://raw.githubusercontent.com/conan-io/cmake-conan/v0.9/conan.cmake"
"${CMAKE_BINARY_DIR}/conan.cmake")
endif()
include(${CMAKE_BINARY_DIR}/conan.cmake)
conan_cmake_run(CONANFILE conanfile.txt BASIC_SETUP)
############################### CONAN SETUP END ############################################
add_library(${PROJECT_NAME}-lib
# controllers
src/controller/StaticController.hpp
# db
src/db/DemoDb.hpp
# mqtt
src/mqtt/MqttCoroutine.hpp
src/mqtt/MqttClient.hpp
# dto
src/dto/PageDto.hpp
src/dto/StatusDto.hpp
src/dto/DemoDto.hpp
# services
src/service/DemoService.cpp
src/service/DemoService.hpp
# base
src/AppComponent.hpp
src/DatabaseComponent.hpp
src/ErrorHandler.cpp
src/ErrorHandler.hpp)
## include directories
target_include_directories(${PROJECT_NAME}-lib PUBLIC src)
add_definitions(
## SQLite database file
-DDATABASE_FILE="${CMAKE_CURRENT_SOURCE_DIR}/sql/db.sqlite"
## SQLite database test file
-DTESTDATABASE_FILE="${CMAKE_CURRENT_SOURCE_DIR}/sql/test-db.sqlite"
## Path to database migration scripts
-DDATABASE_MIGRATIONS="${CMAKE_CURRENT_SOURCE_DIR}/sql"
## my-awesome-project Service
-DHOST="0.0.0.0"
-DPORT=8080
## MQTT Broker
-DMQTT_BROKER_URL="tcp://127.0.0.1:1883"
)
if(CMAKE_SYSTEM_NAME MATCHES Linux)
find_package(Threads REQUIRED)
target_link_libraries(${PROJECT_NAME}-lib INTERFACE Threads::Threads ${CMAKE_DL_LIBS})
endif()
## add executables
add_executable(${PROJECT_NAME}-exe src/App.cpp)
target_link_libraries(${PROJECT_NAME}-exe ${PROJECT_NAME}-lib ${CONAN_LIBS})
add_executable(${PROJECT_NAME}-test
test/tests.cpp
test/app/TestClient.hpp
test/app/TestDatabaseComponent.hpp
test/app/TestComponent.hpp
test/DeviceControllerTest.hpp
test/DeviceControllerTest.cpp)
target_link_libraries(${PROJECT_NAME}-test ${PROJECT_NAME}-lib ${CONAN_LIBS})
enable_testing()
add_test(e2e-tests ${PROJECT_NAME}-test)
伴随着这个conanfile.txt
[requires]
oatpp/1.2.5 # REST library
oatpp-sqlite/1.2.5 # SQLite3 plugin
paho-mqtt-cpp/1.2.0 # MQTT Client
[generators]
cmake
只需使用常规 cmake
命令即可生成 Makefile
。然后运行make
得到可执行文件
我正在开发 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)
我在具有相同体系结构的容器中成功编译此代码后获得的可执行文件 运行s。但是当我将 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 以使用 Conan 下载的 PahoMqttCpp 库构建独立项目可执行文件(使用静态库)
事情是这样的:
我错误地配置了 CMakeLists.txt
以与 conan
集成。由于我事先安装了 paho-mqtt-cpp
,该程序链接到已安装的库,而不是柯南提供的库。
这个 CMakeLists.txt
适合我:
cmake_minimum_required(VERSION 3.1)
set(PROJECT_NAME my-awesome-project)
project(${PROJECT_NAME})
set(CMAKE_CXX_STANDARD 14)
############################### CONAN SETUP BEGIN ##########################################
if(NOT EXISTS "${CMAKE_BINARY_DIR}/conan.cmake")
message(STATUS "Downloading conan.cmake from https://github.com/conan-io/cmake-conan")
file(DOWNLOAD "https://raw.githubusercontent.com/conan-io/cmake-conan/v0.9/conan.cmake"
"${CMAKE_BINARY_DIR}/conan.cmake")
endif()
include(${CMAKE_BINARY_DIR}/conan.cmake)
conan_cmake_run(CONANFILE conanfile.txt BASIC_SETUP)
############################### CONAN SETUP END ############################################
add_library(${PROJECT_NAME}-lib
# controllers
src/controller/StaticController.hpp
# db
src/db/DemoDb.hpp
# mqtt
src/mqtt/MqttCoroutine.hpp
src/mqtt/MqttClient.hpp
# dto
src/dto/PageDto.hpp
src/dto/StatusDto.hpp
src/dto/DemoDto.hpp
# services
src/service/DemoService.cpp
src/service/DemoService.hpp
# base
src/AppComponent.hpp
src/DatabaseComponent.hpp
src/ErrorHandler.cpp
src/ErrorHandler.hpp)
## include directories
target_include_directories(${PROJECT_NAME}-lib PUBLIC src)
add_definitions(
## SQLite database file
-DDATABASE_FILE="${CMAKE_CURRENT_SOURCE_DIR}/sql/db.sqlite"
## SQLite database test file
-DTESTDATABASE_FILE="${CMAKE_CURRENT_SOURCE_DIR}/sql/test-db.sqlite"
## Path to database migration scripts
-DDATABASE_MIGRATIONS="${CMAKE_CURRENT_SOURCE_DIR}/sql"
## my-awesome-project Service
-DHOST="0.0.0.0"
-DPORT=8080
## MQTT Broker
-DMQTT_BROKER_URL="tcp://127.0.0.1:1883"
)
if(CMAKE_SYSTEM_NAME MATCHES Linux)
find_package(Threads REQUIRED)
target_link_libraries(${PROJECT_NAME}-lib INTERFACE Threads::Threads ${CMAKE_DL_LIBS})
endif()
## add executables
add_executable(${PROJECT_NAME}-exe src/App.cpp)
target_link_libraries(${PROJECT_NAME}-exe ${PROJECT_NAME}-lib ${CONAN_LIBS})
add_executable(${PROJECT_NAME}-test
test/tests.cpp
test/app/TestClient.hpp
test/app/TestDatabaseComponent.hpp
test/app/TestComponent.hpp
test/DeviceControllerTest.hpp
test/DeviceControllerTest.cpp)
target_link_libraries(${PROJECT_NAME}-test ${PROJECT_NAME}-lib ${CONAN_LIBS})
enable_testing()
add_test(e2e-tests ${PROJECT_NAME}-test)
伴随着这个conanfile.txt
[requires]
oatpp/1.2.5 # REST library
oatpp-sqlite/1.2.5 # SQLite3 plugin
paho-mqtt-cpp/1.2.0 # MQTT Client
[generators]
cmake
只需使用常规 cmake
命令即可生成 Makefile
。然后运行make
得到可执行文件