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 _MOTOR_H_ #define _MOTOR_H_
#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 :)