使用 ROS 和 cmake 正确链接库

问题描述 投票:0回答:2

我正在尝试为带有 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

当我用gcc编译时,我还得到了“对‘phread_create’的未定义引用”,并通过在main.cpp之前调用-pthread解决了这个问题。 所以我现在的问题是,我怎样才能用 CMake 做同样的事情? 我进行了很多搜索并尝试了不同的解决方案,但到目前为止没有任何效果。当涉及到 ROS 和 CMake 时,我还很陌生,所以我不确定在哪里添加什么,或者我是否在 CMakeLists.txt 或 package.xml 中的某个地方写了错误的内容。

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

提前,谢谢。

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

设置(CMAKE_CXX_FLAGS“-lpthread”)

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

设置(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="[email protected]">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] 尝试使用@fenix688编写的CMakeLists.txt,它给出了与此输出相同的错误:

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
c++ linux cmake pthreads ros
2个回答
0
投票

嗯,我们需要调查您的问题。当你在 cmake 之后运行 make 时,你可以运行

make VERBOSE=1
以便我们可以看到它执行的实际命令以及 pthread 标志是否指定正确。您还可以参考这个帖子:Using CMake with GNU Make: How can I see the certain Commands?


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

希望它有效!

© www.soinside.com 2019 - 2024. All rights reserved.