ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

How to add a linked library for a cpp file in the CMakelists.

asked 2017-07-26 02:24:08 -0600

vcdanda gravatar image

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 ...
(more)
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2017-07-26 03:51:20 -0600

Akif gravatar image

You can try adding

find_package(PkgConfig REQUIRED)
pkg_check_modules(JSONCPP jsoncpp)

and changing include_directories and target_link_libraries as

include_directories(
  ${catkin_INCLUDE_DIRS}
  ${JSONCPP_INCLUDE_DIRS}
)

and

target_link_libraries(json
  ${catkin_LIBRARIES}
  ${JSONCPP_LIBRARIES}
)

Good luck.

edit flag offensive delete link more

Comments

Thanks Akif. I've tried your method recently. It didn't work because it tells me an error:

undefined reference to 'json::Value::Value'

so the cpp cannot find the location of the json file.

I've tried other method like directly add jsoncpp below target_link_directories. It workd.

vcdanda gravatar image vcdanda  ( 2017-12-05 00:11:07 -0600 )edit

But this jsoncpp is only a jsoncpp.a which is a static library. I want to add it in my path planner, it seems it cannot accept the static library there. You can see this question. https://answers.ros.org/question/2772... but now I don't know how add dynamic lib.

vcdanda gravatar image vcdanda  ( 2017-12-05 00:14:46 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-07-26 02:24:08 -0600

Seen: 2,486 times

Last updated: Jul 26 '17