《CMake实践》笔记三:构建静态库(.a) 与 动态库(.so) 及,安装,以及如何使用外部共享库和头文件
https://www.cnblogs.com/52php/p/5681755.html
小结:
0. Cmake语句find_package()函数 https://blog.csdn.net/reasonyuanrobot/article/details/117338283
1.使用catkin时,对于自建未安装package作为依赖项时,需使用find_package(catkin REQUIRED COMPONENTS package_1 package_1 );类似如下:
find_package(catkin REQUIRED COMPONENTS
utils
apriltags
)
或者
使用find_package(package_1 REQUIRED ),需要对package_1的头文件和库文件进行显示关联。
find_package(catkin REQUIRED COMPONENTS
)find_package(utils REQUIRED
)
find_package(apriltags REQUIRED
)
include_directories(${utils_INCLUDE_DIRS} ${apriltags_INCLUDE_DIRS})add_library(${PROJECT_NAME} src/somes.cpp)
target_link_libraries(${PROJECT_NAME}${catkin_LIBRARIES}${apriltags_LIBRARIES}${utils_LIBRARIES})
2. CMakeLists.txt添加opencv库注意事项 https://blog.csdn.net/u012816621/article/details/51732932
简易模板
- - 相关工程类比着写就行,模板中有相关注释说明
cmake_minimum_required(VERSION 2.8.12)project(robot_server)## 0. add definition
add_definitions(-std=c++11)
add_definitions(-DLINUXSOCKETS=1) #use linux sockets for communication
add_definitions(-DBOOST_ERROR_CODE_HEADOR_ONLY) # for boost## 1.debug or release
# set(CMAKE_BUILD_TYPE Release)
set(CMAKE_BUILD_TYPE Debug)## 2.find_package, catkin_package, include
find_package(catkin REQUIRED COMPONENTS simple_message
)
find_package(Boost REQUIRED system threadyaml-cpp) #for boostif(NOT Boost_FOUND)message("NOT found Boost")
endif()# include_directories(untitled ${Boost_INCLUDE_DIRS})
# link_directories(untitled ${Boost_LIBRARY_DIRS})
# target_link_libraries(untitled ${Boost_LIBRARIES})catkin_package(CATKIN_DEPENDSDEPENDSINCLUDE_DIRS includeLIBRARIES ${PROJECT_NAME}simple_message
)include_directories(include ${catkin_INCLUDE_DIRS}
)
include_directories(${Boost_INCLUDE_DIRS}) # for boost
# include_directories(include/control include/plan ${catkin_INCLUDE_DIRS}
# )## 3.set src files; 生成库文件.a / .so ; 连接相关依赖库文件link libset(SRC_FILES src/common.cppsrc/joint_trajectory_handler_download.cppsrc/joint_trajectory_server.cppsrc/robot_interface.cppsrc/robot_state_server.cpp)add_library(${PROJECT_NAME} ${SRC_FILES}
) # 生成库文件target_link_libraries(${PROJECT_NAME}${catkin_LIBRARIES}simple_messagepthread${Boost_LIBRARIES}
) # link lib## 4. main() 生成可执行文件add_executable(traj_server
src/joint_trajectory_server_node.cpp)
target_link_libraries(traj_server ${PROJECT_NAME}${Boost_LIBRARIES})add_executable(state_server
src/robot_state_server_node.cpp)
target_link_libraries(state_server ${PROJECT_NAME})
最外层CMakeLists.txt --- 通用,无需更改
# toplevel CMakeLists.txt for a catkin workspace
# catkin/cmake/toplevel.cmakecmake_minimum_required(VERSION 2.8.3)set(CATKIN_TOPLEVEL TRUE)# search for catkin within the workspace
set(_cmd "catkin_find_pkg" "catkin" "${CMAKE_SOURCE_DIR}")
execute_process(COMMAND ${_cmd}RESULT_VARIABLE _resOUTPUT_VARIABLE _outERROR_VARIABLE _errOUTPUT_STRIP_TRAILING_WHITESPACEERROR_STRIP_TRAILING_WHITESPACE
)
if(NOT _res EQUAL 0 AND NOT _res EQUAL 2)# searching fot catkin resulted in an errorstring(REPLACE ";" " " _cmd_str "${_cmd}")message(FATAL_ERROR "Search for 'catkin' in workspace failed (${_cmd_str}): ${_err}")
endif()# include catkin from workspace or via find_package()
if(_res EQUAL 0)set(catkin_EXTRAS_DIR "${CMAKE_SOURCE_DIR}/${_out}/cmake")# include all.cmake without add_subdirectory to let it operate in same scopeinclude(${catkin_EXTRAS_DIR}/all.cmake NO_POLICY_SCOPE)add_subdirectory("${_out}")else()# use either CMAKE_PREFIX_PATH explicitly passed to CMake as a command line argument# or CMAKE_PREFIX_PATH from the environmentif(NOT DEFINED CMAKE_PREFIX_PATH)if(NOT "$ENV{CMAKE_PREFIX_PATH}" STREQUAL "")string(REPLACE ":" ";" CMAKE_PREFIX_PATH $ENV{CMAKE_PREFIX_PATH})endif()endif()# list of catkin workspacesset(catkin_search_path "")foreach(path ${CMAKE_PREFIX_PATH})if(EXISTS "${path}/.catkin")list(FIND catkin_search_path ${path} _index)if(_index EQUAL -1)list(APPEND catkin_search_path ${path})endif()endif()endforeach()# search for catkin in all workspacesset(CATKIN_TOPLEVEL_FIND_PACKAGE TRUE)find_package(catkin QUIETNO_POLICY_SCOPEPATHS ${catkin_search_path}NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)unset(CATKIN_TOPLEVEL_FIND_PACKAGE)if(NOT catkin_FOUND)message(FATAL_ERROR "find_package(catkin) failed. catkin was neither found in the workspace nor in the CMAKE_PREFIX_PATH. One reason may be that no ROS setup.sh was sourced before.")endif()
endif()catkin_workspace()
使用socket通信时:
add_definitions(-DLINUXSOCKETS=1) #use linux sockets for communication
否则可能会报错:
- error: ‘sockaddr_in’ does not name a type
- error: ‘errno’ was not declared in this scope
使用boost库时的配置:
https://blog.csdn.net/reasonyuanrobot/article/details/106723854 CMakeLists.txt 中配置Boost库