1

I am trying to use the Kilobot simulator Kilombo with a ROS package. I am installing Kilombo as normal and then trying to include the Kilombo header file inside the ROS package. The CMakeLists.txt of Kilombo looks like this

add_library(sim display.c skilobot.c kbapi.c params.c stateio.c runsim.c neighbors.c distribution.c gfx/SDL_framerate.c gfx/SDL_gfxPrimitives.c gfx/SDL_gfxBlitFunc.c gfx/SDL_rotozoom.c)

add_library(headless skilobot.c kbapi.c params.c stateio.c runsim.c neighbors.c distribution.c)
set_target_properties(headless PROPERTIES COMPILE_DEFINITIONS "SKILO_HEADLESS")

if(CMAKE_COMPILER_IS_GNUCXX)
    add_definitions(-std=c99)
    add_definitions("-Wall -O2 -g")
#    add_definitions("-Wall -O3 -march=native -g")  
endif()

INSTALL(TARGETS sim headless
  ARCHIVE DESTINATION lib
)

INSTALL(FILES kilombo.h DESTINATION include)

INSTALL(FILES kilolib.h message.h message_crc.h params.h skilobot.h
    DESTINATION include/kilombo)

add_subdirectory(tests)

The CMakeLists.txt of the ROS package I am creating looks like this:

cmake_minimum_required(VERSION 2.8.3)
project(Kilombo_test)
find_package(catkin REQUIRED roscpp std_msgs)
if(CMAKE_COMPILER_IS_GNUCXX)
    add_definitions(-std=c99)
    add_definitions("-Wall -O2 -g")
endif()
include_directories(/usr/local/include /usr/local/lib)
link_directories(/usr/local/include)
add_executable(generated_test orbit.c)
target_link_libraries(generated_test ${catkin_LIBRARIES})

I include the Kilombo header files just as normal: "#include <kilombo.h>"

However, when I run catkin_make, I get many "undefined reference" errors. Some of those messages are:

orbit.c:53 undefined reference to 'kilo_turn_left'
orbit.c:53 undefined reference to 'set_motors'

These messages are shown after the commands:

####
#### Running command: "make -j2 -l2" in "/home/viki/catkin_ws/build"
####
Linking C executable generated_test

Both, kilo_turn_left and the function set_motors is defined in "kilolib.h" which itself is included in "kilombo.h".

Everything works fine if try running the simulation normally and not as a ROS package. The Makefile when I try to run it normally looks like the following. I have removed the parts which compiled for the real bot and not for the simulation.

SOURCES=orbit.c 
EXECUTABLE=orbit

# path of kilombo.h distributed with the simulator but also needed
# when compiling the user program for the real kilobot (avr-gcc has different default paths)
SIMHEADERS=/usr/local/include

#path to kilolib.a in the official kilolib, needed for real bots only
KILOLIB    =$(KILOHEADERS)/build/kilolib.a

SIM_CFLAGS = -c -g -O2 -Wall -std=c99  

SIM_LFLAGS = -lsim -lSDL -lm -ljansson
sim: $(EXECUTABLE)
hex: $(EXECUTABLE).hex
all: sim hex

clean :
    rm *.o $(EXECUTABLE) *.elf *.hex

However, when running a simulation as a ROS package, we need to define a CMakeLists.txt which is what I am having trouble with. What exactly am I doing wrong?

Rabee
  • 637
  • 5
  • 19
  • There isn't enough information to allow us to help. We need to see some (not necessarily all) of the undefined reference messages. Where do you think those names should be defined? What command is executed when the undefined reference messages are generated? – Jonathan Leffler Sep 03 '17 at 14:11
  • You have to add a reference to Kilombo package in CMakeLists.txt of the ROS. Or you can use other simulators like Kelpie or Gazebo along with ROS – svtag Sep 03 '17 at 14:33
  • @JonathanLeffler I have added more details to the question. Please have another look. – Rabee Sep 06 '17 at 10:58
  • @Sma How do I add the reference to the Kilombo package correctly in the CMakeLists.txt? Using another simulator is not an option. – Rabee Sep 06 '17 at 10:58
  • There comes a point when the nice messages produced by cmake during a build become counter-productive, usually when something goes wrong. This is an example. Saying "linking" gives me nothing to work on, and I don't know enough cmake to know how to push you in the right direction. Are the symbols you give as examples ones from the Kilombo library, or from the local source code? – Jonathan Leffler Sep 06 '17 at 13:43
  • @JonathanLeffler Those are defined in the Kilombo library. Is there any more info that I can add which would help? – Rabee Sep 06 '17 at 14:18
  • I'd need a tutorial on cmake to be able to help much more. I know of it rather than know it. There probably is a way to make it show you what commands it is executing, but I don't know what it is. Are any of the functions your code calls from Kilombo found, or is every single Kilombo function that you call missing? Does the actual linking command line include the Kilombo library? Is there any message about "library not found"? Does the reference to the library appear after the local object files. – Jonathan Leffler Sep 06 '17 at 14:24
  • @JonathanLeffler Every single function call is missing. What is interesting is that the actual "#include " line does not throw an error which means that it finds the header file. But inside "kilombo.h", another file called "kilolib.h" is included which has the functions defined. And somehow it can find the header file but not the functions. What do you mean by "actual linking command line"? No, there is no message about "library not found". – Rabee Sep 06 '17 at 19:01
  • The headers declare the functions; it is very unlikely that they define them. 'Actual linking command line' means that `cmake` is invoking a C compiler (probably GCC, aka `gcc`) with a bunch of arguments to indicate that it is linking and telling it what to link. My suspicion is that this command line is faulty - my current best guess is that the options list the library before the object files and this is broken behaviour on part of `cmake` or the rules in the files. But I'm not expert enough in `cmake` to be able to help more. I might be able to confirm a diagnosis with the command line. – Jonathan Leffler Sep 06 '17 at 19:11
  • You are right, declare and not define. Yeah, unfortunately I have to use CMake because I am using ROS. I have posted this question on the ROS forum. Perhaps that will be more helpful. Thanks! – Rabee Sep 06 '17 at 19:19

1 Answers1

1

The problem was that I wasn't linking to the correct libraries in the CMakeLists.txt of the simulation. Changing the line target_link_libraries(generated_test ${catkin_LIBRARIES}) to target_link_libraries(generated_test ${catkin_LIBRARIES} sim headless SDL m Jansson) fixed it.

Rabee
  • 637
  • 5
  • 19