ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version
target_link_libraries(sojourner ${catkin_LIBRARIES})

In your current CMakeLists.txt at least, you're only linking against whatever catkin_LIBRARIES contains. That does not include the MRAA libraries (as they are not catkin packages), so that's where the undefined reference errors are coming from.

How to link to third party library ? and How do I link a library in cmake? (note that explicitly setting the linker path is discouraged) are related, but just to reiterate:

  1. install your dependency somewhere on your system. This could be in the 'normal' locations (ie: /usr/local/.., or perhaps even system wide, depending on whether you build this from source or install a package distributed via your system's package manager), your home directory or somewhere else.

  2. make your CMakeLists.txt able to find your dependencies: some distribution-provided packages (ie: .debs on Debian/Ubuntu) provide CMake compatible files that allow you to use find_package(PKG_NAME). If no CMake files are available, try PkgConfig. If none of this is available / works, you could hard-code the path to the include and library director(y)(ies), but that should really be used as a last resort only.

  3. finally, if you could use find_package(..), add the pkg_name_LIBRARIES and pkg_name_INCLUDE_DIRS variables to the relevant include_directories(..) and target_link_libraries(..) lines in your CMakeLists.txt. If you used PkgConfig, do the same, but the variable names might be slightly different. If you hard-coded things, I'd still recommend using variables with the same names that the find scripts would use, because that makes it easy to move to a find script in the future.

In your case, you'll want to make sure the directory containing the mraa.h file is on the include path, and that your sojourner binary is linked with the (I'm guessing here) mraa library. Note that the file will probably be called libmraa.so, but CMake expects only the proper name (ie: mraa).

Bonus points if, instead of hard-coding, you write a CMake find or config script for MRAA and use that.

PS: for some more information on the recommended ways for step 2, see How to do common tasks » Package format 2 (recommended) » C++ system library dependencies in the catkin documentation.

target_link_libraries(sojourner ${catkin_LIBRARIES})

In your current CMakeLists.txt at least, you're only linking against whatever catkin_LIBRARIES contains. That does contains, and don't have any of the MRAA paths added to the compiler's include path. catkin_LIBRARIES whill not include contain the MRAA libraries (as they are not catkin packages), and without the MRAA headers on your include path, the compiler will not know about any MRAA datastructures, functions, etc, so that's where the undefined referenceundeclared X errors are coming from.

How to link to third party library ? and How do I link a library in cmake? (note that explicitly setting the linker path is discouraged) are related, but just to reiterate:

  1. install your dependency somewhere on your system. This could be in the 'normal' locations (ie: /usr/local/.., or perhaps even system wide, depending on whether you build this from source or install a package distributed via your system's package manager), your home directory or somewhere else.

  2. make your CMakeLists.txt able to find your dependencies: some distribution-provided packages (ie: .debs on Debian/Ubuntu) provide CMake compatible files that allow you to use find_package(PKG_NAME). If no CMake files are available, try PkgConfig. If none of this is available / works, you could hard-code the path to the include and library director(y)(ies), but that should really be used as a last resort only.

  3. finally, if you could use find_package(..), add the pkg_name_LIBRARIES and pkg_name_INCLUDE_DIRS variables to the relevant include_directories(..) and target_link_libraries(..) lines in your CMakeLists.txt. If you used PkgConfig, do the same, but the variable names might be slightly different. If you hard-coded things, I'd still recommend using variables with the same names that the find scripts would use, because that makes it easy to move to a find script in the future.

In your case, you'll want to make sure the directory containing the mraa.h file is on the include path, and that your sojourner binary is linked with the (I'm guessing here) mraa library. Note that the file will probably be called libmraa.so, but CMake expects only the proper name (ie: mraa).

Bonus points if, instead of hard-coding, you write a CMake find or config script for MRAA and use that.

PS: for some more information on the recommended ways for step 2, see How to do common tasks » Package format 2 (recommended) » C++ system library dependencies in the catkin documentation.

target_link_libraries(sojourner ${catkin_LIBRARIES})

In your current CMakeLists.txt at least, you're only linking against whatever catkin_LIBRARIES contains, and don't have any of the MRAA paths added to the compiler's include path. catkin_LIBRARIES whill not contain the MRAA libraries (as they are not catkin packages), and without the MRAA headers on your include path, the compiler will not know about any MRAA datastructures, functions, etc, so that's where the undeclared X errors are coming from.

How to link to third party library ? and How do I link a library in cmake? (note that explicitly setting the linker path is discouraged) are related, but just to reiterate:

  1. install your dependency somewhere on your system. This could be in the 'normal' locations (ie: /usr/local/.., or perhaps even system wide, depending on whether you build this from source or install a package distributed via your system's package manager), your home directory or somewhere else.

  2. make your CMakeLists.txt able to find your dependencies: some distribution-provided packages (ie: .debs on Debian/Ubuntu) provide CMake compatible files that allow you to use find_package(PKG_NAME). If no CMake files are available, try PkgConfig. If none of this is available / works, you could hard-code the path to the include and library director(y)(ies), but that should really be used as a last resort only.

  3. finally, if you could use find_package(..), add the pkg_name_LIBRARIES and pkg_name_INCLUDE_DIRS variables to the relevant include_directories(..) and target_link_libraries(..) lines in your CMakeLists.txt. If you used PkgConfig, do the same, but the variable names might be slightly different. If you hard-coded things, I'd still recommend using variables with the same names that the find scripts would use, because that makes it easy to move to a find script in the future.

In your case, you'll want to make sure the directory containing the mraa.h file is on the include path, and that your sojourner binary is linked with the (I'm guessing here) mraa library. Note that the file will probably be called libmraa.so, but CMake expects only the proper name (ie: mraa).

Bonus points if, instead of hard-coding, you write a CMake find or config script for MRAA and use that.

PS: for some more information on the recommended ways for step 2, see How to do common tasks » Package format 2 (recommended) » C++ system library dependencies in the catkin documentation.


Edit:

I've tried gvdhoorn's suggestions and I still can't get it past [..]

For now, I wouldn't bother with doing things the 'proper' way. If all this is new to you, just do something like this in your CMakeLists.txt:

...

set (mraa_INCLUDE_DIRS "/path/to/where/mraa/headers/are")
set (mraa_LIBRARIES "/full/path/to/libmraa.so")

...

include_directories(${catkin_INCLUDE_DIRS} ${mraa_INCLUDE_DIRS})

...

add_executable(sojourner src/sojourner.cpp)
...
target_link_libraries(sojourner ${catkin_LIBRARIES} ${mraa_LIBRARIES})

...

I also wouldn't bother with adding anything to the catkin_package(..) statement for now. Get your binary to compile and link successfully. Afterwards, we can deal with the rest. Auto-detection was for bonus points :).

Your package.xml is also not important for now. You can't add the mraa stuff there anyway, as rosdep doesn't know about the MRAA at all (and CMakeLists.txt doesn't care about package.xml).

And btw: remember all of this is CMake. None of it is catkin specific.

That may sound pedantic and minor, but I'm just saying this because catkin is basically CMake, but with a bunch of extra convenience macros and functions (like catkin_package(..)). You'll probably have an easier time finding related questions (on StackExchange/Overflow fi) if you search for 'cmake' instead of 'catkin'.