ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Cmake-Linker error when linking Cmake-Library in subdirectory

asked 2021-01-15 19:40:58 -0500

DatDucati gravatar image

updated 2022-01-22 16:16:16 -0500

Evgeny gravatar image

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 :)

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
0

answered 2021-01-16 04:13:29 -0500

miura gravatar image

updated 2021-01-19 09:07:28 -0500

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}).

edit flag offensive delete link more

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)
DatDucati gravatar image DatDucati  ( 2021-01-16 06:43:43 -0500 )edit

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.

miura gravatar image miura  ( 2021-01-16 07:07:51 -0500 )edit

Have provided more code, can't provide more sadly as the implementation is not public

DatDucati gravatar image DatDucati  ( 2021-01-16 07:18:00 -0500 )edit

@DatDucati Updated the answer.

miura gravatar image miura  ( 2021-01-19 09:08:16 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2021-01-15 19:40:58 -0500

Seen: 1,408 times

Last updated: Jan 19 '21