Question

I have been doing a project that has many classes (including cpp and header files) and one executable cpp that has int main. With ROS, I'm trying to link these with CMakeLists.txt and with the runtime, I'm planning to compile it without having to change the txt every time. Here is my CMakeLists.txt:

cmake_minimum_required(VERSION 2.4.6)
include($ENV{ROS_ROOT}/core/rosbuild/rosbuild.cmake)
rosbuild_init()
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)

rosbuild_add_library(${PROJECT_NAME} Im_Basibos.cpp)
rosbuild_add_library(${PROJECT_NAME} Im_HedefeGitme.cpp)
rosbuild_add_library(${PROJECT_NAME} Im_Konum.cpp)
rosbuild_add_library(${PROJECT_NAME} Im_Robot.cpp)
rosbuild_add_library(${PROJECT_NAME} Im_Sonar.cpp)

rosbuild_add_executable(srctest Im_RobotKontrol.cpp)

I dont know how to link the header files, I have to link these:

Im_Basibos.h, Im_Basibos.cpp
Im_HedefeGitme.h, Im_HedefeGitme.cpp
Im_Konum.h, Im_Konum.cpp
Im_Robot.h, Im_Robot.cpp
Im_Sonar.h, Im_Sonar.cpp

and

Im_Robot.cpp that has int main()

Any answer will be much appreciated. Thanks already..

Was it helpful?

Solution

I guess rosbuild_add_library works the same than add_library and is not meant to works the way you're using it. It's meant to create static or shared libraries, not to build object files.

I'm giving you two possible ways to build your executable.

version 1

If you only need to build an executable srctest and no separate library.

What you need to do is to list your source files in some variables, say srctest_SOURCES:

set(srctest_SOURCES Im_Basibos.cpp Im_HedefeGitme.cpp
                    Im_Konum.cpp Im_Robot.cpp Im_Sonar.cpp
                    Im_RobotKontrol.cpp)

Then build those sources into an executable:

add_executable(srctest ${srctest_SOURCES})

version 2

Now, if you really want to first build a library, say testlib then link it to your srctest executable, that can be done too:

set(testlib_SOURCES Im_Basibos.cpp Im_HedefeGitme.cpp
                    Im_Konum.cpp Im_Robot.cpp Im_Sonar.cpp)

add_library(testlib ${srctest_SOURCES})

add_executable(srctest Im_RobotKontrol.cpp)
target_link_libraries(srctest testlib)

OTHER TIPS

Thanks to Guillaume for the methods, Since I'm working with ROS environment, the exact commands that did the trick were:

rosbuild_add_library(${PROJECT_NAME} Im_Basibos.cpp)
rosbuild_add_library(${PROJECT_NAME} Im_HedefeGitme.cpp)
rosbuild_add_library(${PROJECT_NAME} Im_Konum.cpp)
rosbuild_add_library(${PROJECT_NAME} Im_Robot.cpp)
rosbuild_add_library(${PROJECT_NAME} Im_Sonar.cpp)

rosbuild_add_executable(srctest Im_RobotKontrol.cpp)

target_link_libraries(srctest ${PROJECT_NAME}) 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top