0

当我运行我的 catkin_make 时,我知道它应该自动将我包含在主 cpp 文件中的头文件复制到 devel 并创建一个可执行文件,但是它没有这样做。

错误:

Linking CXX executable /home/catkin_ws/devel/lib/mqtt_pub/mqtt_pub_node
/usr/bin/ld: cannot find -lmosquitto.h
collect2: error: ld returned 1 exit status
make[2]: *** [/home/catkin_ws/devel/lib/mqtt_pub/mqtt_pub_node] Error 1
make[1]: *** [mqtt_pub/CMakeFiles/mqtt_pub_node.dir/all] Error 2
make: *** [all] Error 2
Invoking "make -j1 -l1" failed

请注意,mqtt_pub_node 不存在。为什么它要寻找不存在的东西?它应该是自动创建的。据我所知,可执行文件应该在 devel/lib/mqtt_pub 中,不确定系统在哪里考虑 mqtt_pub_node(directory)。如果我创建 dir mqtt_pub_node 并将我的头文件放入其中,catkin_make 成功,但不会创建可执行文件。

[编辑] 头文件应该复制到 devel/include,但是在我的 catkin_ws 上,没有这样的目录。

制作清单

find_package(catkin REQUIRED COMPONENTS
  roscpp
  std_msgs
)

catkin_package(
  INCLUDE_DIRS include
  LIBRARIES mqtt_pub
  CATKIN_DEPENDS roscpp std_msgs
  DEPENDS system_lib
)

include_directories(
  ${catkin_INCLUDE_DIRS}
  /catkin_ws/src/mqtt_pub/include/mqtt_pub
  include
)

link_directories(
  /catkin_ws/src/mqtt_pub/include/mqtt_pub
)

link_libraries(
  mosquitto.h
)

add_executable(mqtt_pub_node src/mqtt_publish.cpp)
target_link_libraries(mqtt_pub_node ${catkin_LIBRARIES})

多谢指导,谢谢!

[编辑] cassinaj 给出的解决方案出错

CMakeFiles/mqtt_pub_node.dir/src/mqtt_publish.cpp.o: In function `main':
mqtt_publish.cpp:(.text+0x1f8): undefined reference to `mosquitto_lib_init'
mqtt_publish.cpp:(.text+0x210): undefined reference to `mosquitto_new'
mqtt_publish.cpp:(.text+0x237): undefined reference to   `mosquitto_username_pw_set'
mqtt_publish.cpp:(.text+0x259): undefined reference to `mosquitto_connect'
mqtt_publish.cpp:(.text+0x285): undefined reference to `mosquitto_loop_start'
mqtt_publish.cpp:(.text+0x2bc): undefined reference to `mosquitto_publish'
mqtt_publish.cpp:(.text+0x2d0): undefined reference to `mosquitto_loop_stop'
mqtt_publish.cpp:(.text+0x2df): undefined reference to `mosquitto_disconnect'
mqtt_publish.cpp:(.text+0x2ee): undefined reference to `mosquitto_destroy'
mqtt_publish.cpp:(.text+0x2f3): undefined reference to `mosquitto_lib_cleanup'
collect2: error: ld returned 1 exit status
make[2]: *** [/home/lorawan/catkin_ws/devel/lib/mqtt_pub/mqtt_pub_node] Error 1
make[1]: *** [mqtt_pub/CMakeFiles/mqtt_pub_node.dir/all] Error 2
make: *** [all] Error 2
Invoking "make -j1 -l1" failed
4

2 回答 2

0

解决了。使用 Mosquitto 时,我必须在我的 CMakeList 中链接客户端库。基本上是 libmosquitto.so 文件,它是客户端库。

我将以下内容添加到我的 cmake 列表中:

set(Mosquitto_libs
  /usr/lib/x86_64-linux-gnu/libmosquitto.so
  /usr/lib/x86_64-linux-gnu/libmosquitto.so.1
)
target_link_libraries(mqtt_pub_node ${catkin_LIBRARIES} ${Mosquitto_libs})
于 2016-12-23T08:18:11.757 回答
0

使用catkin,您通常不需要link_directories(...)也不需要link_libraries(mosquitto.h)导致您的问题。对于后者,您告诉 cmake 将所有库和可执行文件链接到名为 的库mosquitto.h,该库不是库,而只是头文件。尝试以下方法:

find_package(catkin REQUIRED COMPONENTS
  roscpp
  std_msgs
)

catkin_package(
  INCLUDE_DIRS include
  # LIBRARIES mqtt_pub
  CATKIN_DEPENDS roscpp std_msgs
)

include_directories(
  ${catkin_INCLUDE_DIRS}
  /catkin_ws/src/mqtt_pub/include/mqtt_pub
  include
)

add_executable(mqtt_pub_node src/mqtt_publish.cpp)
target_link_libraries(mqtt_pub_node ${catkin_LIBRARIES})

请注意,我注释掉了LIBRARIES mqtt_pub这一行,因为这要求您实际构建一个名为mqtt_pub.

于 2016-12-23T00:06:13.340 回答