当我运行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(目录)。如果我创建dir mqtt_pub_node并将我的头文件放入其中,则catkin_make成功,但不会创建可执行文件。
[编辑]头文件应该复制到devel / include中,但是在我的catkin_ws上,没有这样的目录。
Cmakelist
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
答案 0 :(得分: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
的库。
答案 1 :(得分: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})