I'm trying to compile a ROS node, where a use the robotcontrol header as an extern "C" include in my C++ code. Compiling fails at the point, where it can not find the references to several functions of the robotcontrol library that I use (e.g.:)
mspeed.cpp:(.text+0x798): undefined reference to `rc_uart_write'
mspeed.cpp:(.text+0x7ba): undefined reference to `rc_uart_flush'
mspeed.cpp:(.text+0x7ce): undefined reference to `rc_uart_write'
I tried several ways to include the library binary in my CMakeLists file that I compile using catkin_make. Did anyone use C code (as binary not from source) within a ROS node before? It might be that I leak knowledge on C and C++ or compiling with cmake in general, so I'm happy for any advice.
All I found so far has used the raw c files.
So this is how I include the library:
#ifdef __cplusplus
extern "C"
{
#endif
#include <robotcontrol.h> // includes ALL Robot Control subsystems
#ifdef __cplusplus
}
#endif
I'm calling the functions then like here:
rc_uart_write(bus, (uint8_t*)data[bus2mem(bus)], data_count[bus2mem(bus)]);
The code itself works, when I use it just as a C code. But I need to include it into the ROS node. To compile the plain C version I have another Makefile that comes along with the robotcontrol library. Do I need to provide some other compile flags to my solution?:
# This is a general use makefile for librobotcontrol projects written in C.
# Just change the target name to match your main source code filename.
TARGET = driver_communication
# compiler and linker binaries
CC := gcc
LINKER := gcc
# compiler and linker flags
WFLAGS := -Wall -Wextra -Werror=float-equal -Wuninitialized -Wunused-variable -Wdouble-promotion
CFLAGS := -g -c -Wall
LDFLAGS := -pthread -lm -lrt -l:librobotcontrol.so.1
SOURCES := $(wildcard *.c)
INCLUDES := $(wildcard *.h)
OBJECTS := $(SOURCES:$%.c=$%.o)
prefix := /usr/local
RM := rm -f
INSTALL := install -m 4755
INSTALLDIR := install -d -m 755
SYMLINK := ln -s -f
SYMLINKDIR := /etc/robotcontrol
SYMLINKNAME := link_to_startup_program
# linking Objects
$(TARGET): $(OBJECTS)
@$(LINKER) -o $@ $(OBJECTS) $(LDFLAGS)
@echo "Made: $@"
# compiling command
$(OBJECTS): %.o : %.c $(INCLUDES)
@$(CC) $(CFLAGS) $(WFLAGS) $(DEBUGFLAG) $< -o $@
@echo "Compiled: $@"
all: $(TARGET)
debug:
$(MAKE) $(MAKEFILE) DEBUGFLAG="-g -D DEBUG"
@echo " "
@echo "$(TARGET) Make Debug Complete"
@echo " "
install:
@$(MAKE) --no-print-directory
@$(INSTALLDIR) $(DESTDIR)$(prefix)/bin
@$(INSTALL) $(TARGET) $(DESTDIR)$(prefix)/bin
@echo "$(TARGET) Install Complete"
clean:
@$(RM) $(OBJECTS)
@$(RM) $(TARGET)
@echo "$(TARGET) Clean Complete"
uninstall:
@$(RM) $(DESTDIR)$(prefix)/bin/$(TARGET)
@echo "$(TARGET) Uninstall Complete"
runonboot:
@$(MAKE) install --no-print-directory
@$(SYMLINK) $(DESTDIR)$(prefix)/bin/$(TARGET) $(SYMLINKDIR)/$(SYMLINKNAME)
@echo "$(TARGET) Set to Run on Boot"
My CMakeLists.txt file looks like this at the moment (not including any link to the robotcontrol library explicitly:
cmake_minimum_required(VERSION 2.8.3)
project(indoor_navigation)
find_package(catkin REQUIRED COMPONENTS
message_generation
message_runtime
roscpp
rospy
sensor_msgs
std_msgs
)
## Generate messages in the 'msg' folder
add_message_files(
FILES
Directions.msg
)
## Generate added messages and services with any dependencies listed here
generate_messages(
DEPENDENCIES
sensor_msgs
std_msgs
)
catkin_package(
LIBRARIES indoor_navigation
)
## Specify additional locations of header files
## Your package locations should be listed before other locations
include_directories(
# include
${catkin_INCLUDE_DIRS}
)
add_executable(directions src/directions.cpp)
add_executable(mspeed src/mspeed.cpp)
add_dependencies(directions ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
add_dependencies(directions ${PROJECT_NAME}_gencfg)
add_dependencies(directions ${PROJECT_NAME}_generate_messages_cpp)
add_dependencies(mspeed ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
add_dependencies(mspeed ${PROJECT_NAME}_gencfg)
add_dependencies(mspeed ${PROJECT_NAME}_generate_messages_cpp)
target_link_libraries(directions ${catkin_LIBRARIES})
target_link_libraries(mspeed ${catkin_LIBRARIES})
I tried to include the lib in my CMakeLists with an
add_executable(robotcontrol /usr/lib/robotcontrol)
which however expects a source file
I tried several ways of
target_link_libraries(...)
to be honest, I tried that many versions that I don't know which ones and I know this makes helping for you guys much harder. So I keep trying and collect these.
Thanks for the support. Basically I had a missunderstanding of the linking process. One has to add the library first, which can be done with the existing shared object file:
add_library(librobotcontrol SHARED IMPORTED GLOBAL)
set_target_properties(librobotcontrol PROPERTIES IMPORTED_LOCATION /usr/lib/librobotcontrol.so)
So far so good... all left to do now is to link the file I want to use the library in, against my typical libraries plus the newly added one:
target_link_libraries(mspeed ${catkin_LIBRARIES} robotcontrol)