How to use catkin_make with external C static library?

asked 2016-03-17 18:34:10 -0500

msaka gravatar image


I am currently trying to use some external C functions in a ROS node. I have compiled the external C functions into a static .a library and am having problems linking it in my CMakeLists file. The static library is libmycfunc.a which I built previously based on my mycfunc.c and mycfunc.o files. When I compiled mycfunc.o the functions are dependent on additional static libraries lib1.a, lib2.a, and lib3.a. If I compile mycfunc.c with a main() as an executable, the external libraries lib1-3.a are linked properly I am able to use the executable.

As an example I am using the beginner_tutorials ROS package with the "talker" and "listener" nodes. I would like to use some of the functions in libmycfunc.a as a proof of concept however I am not very familiar with cmake. Below is my CMakeLists file:

cmake_minimum_required(VERSION 2.8.3)

find_package(catkin REQUIRED COMPONENTS

#  INCLUDE_DIRS include
#  LIBRARIES beginner_tutorials
#  CATKIN_DEPENDS roscpp rospy std_msgs
#  DEPENDS system_lib

include_directories(include ${catkin_INCLUDE_DIRS})

add_executable(talker src/talker.cpp)
add_dependencies(talker beginner_tutorials_generate_messages_cpp)
target_link_libraries(talker ${catkin_LIBRARIES}

add_executable(listener src/listener.cpp)
add_dependencies(listener beginner_tutorials_generate_messages_cpp)
target_link_libraries(listener ${catkin_LIBRARIES})

However when I catkin_make the beginner_tutorials package, I get errors:

Linking CXX executable /home/sakagma/catkin_ws/devel/lib/beginner_tutorials/talker
<path_to_c_func>/mycfunc.c: undefined reference to 'func1':

where "func1" is a function defined in the linked static library lib1.a used in mycfunc.c.

How do I properly link my static library libmycfunc.a so that I can call those functions in talker.cpp?

Note: In talker.cpp I am referencing my C functions by including:

extern "C" {
     #include "mycfunc.h"

Any help would be appreciated!

edit retag flag offensive close merge delete



Hi msaka, did you find a solution eventually ?

ankalou gravatar image ankalou  ( 2016-04-19 08:52:49 -0500 )edit

Also currently have this issue. Any help would be much appreciated!

awaters gravatar image awaters  ( 2020-12-10 00:42:04 -0500 )edit