Cmake-Linker error when linking Cmake-Library in subdirectory
Hello, I'm trying to link an existing (selfwritten cmake lib) with my ros node, but when I try to link it, I get
undefined reference to ros::init(int&, char**, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned int)
and many others (all undefined references, all in the ros namespace)
My Cmake file:
cmake_minimum_required(VERSION 3.0.2)
project(ros_motor)
add_compile_options(-std=c++11)
find_package(catkin REQUIRED COMPONENTS
can_msgs
roscpp
socketcan_interface
)
catkin_package(
# INCLUDE_DIRS include
# LIBRARIES ros_motor
# CATKIN_DEPENDS can_msgs roscpp socketcan_interface
# DEPENDS system_lib
)
add_subdirectory(lib/motor/)
include_directories(
${catkin_INCLUDE_DIRS}
)
add_executable(motor_node src/motor_node.cpp)
target_link_libraries(motor_node motor ${catkin_PACKAGES})
I don't know where my issue is, else I wouldn't post here :)
Edit: My code of motor_node.cpp:
#include <ros/ros.h>
#include <can_msgs/Frame.h>
#include "motor.h"
int main(int argc, char **argv) {
ros::init(argc, argv, "motor_node");
ros::NodeHandle nh;
ros::Rate r(10);
auto motor_command_pub = nh.advertise<can_msgs::Frame>("/can/send", 10, false);
int max = 30;
int min = 10;
int current = 10;
int increment = 1;
bool rev = false;
auto ccmode = CurrentControl(0xDF);
while(!ros::isShuttingDown()) {
ccmode.setCurrent(current);
auto frame = ccmode.getMessage();
can_msgs::Frame ros_frame;
ros_frame.dlc = 8;
ros_frame.id = frame->can_id;
boost::array<uint8_t, 8> data;
auto c_frame = ros_frame.data.c_array();
c_frame = frame->data;
motor_command_pub.publish(ros_frame);
if(current < max && !rev) {
current += increment;
} else if(current == max){
rev = true;
current -= increment;
} else if(current > min){
current -= increment;
} else {
rev = false;
current += increment;
}
r.sleep();
}
}
Motor.h of the library: #ifndef MOTORH_ #define MOTORH_
#include "can_frame.h"
#include "motor_const.h"
#include "Send/MessageBuilder.h"
//#include "Recv/MultipleControllerParser.h"
#endif
Send/MessageBuilder.h:
class MessageBuilder {
protected:
uint8_t data[8];
uint8_t can_address;
public:
MessageBuilder(uint8_t address);
MessageBuilder();
can_frame* getMessage();
};
class ScanMessage : public MessageBuilder {
public:
ScanMessage();
};
class PingMessage : public MessageBuilder {
public:
PingMessage(uint8_t address);
};
class CurrentControl : public MessageBuilder {
public:
CurrentControl(uint8_t address);
void setCurrent(int32_t current);
};
Can't provide more :)
Asked by DatDucati on 2021-01-15 20:40:58 UTC
Answers
Probably because the following is a comment.
# CATKIN_DEPENDS can_msgs roscpp socketcan_interface
If you don't include CAKIN_DEPENDS roscpp
, ros::init
will not be linked with target_link_libraries(motor_node motor ${catkin_PACKAGES})
.
update
Some of them are commented because there is no code, but they were successfully built.
https://github.com/kokosabu/ros_motor
The key is to change target_link_libraries(motor_node motor ${catkin_PACKAGES})
to target_link_libraries(motor_node motor ${catkin_LIBRARIES})
.
Asked by miura on 2021-01-16 05:13:29 UTC
Comments
That didn't fix anything. I just sanity-checked my library and wrote a small demo without ros and its compiling and running. Cmake file of that project:
cmake_minimum_required(VERSION 3.0.0)
project(motor_test VERSION 0.1.0 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 11)
add_subdirectory(lib/motor)
include_directories(lib/motor/include)
add_executable(main src/main.cpp)
target_link_libraries(main motor)
Asked by DatDucati on 2021-01-16 07:43:43 UTC
The only thing I could predict from the information available was the above. If possible, could you please publish the entire code? I may be able to provide additional advice.
Asked by miura on 2021-01-16 08:07:51 UTC
Have provided more code, can't provide more sadly as the implementation is not public
Asked by DatDucati on 2021-01-16 08:18:00 UTC
@DatDucati Updated the answer.
Asked by miura on 2021-01-19 10:08:16 UTC
Comments