catkin_make unable to create executable and automatically copy header files to devel

1.4k views Asked by At

when I ran my catkin_make, I understand that it should automatically copy the header files which I included in the main cpp file into devel and create an executable, however, it is not doing so.

The error:

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

Note that mqtt_pub_node doesn't exist. Why is it looking for something that doesn't exist? It should be automatically created. From what I know, the executable should be in devel/lib/mqtt_pub, not sure where did the system think about mqtt_pub_node(directory). If I create the dir mqtt_pub_node and put my header file in it, the catkin_make is successful, but the executable would not be created.

[EDIT] The header files should be copied into devel/include, but on my catkin_ws, there is no such directory.

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

Would appreciate the guidance, thanks!

[EDIT] Error from the solution given by 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
2

There are 2 answers

2
cassinaj On

With catkin you typically don't need link_directories(...) nor do you need link_libraries(mosquitto.h) which is causing your issue. With the latter one you are telling cmake to link all libraries and executables to a library named mosquitto.h, which is not a library but only a head file. Try out the following:

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

Note that i commented out LIBRARIES mqtt_pub line because this requires that you are actually building a library named mqtt_pub.

1
cechsterRK On

Solved. When using Mosquitto, I had to link the client library in my CMakeList. Basically the libmosquitto.so file, which is the client library.

I added the following to my cmake list:

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