# catkin_make unable to create executable & automatically copy .h files to devel

When I ran my catkin_make, I understand that it should automatically copy the header files (in this case mosquitto.h) which I included in the main cpp file into devel/include and create an executable, however, it is not doing so. devel/include doesn't even exist in my catkin_ws. My header file is in catkin_ws/src/package_name/include/package_name

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 devel/lib/mqtt_pub/mqtt_pub_node. If I create the devel/lib/mqtt_pub/mqtt_pub_node and put my header file in it, the catkin_make is successful, but the executable would not be created.

CMakeList.txt

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


package.xml

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


Would appreciate the guidance in solving this issue. I'm really clueless about the error, have research online and my cmake and xml file seems to be alright. Errors in my maincpp file have been rectified. Thanks!

edit retag close merge delete

Sort by » oldest newest most voted

First: this is not a ROS problem per se, but really a matter of using CMake correctly.

I understand that it should automatically copy the header files [..] which I included in the main cpp file into devel/include

No, that is not the case: header files will not be copied to the devel space by catkin (unless they are auto-generated, or there are statements in the CMakeLists.txt that do that). It's also not necessary: the dependents include path will automatically include the correct paths (in the src space) if the providing package properly exports those include locations.

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
[..]


Note that mqtt_pub_node doesn't exist. Why is it looking for something that doesn't exist? It should be automatically created.

yes, it should, but only if linking actually succeeded. Which it didn't.

From what I know, the executable should be in devel/lib/mqtt_pub, not sure where did the system think about devel/lib/mqtt_pub/mqtt_pub_node [..].

the error message you refer to simply states that while linking a binary called mqtt_pub_node (which will be placed in devel/lib/mqtt_pub if linking is successful), no library called mosquitto.h could be found. In your CMakeLists.txt, you defined the name of the binary as mqtt_pub_node with the add_executable(mqtt_pub_node ..) line.

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


Multiple things are not as they should be here:

1. mosquitto.h is not a library (obviously), it's a header. In your CMakeLists.txt you are asking CMake to link_libraries(..) against that header. That will obviously not work, resulting in the error that you are seeing. Note that this will never work, not even if search paths were setup correctly

2. location of your workspace is slghtly unorthodox (directly under /home). That is definitely legal, but perhaps placing it in your user's home directory (ie: /home/$USER) would be better 3. I doubt your catkin workspace is located at /catkin_ws, as that would be in the root of your file system. From the error messages, it should be at least /home/catkin_ws, but see my previous comment 4. it's typically recommended to place your package's include directories before anything else, as that allows you to provide files that override system includes 5. the use of link_directories(..) is typically not recommended with CMake. Prefer to use absolute paths to libraries, as that will avoid surprises if anyone ever overrides your linker's search path 6. your catkin_package(..) invocation states that your package depends on something called system_lib. That is probably not correct 7. in the same catkin_package(..) invocation a LIBRARY called mqtt_pub is exported, but that library is not build by your CMakeLists.txt (or at least, not in the snippet that you included) 8. nowhere ... more ## Comments Thank you for your prompt reply! Will try out your suggestions. Regarding point 2 & 3, opps sorry, I accidentally missed out when typing out the$User. Yes my catkin_ws is in home/$user/catkin_ws ( 2016-12-22 02:15:32 -0600 )edit 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 did not gvdhoorn suggested, but what he suggested was similar to what I did, he pointed out that I was missing the Mosquitto 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})

more

But that is basically what I wrote in my answer, is it not?

( 2016-12-23 02:58:53 -0600 )edit
1

And a comment: absolute paths are good in CMake. Hard-coding locations of files (libraries in this case) is not. I really recommend you use a find script.

( 2016-12-23 02:59:36 -0600 )edit

Yup, thank you so much! Have a great holiday! Alright, noted, will keep that in mind. Thanks again!

( 2016-12-26 19:20:25 -0600 )edit