Linker problem ROS1 windows10 undefined reference to ros init

asked 2019-09-30 07:45:04 -0500

Marcel Usai gravatar image

Hi, I am trying to compile a very simple code on windows10 (ROS melodic) with mingw73_64. I have to use cmake only (no catkin). I know this works pretty well on linux. Maybe I am missing something windows-specific?

This is my CMakeLists.txt:

cmake_minimum_required(VERSION 3.4 FATAL_ERROR)
set(APP_NAME "HUD")
project(ScreenHUD)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

#SET_PROPERTY(GLOBAL PROPERTY USE_FOLDERS ON)

SET(ROS_ROOT "C://opt/ros/melodic/x64" CACHE PATH "ROS Root" FORCE)
SET(ROSDEPS_ROOT "C://opt/rosdeps/x64" CACHE PATH "ROSDEPS Root" FORCE)

include_directories("${ROS_ROOT}/include")
include_directories("${ROSDEPS_ROOT}/include")
include_directories("${ROS_CUSTOM_ROOT}/include")
include_directories("${ROSDEPS_ROOT}/include/boost-1_66")
include_directories("${ROS_ROOT}/lib")
include_directories("${ROS_ROOT}/bin")

link_directories("${ROS_ROOT}/lib")
link_directories("${ROS_ROOT}/bin")
link_directories("${ROSDEPS_ROOT}/lib")
link_directories("${ROSDEPS_ROOT}/bin")

set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)

add_executable(${APP_NAME}
src/main.cpp
)

# After include_directory and link_directory combination did not work, I tried this:
find_library(roscpp
    roscpp
    PATHS "${ROS_ROOT}/lib"
    PATH_SUFFIXES ".lib")
find_library(roscpp_serialization
    roscpp_serialization
    PATHS "${ROS_ROOT}/lib"
    PATH_SUFFIXES ".lib")
find_library(rosconsole
    rosconsole
    PATHS "${ROS_ROOT}/lib"
    PATH_SUFFIXES ".lib")
find_library(rostime
    rostime
    PATHS "${ROS_ROOT}/lib"
    PATH_SUFFIXES ".lib")
find_library(rosbag
    rosbag
    PATHS "${ROS_ROOT}/lib"
    PATH_SUFFIXES ".lib")

set(LINK_LIBRARY
    roscpp
    roscpp_serialization
    rosconsole
    rostime
    rosbag)

target_link_libraries(${APP_NAME}
    ${roscpp}
    ${roscpp_serialization}
    ${rosconsole}
    ${rostime}
    ${rosbag}
    #${LINK_LIBRARY} <-- using this instead of single libs as above resulted in the same problem
 )

My main.cpp looks like:

#include <ros/ros.h>

int main(int argc, char** argv)
{
    ros::init(argc, argv, "node_name");

    while (ros::ok())
    {
        sleep(1);
    }
    return 0;
}

When I build, the linker throws me an undefined reference to ros init error. This tells me that the library could not be linked. Using the message output of cmake, I could see that cmake finds the libraries but somehow cannot or does not link them.

Any hints on what might be the problem are very appreciated!

edit retag flag offensive close merge delete