How to add a linked library for a cpp file in the CMakelists.
Hi, this is a new user to use Cmakelists. I want to use a C++ file which can help me analysis the json file. Normally, when I make it executable, I run g++ json.cpp -o -ljson However, when I tried to add the c++ file in the ros package, I have some problems to make them.
Here is my original makefile:
LIB=-L/usr/local/lib/libjson/ -ljson
haha: a.o
g++ -o a -std=c++0x a.o $(LIB)
sdfds: a.cpp
g++ -c a.cpp
# dfasasdf:
# rm a.o a
clean:
rm edit a.o
Here is my CMakelist.txt: cmake_minimum_required(VERSION 2.8.3) project(rosauth)
## 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 roscpp message_generation rostest)
#######################################
## Declare ROS messages and services ##
#######################################
## Generate services in the 'srv' folder
add_service_files(
FILES
Authentication.srv
)
## Generate added messages and services with any dependencies listed here
generate_messages(
DEPENDENCIES
)
###################################################
## 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(CATKIN_DEPENDS roscpp message_runtime)
###########
## Build ##
###########
include_directories(
${catkin_INCLUDE_DIRS}
)
include_directories(
/opt/JSON/jsoncpp-src-0.5.0/libs/linux-gcc-4.8/
)
## Declare a cpp executable
add_executable(ros_mac_authentication src/ros_mac_authentication.cpp)
## Specify libraries to link a library or executable target against
target_link_libraries(ros_mac_authentication
${catkin_LIBRARIES} crypto
)
add_dependencies(ros_mac_authentication ${PROJECT_NAME}_gencpp)
#############
## Install ##
#############
## Mark executables and/or libraries for installation
install(TARGETS ros_mac_authentication
RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)
#############
## Testing ##
#############
if(CATKIN_ENABLE_TESTING)
## Add gtest based cpp test target and link libraries
add_executable(ros_mac_authentication_test test/ros_mac_authentication_test.cpp)
add_dependencies(ros_mac_authentication_test ${PROJECT_NAME}_gencpp)
target_link_libraries(ros_mac_authentication_test ${catkin_LIBRARIES} ${GTEST_LIBRARIES} crypto dl pthread)
add_rostest(test/ros_mac_authentication.test)
endif()
add_executable(json
src/json.cpp
)
add_dependencies(json ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
target_link_libraries(json
${catkin_LIBRARIES} json_linux-gcc-4.8_libmt
)
And I catkin_make it. it gives me this hint.
Linking CXX executable /home/jimmy/catkin_ws/devel/lib/rosauth/json
Scanning dependencies of target sick_tim310s01
Scanning dependencies of target rbx2_utils_generate_messages
Scanning dependencies of target sick_tim310
Scanning dependencies of target sick_tim310_1130000m01
Scanning dependencies of target sick_tim551_2050001
Scanning dependencies of target sick_tim_datagram_test
Scanning dependencies of target amcl
[ 84%] Built target rbx2_utils_generate_messages
Scanning dependencies of target robot_pose_ekf_gencpp
[ 84%] Built target robot_pose_ekf_gencpp
Scanning dependencies of target robot_pose_ekf_generate_messages
[ 84%] Built target robot_pose_ekf_generate_messages
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 1 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 2 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 3 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 4 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 5 has invalid symbol index 13
/usr/bin/ld ...