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

CMake error trying to link target to libraries

asked 2015-12-23 05:04:45 -0600

darshandoria gravatar image

updated 2015-12-28 02:55:00 -0600

gvdhoorn gravatar image

Dear All, I am using Ubuntu 14.04 and ROS Indigo.

I have created a pkg named "joy" in which there are two source file named joystick.cpp and js_info.cpp under joy/src folder. also i have header file joystick.h under include/joy folder.

i have compiled that 3 files and they work correctly.

But when i placed this files as told you above, gives me error as shown below:

CMake Error at joy/CMakeLists.txt:61 (target_link_libraries):
  Target "joystick" of type EXECUTABLE may not be linked into another target.
  One may link only to STATIC or SHARED libraries, or to executables with the
  ENABLE_EXPORTS property set.

-- Configuring incomplete, errors occurred!
See also "/home/darshan/catkin_ws/build/CMakeFiles/CMakeOutput.log".
See also "/home/darshan/catkin_ws/build/CMakeFiles/CMakeError.log".
make: *** [cmake_check_build_system] Error 1
Invoking "make cmake_check_build_system" failed

This below shown is my CMakeLists.txt file:

cmake_minimum_required(VERSION 2.8.3)
project(joy)

find_package(catkin REQUIRED COMPONENTS
  roscpp
  rospy
  std_msgs
)
catkin_package(
  INCLUDE_DIRS include
  CATKIN_DEPENDS roscpp rospy std_msgs
)
include_directories(include
  ${catkin_INCLUDE_DIRS}
)
add_executable(joystick src/joystick.cpp)
target_link_libraries(joystick ${catkin_LIBRARIES})

add_executable(js_info src/js_info.cpp)
target_link_libraries(js_info ${catkin_LIBRARIES} joystick)

Please provide me the solution. I would be thankful to all of you.


Edit: Thank you so much for quick and prompt reply.

But if i change that line to

target_link_libraries(js_info ${catkin_LIBRARIES} )

like this. I get the following error.

darshan@darshan-Inspiron-N4010:~/catkin_ws$ catkin_make
Base path: /home/darshan/catkin_ws
Source space: /home/darshan/catkin_ws/src
Build space: /home/darshan/catkin_ws/build
Devel space: /home/darshan/catkin_ws/devel
Install space: /home/darshan/catkin_ws/install
####
#### Running command: "make cmake_check_build_system" in "/home/darshan/catkin_ws/build"
####
####
#### Running command: "make -j4 -l4" in "/home/darshan/catkin_ws/build"
####
[  0%] Built target _rosserial_arduino_generate_messages_check_deps_Test
[  0%] [  0%] [  0%] Built target _rosserial_msgs_generate_messages_check_deps_RequestParam
Built target _rosserial_msgs_generate_messages_check_deps_RequestServiceInfo
Built target _rosserial_arduino_generate_messages_check_deps_Adc
[  0%] Built target _rosserial_msgs_generate_messages_check_deps_Log
[  0%] Built target _rosserial_msgs_generate_messages_check_deps_TopicInfo
[  0%] Built target _rosserial_msgs_generate_messages_check_deps_RequestMessageInfo
[  0%] Built target std_msgs_generate_messages_py
[ 13%] Built target abc
[ 13%] Built target _beginner_tutorials_generate_messages_check_deps_Num
[ 13%] Built target std_msgs_generate_messages_lisp
[ 13%] Built target _beginner_tutorials_generate_messages_check_deps_AddTwoInts
[ 13%] Built target std_msgs_generate_messages_cpp
[ 15%] Built target hello
Linking CXX executable /home/darshan/catkin_ws/devel/lib/joy/joystick
[ 28%] Built target gui_ros
Linking CXX executable /home/darshan/catkin_ws/devel/lib/joy/js_info
[ 30%] Built target hello_world
[ 43%] Built target example
[ 46%] Built target rosserial_arduino_generate_messages_lisp
[ 47%] Built target turtle_teleop_joy
[ 53%] Built target rosserial_arduino_generate_messages_py
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 1 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 2 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 3 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 4 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 5 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug ...
(more)
edit retag flag offensive close merge delete

Comments

May I suggest to change the title of your question? It has nothing to do with header files. Perhaps something like "CMake error trying to link target to libraries" is more appropriate.

gvdhoorn gravatar image gvdhoorn  ( 2015-12-23 06:26:19 -0600 )edit

Also, a ROS package with the name joy already exists. This will cause problems if you need both packages at the same time.

http://wiki.ros.org/joy

kmhallen gravatar image kmhallen  ( 2015-12-23 17:55:02 -0600 )edit

Please do not post answers to provide us with more information, update your question description for that, using the edit link/button below it. I've moved the contents of your answer this time.

gvdhoorn gravatar image gvdhoorn  ( 2015-12-28 02:45:44 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2015-12-23 05:18:56 -0600

gvdhoorn gravatar image

updated 2015-12-28 02:51:13 -0600

The error you get is actually quite clear, especially when you look at the following snippet from your CMakeLists.txt:

add_executable(joystick src/joystick.cpp)
target_link_libraries(joystick ${catkin_LIBRARIES})

add_executable(js_info src/js_info.cpp)
target_link_libraries(js_info ${catkin_LIBRARIES} joystick)

You are trying to link js_info against catkin_LIBRARIES (good) and against joystick (not good). That can't work: joystick is an executable, you just declared it 4 lines earlier with add_executable(joystick src/joystick.cpp). You cannot link two executables together.

Also: this is really not ROS related, it's basic CMake and building of executables / binaries.


Edit:

[..] if i change that line to

target_link_libraries(js_info ${catkin_LIBRARIES} )

like this. I get the following error:

[..]
CMakeFiles/js_info.dir/src/js_info.cpp.o: In function `main':
js_info.cpp:(.text+0x7a): undefined reference to `Joystick::Joystick(char const*)'
js_info.cpp:(.text+0x204): undefined reference to `Joystick::getName()'
js_info.cpp:(.text+0x234): undefined reference to `Joystick::numAxes()'
[..]

This seems to indicate that the js_info target requires symbols from joystick.cpp. You could either:

  • make the joystick target a library (add_library(..) instead of add_executable(..)) and link js_info against that, or
  • add joystick.cpp to the list of source files for the js_info target
edit flag offensive delete link more

Comments

Thank You so much sir, for your great help ! Made changes as suggested by you solved my problem. Thank you once again.

darshandoria gravatar image darshandoria  ( 2015-12-28 06:16:23 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-12-23 05:04:45 -0600

Seen: 9,821 times

Last updated: Dec 28 '15