Ask Your Question
0

Proper CMakeLists

asked 2019-04-04 06:32:11 -0500

EdwardNur gravatar image

It looks like I do not understand something in CMakelists. In my package, I have two C++ files, one called uart.cpp and another one called joint.cpp.

Joint uses uart functions and variables but only Join has rosnode and main function, so how do I make my CMakelists? I get that uart functions have undefined references and my CMakelists:

cmake_minimum_required(VERSION 2.8.3)
project(genius_control)

add_compile_options(-std=c++11)


find_package(catkin REQUIRED COMPONENTS
        roscpp
        serial
)

catkin_package(
   INCLUDE_DIRS
        include
   CATKIN_DEPENDS
        roscpp
   LIBRARIES
        genius_control
)


include_directories(
  include
  ${catkin_INCLUDE_DIRS}
)

install(DIRECTORY include/${PROJECT_NAME}/
        DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
        FILES_MATCHING PATTERN "*.h"
        PATTERN ".svn" EXCLUDE)

add_library(genius_control
        include/${PROJECT_NAME}/uart.h
        include/${PROJECT_NAME}/joint.h
        src/uart.cpp
        src/joint.cpp
        )

add_executable(joint src/joint.cpp)

target_link_libraries(genius_control ${catkin_LIBRARIES})
edit retag flag offensive close merge delete

Comments

3

Please note: this is not a Catkin or ROS related question. It's purely CMake.

gvdhoorn gravatar image gvdhoorn  ( 2019-04-04 07:35:46 -0500 )edit

1 Answer

Sort by » oldest newest most voted
0

answered 2019-04-04 07:24:57 -0500

kosmastsk gravatar image

updated 2019-04-04 09:19:35 -0500

You should change the add_executable to add_executable(joint src/joint.cpp src/uart.cpp) otherwise it cannot locate the implementation of the functions that the uart.cpp file includes. As for the add_library, it can include only the header files and not the *.cpp. As for the target_link_libraries(genius_control ${catkin_LIBRARIES}), you should change it to `target_link_libraries(joint ${catkin_LIBRARIES})

edit: The previous is a working, but not really proper way to solve this, as far as I know. A better way would be the following:

[..previous are ok]

catkin_package(
   INCLUDE_DIRS
        include
   CATKIN_DEPENDS
        roscpp
   LIBRARIES
        genius_control  
        uart
)

include_directories(
  include
  ${catkin_INCLUDE_DIRS}
)

install(DIRECTORY include/${PROJECT_NAME}/
        DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
        FILES_MATCHING PATTERN "*.h"
        PATTERN ".svn" EXCLUDE)

add_library(uart  
     src/uart.cpp  
)  
target_link_libraries(uart
  ${catkin_LIBRARIES}
)

add_executable(joint    
     src/joint.cpp
)  
target_link_libraries(joint   
    ${catkin_LIBRARIES}  
    uart  
)
edit flag offensive delete link more

Comments

I am getting this error

CMake Error: CMake can not determine linker language for target: genius_control
CMake Error: Cannot determine link language for target "genius_control".
EdwardNur gravatar image EdwardNur  ( 2019-04-04 08:16:39 -0500 )edit

I edited my answer, check if this works

kosmastsk gravatar image kosmastsk  ( 2019-04-04 08:18:01 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2019-04-04 06:32:11 -0500

Seen: 79 times

Last updated: Apr 04 '19