1.原文件:
cmake_minimum_required(VERSION 2.8.3)
project(usb_cam)
Find catkin macros and libraries
if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
is used, also find other catkin packages
find_package(catkin REQUIRED COMPONENTS image_transport roscpp std_msgs std_srvs sensor_msgs camera_info_manager)
pkg-config libraries
find_package(PkgConfig REQUIRED)
pkg_check_modules(avcodec libavcodec REQUIRED)
pkg_check_modules(swscale libswscale REQUIRED)
###################################################
Declare things to be passed to other projects
###################################################
LIBRARIES: libraries you create in this project that dependent projects also need
CATKIN_DEPENDS: catkin_packages dependent projects also need
DEPENDS: system dependencies of this project that dependent projects also need
catkin_package(
INCLUDE_DIRS include
LIBRARIES ${PROJECT_NAME}
)
###########
Build
###########
include_directories(include
${catkin_INCLUDE_DIRS}
${avcodec_INCLUDE_DIRS}
${swscale_INCLUDE_DIRS}
)
Declare a cpp executable
add_executable(
P
R
O
J
E
C
T
N
A
M
E
n
o
d
e
n
o
d
e
s
/
u
s
b
c
a
m
n
o
d
e
.
c
p
p
)
t
a
r
g
e
t
l
i
n
k
l
i
b
r
a
r
i
e
s
(
{PROJECT_NAME}_node nodes/usb_cam_node.cpp) target_link_libraries(
PROJECTNAMEnodenodes/usbcamnode.cpp)targetlinklibraries({PROJECT_NAME}_node
${PROJECT_NAME}
${avcodec_LIBRARIES}
${swscale_LIBRARIES}
${catkin_LIBRARIES}
)
#############
Install
#############
Mark executables and/or libraries for installation
install(TARGETS ${PROJECT_NAME}_node ${PROJECT_NAME}
RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
)
Copy launch files
install(DIRECTORY launch/
DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}/launch
FILES_MATCHING PATTERN “*.launch”
)
install(DIRECTORY include/${PROJECT_NAME}/
DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
FILES_MATCHING PATTERN “.h" PATTERN ".hpp”
)
2.改动:
1)在add_executable前添加以下片段,即首先将h文件对应的cpp函数通过add_library封装成so库,同时通过target_link_libraries将cpp本身所需要链接的库也添加进来(此时可照抄Cmakelists文件修改前的add_executable位置下面的target_link_libraries所包含的LIBRARIES)
Build the USB camera library
add_library(mylibname src/usb_cam.cpp)
target_link_libraries(mylibname
${avcodec_LIBRARIES}
${swscale_LIBRARIES}
${catkin_LIBRARIES}
)
2)编译生成so库,生成后是默认存储在devel的lib文件夹中,将其复制拷贝到任意自己想要的位置。
3)在Cmakelists中将add_library这一段生成库的片段删除,将对应的cpp文件全部删除,然后将生成的so文件的绝对路径放置在dd_executable位置下面的target_link_libraries的括号中。
Declare a cpp executable
add_executable(
P
R
O
J
E
C
T
N
A
M
E
n
o
d
e
n
o
d
e
s
/
u
s
b
c
a
m
n
o
d
e
.
c
p
p
)
t
a
r
g
e
t
l
i
n
k
l
i
b
r
a
r
i
e
s
(
{PROJECT_NAME}_node nodes/usb_cam_node.cpp) target_link_libraries(
PROJECTNAMEnodenodes/usbcamnode.cpp)targetlinklibraries({PROJECT_NAME}_node
mylibname
${avcodec_LIBRARIES}
${swscale_LIBRARIES}
${catkin_LIBRARIES}
)