Robotics StackExchange | Archived questions

Undefined reference to c function from manufacturer API

Hello, I'm trying to bring ST Electronics VL53L5CXULDlinux API, which is just a set of c functions that enable me to talk to a sensor over I2C, found here, into ROS . I was able to run the API successfully outside ROS, but once inside ROS when trying to include it inside a c++ script I'm running into an issue that I think is caused from mixing c and c++.

My approach was to create a ROS package and add the API (.c into src, .h into include/package_name) into a working publisher node with the API header file included. I got this to compile just fine, but when actually trying to call any functions from the API I get this error:

[100%] Linking CXX executable /home/avt/catkinVL53L5CX/devel/lib/vl53l5cx/vl53l5cxnode CMakeFiles/vl53l5cxnode.dir/src/vl53l5cxnode.cpp.o: In function main': vl53l5cx_node.cpp:(.text+0x10c): undefined reference tovl53l5cxcommsinit(VL53L5CXPlatform*)' collect2: error: ld returned 1 exit status vl53l5cx/CMakeFiles/vl53l5cxnode.dir/build.make:112: recipe for target '/home/avt/catkinVL53L5CX/devel/lib/vl53l5cx/vl53l5cxnode' failed make[2]: *** [/home/avt/catkinVL53L5CX/devel/lib/vl53l5cx/vl53l5cxnode] Error 1 CMakeFiles/Makefile2:944: recipe for target 'vl53l5cx/CMakeFiles/vl53l5cxnode.dir/all' failed make[1]: *** [vl53l5cx/CMakeFiles/vl53l5cxnode.dir/all] Error 2 Makefile:140: recipe for target 'all' failed make: *** [all] Error 2

My best guess is that the c++ compiler is mangling the names of the functions. Can anyone help me understand why I'm getting "undefined reference" to a function in a library that is seemingly compiling and linking fine?

Below are my CMakelists.txt and c++ code. I'm using a Jetson Nano, Ubuntu 18.04, ROS Melodic

//ROS Publisher Node
#include "ros/ros.h"
#include "std_msgs/String.h"
#include <unistd.h>
#include <signal.h>
#include <dlfcn.h>
#include <stdio.h>
#include <string.h>
extern "C"{
#include "vl53l5cx_api.h"
}

int main(int argc, char **argv){
ros::init(argc, argv, "Publisher");
ros::NodeHandle nh;

ros::Publisher topic_pub = nh.advertise<std_msgs::String>("distance_array", 1000);
ros::Rate loop_rate(1);


char choice[20];
int status;
VL53L5CX_Configuration  Dev;

status = vl53l5cx_comms_init(&Dev.platform);
if(status)
{
    printf("VL53L5CX comms init failed\n");
    return -1;
}

while(ros::ok()){
    std_msgs::String msg;
    msg.data = "Distance Array Here";

    topic_pub.publish(msg);
    ros::spinOnce();
    loop_rate.sleep();
}
}

CMakelists.txt

cmake_minimum_required(VERSION 3.0.2)
project(vl53l5cx)

find_package(catkin REQUIRED COMPONENTS
roscpp
std_msgs
genmsg
)

## Generate added messages and services with any dependencies listed here
generate_messages(
DEPENDENCIES
std_msgs
 )

 catkin_package(
 INCLUDE_DIRS include
 LIBRARIES vl53l5cx
 CATKIN_DEPENDS roscpp std_msgs
 #  DEPENDS system_lib
 )

include_directories(
include
include/vl53l5cx
${catkin_INCLUDE_DIRS}
)

## Declare a C++ library
add_library(vl53l5cx
src/vl53l5cx_api.c
src/platform.c
src/vl53l5cx_plugin_detection_thresholds.c
src/vl53l5cx_plugin_motion_indicator.c
src/vl53l5cx_plugin_xtalk.c
)

add_executable(vl53l5cx_node src/vl53l5cx_node.cpp)


target_link_libraries(vl53l5cx_node
${catkin_LIBRARIES}
)

Asked by iamthenarwal on 2023-06-22 19:56:20 UTC

Comments

Answers

add_library(vl53l5cx ...)

where do you link against this library?

Asked by gvdhoorn on 2023-06-23 00:58:24 UTC

Comments

@gvdhoorn Your question actually just answered my question. So thank you very much for that!! I'm brand new to the world of CMakelists and I thought that I had linked everything properly because it was compiling fine. But I needed to add my library under target_link_libraries() and all was well. Thanks again!

Asked by iamthenarwal on 2023-06-23 11:48:18 UTC