使用ROS和cmake链接库是正确的

时间:2015-01-08 14:24:00

标签: c++ linux cmake pthreads ros

我试图为使用ROS的MOXA I / O以太网模块添加一些代码。我使用了一些示例代码,以确保它有效。我用gcc编译了代码,所以我知道代码有效。我用这一行从终端编译了它:

  

g ++ -L / usr / lib / x86_64-linux-gnu -pthread main.cpp -Wall -O3 -omain_test -L / usr / local / lib -lmxio_x64

我得到了main_test.out并且有效。

所以我按照ROS教程创建了一个catkin工作区和一个新包。我将我的代码添加到src文件夹,编辑CMakeList和package.xml。然后,当我尝试运行catkin_make -Wall(以消除大量警告)时,我收到以下消息:

Base path: /home/johau/ros_ws
Source space: /home/johau/ros_ws/src
Build space: /home/johau/ros_ws/build
Devel space: /home/johau/ros_ws/devel
Install space: /home/johau/ros_ws/install
####
#### Running command: "make cmake_check_build_system" in "/home/johau/ros_ws/build"
####
-- Using CATKIN_DEVEL_PREFIX: /home/johau/ros_ws/devel
-- Using CMAKE_PREFIX_PATH: /home/johau/ros_ws/devel;/opt/ros/indigo
-- This workspace overlays: /home/johau/ros_ws/devel;/opt/ros/indigo
-- Using PYTHON_EXECUTABLE: /usr/bin/python
-- Using Debian Python package layout
-- Using empy: /usr/bin/empy
-- Using CATKIN_ENABLE_TESTING: ON
-- Call enable_testing()
-- Using CATKIN_TEST_RESULTS_DIR: /home/johau/ros_ws/build/test_results
-- Found gtest sources under '/usr/src/gtest': gtests will be built
-- Using Python nosetests: /usr/bin/nosetests-2.7
-- catkin 0.6.9
-- BUILD_SHARED_LIBS is on
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- ~~  traversing 1 packages in topological order:
-- ~~  - master
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- +++ processing catkin package: 'master'
-- ==> add_subdirectory(master)
-- Boost version: 1.54.0
-- Found the following Boost libraries:
--   thread
-- Configuring done
-- Generating done
-- Build files have been written to: /home/johau/ros_ws/build
####
#### Running command: "make -Wall -j8 -l8" in "/home/johau/ros_ws/build"
####
Linking CXX executable /home/johau/ros_ws/devel/lib/master/main
CMakeFiles/main.dir/src/main.cpp.o: In function `main':
main.cpp:(.text+0xdd0): warning: the `gets' function is dangerous and should not be used.
/usr/local/lib/libmxio_x64.so: undefined reference to `pthread_create'
collect2: error: ld returned 1 exit status
make[2]: *** [/home/johau/ros_ws/devel/lib/master/main] Error 1
make[1]: *** [master/CMakeFiles/main.dir/all] Error 2
make: *** [all] Error 2
Invoking "make" failed

我还得到了"未定义的对'phread_create'""当我用gcc编译时,通过在main.cpp之前调用-pthread解决了这个问题。 所以现在我的问题是,我怎么能用CMake做同样的事情? 我经常搜索并尝试不同的解决方案,但到目前为止还没有任何工作。我很绿,当谈到ROS和CMake时,所以我不确定要添加到哪里,或者我在CMakeLists.txt或package.xml中的某处写错了。

我正在使用Ubuntu 14.04 LTS和ROS Indigo。 如果您需要更多信息,请告知我们。

事先,谢谢。

[编辑1] 我现在尝试添加

  

设置(CMAKE_CXX_FLAGS" -lpthread")

在CMakeLists.txt中的项目(主)之后。 我也尝试过:

  

设置(CMAKE_CXX_FLAGS" -L / usr / lib / x86_64-linux-gnu -pthread")

set(CMAKE_CXX_FLAGS "-L/usr/lib/x86_64-linux-gnu")
set(CMAKE_CXX_FLAGS "-pthread")

但它给出了同样的错误信息。

我的CMakeLists.txt:

cmake_minimum_required(VERSION 2.8.3)
project(master)

unset(MOXA_LIBRARY CACHE)

find_library(
   MOXA_LIBRARY
   NAMES mxio_x64
   PATHS /usr/local/lib
   PATH_SUFFIXES lib
   NO_DEFAULT_PATH
)

if(MOXA_LIBRARY STREQUAL "MOXA_LIBRARY-NOTFOUND")
    message(WARNING "Moxa Library not present !")
else()
    add_definitions(-DUSE_MOXA)
endif()

find_package(catkin REQUIRED COMPONENTS
  roscpp
  std_msgs
)

## System dependencies are found with CMake's conventions
 find_package(Boost REQUIRED COMPONENTS thread)

###################################
## catkin specific configuration ##
###################################

catkin_package(
   CATKIN_DEPENDS roscpp std_msgs
)

###########
## Build ##
###########

## Specify additional locations of header files
## Your package locations should be listed before other locations
# include_directories(include)
include_directories(
  ${catkin_INCLUDE_DIRS}
  /usr/lib/x86_64-linux-gnu/
)

## Declare a cpp executable
 add_executable(main
     src/main.cpp)

## Specify libraries to link a library or executable target against
 target_link_libraries(main
   ${MOXA_LIBRARY}
   ${catkin_LIBRARIES}
   ${CMAKE_THREAD_LIBS_INIT}
 )

我的package.xml:

<?xml version="1.0"?>
<package>
  <name>master</name>
  <version>0.0.0</version>
  <description>The master package</description>

  <maintainer email="johau@todo.todo">johau</maintainer>

  <license>BSD</license>

  <buildtool_depend>catkin</buildtool_depend>
  <build_depend>roscpp</build_depend>
  <build_depend>std_msgs</build_depend>
  <run_depend>roscpp</run_depend>
  <run_depend>std_msgs</run_depend>


  <!-- The export tag contains other, unspecified, tags -->
  <export>
    <!-- You can specify that this package is a metapackage here: -->
    <!-- <metapackage/> -->

    <!-- Other tools can request additional information be placed here -->

  </export>
</package>

[编辑2] 在@Lu-Niu请求之后我运行了catkin_make VERBOSE=1(catkin_make应该和make命令一样,据我所知,catkin部分只是因为它在ROS中)并且输出是在下面的方框中。到目前为止,正如我所看到的那样,指定了-pthread,而我认为它应该是-lpthread,当目录没有首先指定时(如果我错了,请纠正我)。那么我应该编辑什么来改变命令的顺序呢?

Base path: /home/johau/ros_ws
Source space: /home/johau/ros_ws/src
Build space: /home/johau/ros_ws/build
Devel space: /home/johau/ros_ws/devel
Install space: /home/johau/ros_ws/install
####
#### Running command: "make cmake_check_build_system" in "/home/johau/ros_ws/build"
####
####
#### Running command: "make VERBOSE=1 -j8 -l8" in "/home/johau/ros_ws/build"
####
/usr/bin/cmake -H/home/johau/ros_ws/src -B/home/johau/ros_ws/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /home/johau/ros_ws/build/CMakeFiles /home/johau/ros_ws/build/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory `/home/johau/ros_ws/build'
make -f master/CMakeFiles/main.dir/build.make master/CMakeFiles/main.dir/depend
make[2]: Entering directory `/home/johau/ros_ws/build'
cd /home/johau/ros_ws/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/johau/ros_ws/src /home/johau/ros_ws/src/master /home/johau/ros_ws/build /home/johau/ros_ws/build/master /home/johau/ros_ws/build/master/CMakeFiles/main.dir/DependInfo.cmake --color=
make[2]: Leaving directory `/home/johau/ros_ws/build'
make -f master/CMakeFiles/main.dir/build.make master/CMakeFiles/main.dir/build
make[2]: Entering directory `/home/johau/ros_ws/build'
Linking CXX executable /home/johau/ros_ws/devel/lib/master/main
cd /home/johau/ros_ws/build/master && /usr/bin/cmake -E cmake_link_script CMakeFiles/main.dir/link.txt --verbose=1
/usr/bin/c++   -lpthread    CMakeFiles/main.dir/src/main.cpp.o  -o /home/johau/ros_ws/devel/lib/master/main  -L/usr/local/lib -rdynamic -lmxio_x64 /opt/ros/indigo/lib/libroscpp.so -lboost_signals -lboost_filesystem /opt/ros/indigo/lib/librosconsole.so /opt/ros/indigo/lib/librosconsole_log4cxx.so /opt/ros/indigo/lib/librosconsole_backend_interface.so -llog4cxx -lboost_regex /opt/ros/indigo/lib/libxmlrpcpp.so /opt/ros/indigo/lib/libroscpp_serialization.so /opt/ros/indigo/lib/librostime.so -lboost_date_time /opt/ros/indigo/lib/libcpp_common.so -lboost_system -lboost_thread -lpthread -lconsole_bridge -Wl,-rpath,/usr/local/lib:/opt/ros/indigo/lib 
CMakeFiles/main.dir/src/main.cpp.o: In function `main':
main.cpp:(.text+0xdd0): warning: the `gets' function is dangerous and should not be used.
/usr/local/lib/libmxio_x64.so: undefined reference to `pthread_create'
collect2: error: ld returned 1 exit status
make[2]: *** [/home/johau/ros_ws/devel/lib/master/main] Error 1
make[2]: Leaving directory `/home/johau/ros_ws/build'
make[1]: *** [master/CMakeFiles/main.dir/all] Error 2
make[1]: Leaving directory `/home/johau/ros_ws/build'
make: *** [all] Error 2
Invoking "make" failed

[编辑3] @ fenix688的两个建议都给出了这个输出(VERBOSE = 1):

Base path: /home/johau/ros_ws
Source space: /home/johau/ros_ws/src
Build space: /home/johau/ros_ws/build
Devel space: /home/johau/ros_ws/devel
Install space: /home/johau/ros_ws/install
####
#### Running command: "make cmake_check_build_system" in "/home/johau/ros_ws/build"
####
#### Running command: "make VERBOSE=1 -j8 -l8" in "/home/johau/ros_ws/build"
####
/usr/bin/cmake -H/home/johau/ros_ws/src -B/home/johau/ros_ws/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /home/johau/ros_ws/build/CMakeFiles /home/johau/ros_ws/build/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory `/home/johau/ros_ws/build'
make -f master/CMakeFiles/main.dir/build.make master/CMakeFiles/main.dir/depend
make[2]: Entering directory `/home/johau/ros_ws/build'
cd /home/johau/ros_ws/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/johau/ros_ws/src /home/johau/ros_ws/src/master /home/johau/ros_ws/build /home/johau/ros_ws/build/master /home/johau/ros_ws/build/master/CMakeFiles/main.dir/DependInfo.cmake --color=
make[2]: Leaving directory `/home/johau/ros_ws/build'
make -f master/CMakeFiles/main.dir/build.make master/CMakeFiles/main.dir/build
make[2]: Entering directory `/home/johau/ros_ws/build'
Linking CXX executable /home/johau/ros_ws/devel/lib/master/main
cd /home/johau/ros_ws/build/master && /usr/bin/cmake -E cmake_link_script CMakeFiles/main.dir/link.txt --verbose=1
/usr/bin/c++       CMakeFiles/main.dir/src/main.cpp.o  -o /home/johau/ros_ws/devel/lib/master/main  -L/usr/local/lib -rdynamic -lpthread -lboost_thread -lpthread -lmxio_x64 /opt/ros/indigo/lib/libroscpp.so -lboost_signals -lboost_filesystem /opt/ros/indigo/lib/librosconsole.so /opt/ros/indigo/lib/librosconsole_log4cxx.so /opt/ros/indigo/lib/librosconsole_backend_interface.so -llog4cxx -lboost_regex /opt/ros/indigo/lib/libxmlrpcpp.so /opt/ros/indigo/lib/libroscpp_serialization.so /opt/ros/indigo/lib/librostime.so -lboost_date_time /opt/ros/indigo/lib/libcpp_common.so -lboost_system -lboost_thread -lpthread -lconsole_bridge -lmxio_x64 /opt/ros/indigo/lib/libroscpp.so -lboost_signals -lboost_filesystem /opt/ros/indigo/lib/librosconsole.so /opt/ros/indigo/lib/librosconsole_log4cxx.so /opt/ros/indigo/lib/librosconsole_backend_interface.so -llog4cxx -lboost_regex /opt/ros/indigo/lib/libxmlrpcpp.so /opt/ros/indigo/lib/libroscpp_serialization.so /opt/ros/indigo/lib/librostime.so -lboost_date_time /opt/ros/indigo/lib/libcpp_common.so -lboost_system -lconsole_bridge -Wl,-rpath,/usr/local/lib:/opt/ros/indigo/lib 
CMakeFiles/main.dir/src/main.cpp.o: In function `main':
main.cpp:(.text+0xdd0): warning: the `gets' function is dangerous and should not be used.
/usr/local/lib/libmxio_x64.so: undefined reference to `pthread_create'
collect2: error: ld returned 1 exit status
make[2]: *** [/home/johau/ros_ws/devel/lib/master/main] Error 1
make[2]: Leaving directory `/home/johau/ros_ws/build'
make[1]: *** [master/CMakeFiles/main.dir/all] Error 2
make[1]: Leaving directory `/home/johau/ros_ws/build'
make: *** [all] Error 2
Invoking "make" failed

如果我使用CMakeLists.txt中的pthread删除所有内容,我是否会获得此VERBOSE = 1输出,其中-lpthread仍然设置。

Base path: /home/johau/ros_ws
Source space: /home/johau/ros_ws/src
Build space: /home/johau/ros_ws/build
Devel space: /home/johau/ros_ws/devel
Install space: /home/johau/ros_ws/install
####
#### Running command: "make cmake_check_build_system" in "/home/johau/ros_ws/build"
####
####
#### Running command: "make VERBOSE=1 -j8 -l8" in "/home/johau/ros_ws/build"
####
/usr/bin/cmake -H/home/johau/ros_ws/src -B/home/johau/ros_ws/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /home/johau/ros_ws/build/CMakeFiles /home/johau/ros_ws/build/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory `/home/johau/ros_ws/build'
make -f master/CMakeFiles/main.dir/build.make master/CMakeFiles/main.dir/depend
make[2]: Entering directory `/home/johau/ros_ws/build'
cd /home/johau/ros_ws/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/johau/ros_ws/src /home/johau/ros_ws/src/master /home/johau/ros_ws/build /home/johau/ros_ws/build/master /home/johau/ros_ws/build/master/CMakeFiles/main.dir/DependInfo.cmake --color=
make[2]: Leaving directory `/home/johau/ros_ws/build'
make -f master/CMakeFiles/main.dir/build.make master/CMakeFiles/main.dir/build
make[2]: Entering directory `/home/johau/ros_ws/build'
Linking CXX executable /home/johau/ros_ws/devel/lib/master/main
cd /home/johau/ros_ws/build/master && /usr/bin/cmake -E cmake_link_script CMakeFiles/main.dir/link.txt --verbose=1
/usr/bin/c++       CMakeFiles/main.dir/src/main.cpp.o  -o /home/johau/ros_ws/devel/lib/master/main  -L/usr/local/lib -rdynamic -lmxio_x64 /opt/ros/indigo/lib/libroscpp.so -lboost_signals -lboost_filesystem /opt/ros/indigo/lib/librosconsole.so /opt/ros/indigo/lib/librosconsole_log4cxx.so /opt/ros/indigo/lib/librosconsole_backend_interface.so -llog4cxx -lboost_regex /opt/ros/indigo/lib/libxmlrpcpp.so /opt/ros/indigo/lib/libroscpp_serialization.so /opt/ros/indigo/lib/librostime.so -lboost_date_time /opt/ros/indigo/lib/libcpp_common.so -lboost_system -lboost_thread -lpthread -lconsole_bridge -Wl,-rpath,/usr/local/lib:/opt/ros/indigo/lib 
CMakeFiles/main.dir/src/main.cpp.o: In function `main':
main.cpp:(.text+0xdd0): warning: the `gets' function is dangerous and should not be used.
/usr/local/lib/libmxio_x64.so: undefined reference to `pthread_create'
collect2: error: ld returned 1 exit status
make[2]: *** [/home/johau/ros_ws/devel/lib/master/main] Error 1
make[2]: Leaving directory `/home/johau/ros_ws/build'
make[1]: *** [master/CMakeFiles/main.dir/all] Error 2
make[1]: Leaving directory `/home/johau/ros_ws/build'
make: *** [all] Error 2
Invoking "make" failed

[编辑4] 尝试使用CMakeLists.txt @ fenix688编写,它在此输出中给出了同样的错误:

Base path: /home/johau/ros_ws
Source space: /home/johau/ros_ws/src
Build space: /home/johau/ros_ws/build
Devel space: /home/johau/ros_ws/devel
Install space: /home/johau/ros_ws/install
####
#### Running command: "make cmake_check_build_system" in "/home/johau/ros_ws/build"
####
####
#### Running command: "make VERBOSE=1 -j8 -l8" in "/home/johau/ros_ws/build"
####
/usr/bin/cmake -H/home/johau/ros_ws/src -B/home/johau/ros_ws/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /home/johau/ros_ws/build/CMakeFiles /home/johau/ros_ws/build/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory `/home/johau/ros_ws/build'
make -f master/CMakeFiles/main.dir/build.make master/CMakeFiles/main.dir/depend
make[2]: Entering directory `/home/johau/ros_ws/build'
cd /home/johau/ros_ws/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/johau/ros_ws/src /home/johau/ros_ws/src/master /home/johau/ros_ws/build /home/johau/ros_ws/build/master /home/johau/ros_ws/build/master/CMakeFiles/main.dir/DependInfo.cmake --color=
make[2]: Leaving directory `/home/johau/ros_ws/build'
make -f master/CMakeFiles/main.dir/build.make master/CMakeFiles/main.dir/build
make[2]: Entering directory `/home/johau/ros_ws/build'
Linking CXX executable /home/johau/ros_ws/devel/lib/master/main
cd /home/johau/ros_ws/build/master && /usr/bin/cmake -E cmake_link_script CMakeFiles/main.dir/link.txt --verbose=1
/usr/bin/c++       CMakeFiles/main.dir/src/main.cpp.o  -o /home/johau/ros_ws/devel/lib/master/main  -L/usr/local/lib -rdynamic -lpthread -lmxio_x64 /opt/ros/indigo/lib/libroscpp.so -lboost_signals -lboost_filesystem /opt/ros/indigo/lib/librosconsole.so /opt/ros/indigo/lib/librosconsole_log4cxx.so /opt/ros/indigo/lib/librosconsole_backend_interface.so -llog4cxx -lboost_regex /opt/ros/indigo/lib/libxmlrpcpp.so /opt/ros/indigo/lib/libroscpp_serialization.so /opt/ros/indigo/lib/librostime.so -lboost_date_time /opt/ros/indigo/lib/libcpp_common.so -lboost_system -lboost_thread -lpthread -lconsole_bridge -lboost_thread -lpthread -lconsole_bridge -Wl,-rpath,/usr/local/lib:/opt/ros/indigo/lib 
CMakeFiles/main.dir/src/main.cpp.o: In function `main':
main.cpp:(.text+0xdd0): warning: the `gets' function is dangerous and should not be used.
/usr/local/lib/libmxio_x64.so: undefined reference to `pthread_create'
collect2: error: ld returned 1 exit status
make[2]: *** [/home/johau/ros_ws/devel/lib/master/main] Error 1
make[2]: Leaving directory `/home/johau/ros_ws/build'
make[1]: *** [master/CMakeFiles/main.dir/all] Error 2
make[1]: Leaving directory `/home/johau/ros_ws/build'
make: *** [all] Error 2
Invoking "make" failed

2 个答案:

答案 0 :(得分:0)

好吧,我们需要调查您的问题。当您在cmake之后运行make时,是否可以运行make VERBOSE=1以便我们可以看到它执行的实际命令以及是否正确指定了pthread标志。你也可以参考这个帖子:Using CMake with GNU Make: How can I see the exact commands?

答案 1 :(得分:0)

您可以尝试这个完整的 CMakeLists.txt 代码:

cmake_minimum_required(VERSION 2.8.3)
project(master)

unset(MOXA_LIBRARY CACHE)

find_library(
   MOXA_LIBRARY
   NAMES mxio_x64
   PATHS /usr/local/lib
   PATH_SUFFIXES lib
   NO_DEFAULT_PATH
)

if(MOXA_LIBRARY STREQUAL "MOXA_LIBRARY-NOTFOUND")
    message(WARNING "Moxa Library not present !")
else()
    add_definitions(-DUSE_MOXA)
endif()

find_package(catkin REQUIRED COMPONENTS
  roscpp
  std_msgs
)

## System dependencies are found with CMake's conventions
find_package(Boost COMPONENTS thread REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})

###################################
## catkin specific configuration ##
###################################

catkin_package(
   CATKIN_DEPENDS roscpp std_msgs
)

include_directories(${catkin_INCLUDE_DIRS})

find_package(Threads REQUIRED)

## Declare a cpp executable
add_executable(main src/main.cpp)

## Specify libraries to link a library or executable target against
target_link_libraries (main    ${CMAKE_THREAD_LIBS_INIT}
                               ${MOXA_LIBRARY}
                               ${catkin_LIBRARIES}
                               ${Boost_LIBRARIES})

我希望它有效!