0

I have some issue with including sub-folders on a CMAKE project. Below in short the summary of the CMAKE tree that I have. The first layer works ok, but the second layer does not work properly throwing a linker error:

ERROR:

CMake Error at src/ROSTemplatesMsgs/CMakeLists.txt:17 (add_executable): Cannot find source file:

actionlib_msgs/goalID/dbGoalID.cpp

Tried extensions .c .C .c++ .cc .cpp .cxx .m .M .mm .h .hh .h++ .hm .hpp .hxx .in .txx

CMake Error: CMake can not determine linker language for target: goalID CMake Error: Cannot determine link language for target "goalID". Generating done

Project tree is shown below:

.
+-- Project-DriveAll[master]
+-- CMakeLists.txt
+-- src
|   +-- projectA
|   +-- projectB
|   +-- ROSTemplatesMsgs
|       +-- CMakeLists.txt
|       +-- main
|           +-- geometry_msgs.h
|           +-- main.cpp
|           +-- pointField.h
|           +-- ros_headers.h
|       +-- src
|           +-- actionlib_msgs
|               +-- goalID
|                   +-- dbGoalID.cpp
|                   +-- dbGoalID.h
|                   +-- goalIDItem.cpp
|                   +-- goalIDItem.h
|               +-- goalStatus
|                   +-- dbGoalStatus.cpp
|                   +-- dbGoalStatus.h
|                   +-- goalStatusItem.cpp
|                   +-- goalStatusItem.h
|           +-- sensor_msgs
|               +-- laserscan
|               ........

For a better visualization see also this:

viz

so the root file is below and this works well:

cmake_minimum_required(VERSION 3.1)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")

set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/lib)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
set(INCLUDE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/include)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99 -g") 
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -g")

project(Project-DriveAll)
add_subdirectory(src/projectA)
add_subdirectory(src/projectB)
add_subdirectory(src/ROSTemplatesMsgs)

However, below is the CMakeLists.txt under the project src/ROSTemplatesMsgs that is not compiling properly:

cmake_minimum_required (VERSION 3.1)
project(ROSTemplatesMsgs)
find_package(Qt5Widgets REQUIRED)

set (OpenCV_DIR /home/to/opencv/build)
set (BOOST_LIBRARYDIR /usr/lib/x86_64-linux-gnu)

find_package( OpenCV REQUIRED )
find_package(Boost COMPONENTS system thread filesystem REQUIRED)
find_package(Qt5PrintSupport REQUIRED)
find_package(Qt5 REQUIRED COMPONENTS Core Quick Sql)

include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)

set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)

include_directories(${OpenCV_INCLUDE_DIRS})
INCLUDE_DIRECTORIES( ${Boost_INCLUDE_DIR} )
INCLUDE_DIRECTORIES(${Qt5Widgets_INCLUDE_DIRS})

file(GLOB SRCS
    "src/*.h"
    "src/*.cpp"
    "src/*.hpp"

    "src/actionlib_msgs/goalID/dbGoalID.cpp"
    "src/actionlib_msgs/goalID/dbGoalID.h"
    "src/actionlib_msgs/goalID/dbGoalIDItem.cpp"
    "src/actionlib_msgs/goalID/dbGoalIDItem.h"

    "src/actionlib_msgs/goalStatusArray/dbGoalStatusArray.cpp"
    "src/actionlib_msgs/goalStatusArray/dbGoalStatusArray.h"
    "src/actionlib_msgs/goalStatusArray/goalStatusArrayItem.cpp"
    "src/actionlib_msgs/goalStatusArray/goalStatusArrayItem.h"


    "src/sensor_msgs/laserscan/*.h"
    "src/sensor_msgs/laserscan/*.cpp"
    "src/sensor_msgs/laserscan/*.hpp"

    "src/visualization_msgs/marker/*.h"
    "src/visualization_msgs/marker/*.cpp"
    "src/visualization_msgs/marker/*.hpp"

    "src/visualization_msgs/markerarray/*.h"
    "src/visualization_msgs/markerarray/*.cpp"
    "src/visualization_msgs/markerarray/*.hpp"

    "src/sensor_msgs/pointcloud2/*.h"
    "src/sensor_msgs/pointcloud2/*.cpp"
    "src/sensor_msgs/pointcloud2/*.hpp"

    "src/sensor_msgs/pointcloud/*.h"
    "src/sensor_msgs/pointcloud/*.cpp"
    "src/sensor_msgs/pointcloud/*.hpp"


    "main/*.h"
    "main/*.cpp"
    "main/*.hpp"
)

file(GLOB UI_SRCS
    "ui/*.h"
    "ui/*.cpp"
    "ui/*.hpp"
)

add_executable(ROSTemplatesMsgs main/main.cpp ${SRCS})
target_link_libraries (ROSTemplatesMsgs  Qt5::Widgets  ${Boost_LIBRARIES} ${OpenCV_LIBS} Qt5::PrintSupport Qt5::Core Qt5::Quick Qt5::Sql)

add_library(ROSTemplatesMsgs_lib SHARED ${SRCS} ${UI_HDRS} ${UI_SRCS})
target_include_directories (ROSTemplatesMsgs_lib PUBLIC "src/actionlib_msgs/" )
target_include_directories (ROSTemplatesMsgs_lib PUBLIC "src/sensor_msgs/" )
target_link_libraries (ROSTemplatesMsgs_lib Qt5::Widgets  ${Boost_LIBRARIES} ${OpenCV_LIBS} Qt5::PrintSupport Qt5::Core Qt5::Quick Qt5::Sql)

I consulted a lot of resources such as this one and this one however in my case I am not using the TARGET option as not always suggested in the CMAKE documentation. However I tried also to set_target_properties as suggested in this post but nothing changed. I went through this post but wasn't useful to find out what the problem was.

I also tried to add_subdirectory as advised on this post in the following way but that also didn't work:

add_subdirectory(src/actionlib_msgs)
add_subdirectory(src/sensor_msgs)

Another source I counsulted was this one and it actually helped understand the different use if dealing with a complex project and another that is not. My project has other subprojects as shown at the beginning of the question with multiple CMakeLists.txt but the project ROSTemplatesMsgs is the one that has multiple subfolders and that is complicating the tree.

The root has the add_subdirectory() command and that is working well in fact. ProjectA and ProjectB have their own CMakeLists.txt and no problem (but they also don't have multiple subfolders). When it comes to the last project (the one with subfolders) something does not work properly and I am running out of ideas.

EDITS

Additional information after building the project can be seen in the terminal error below:

In file included from /home/build-ultrasound_mapper-Desktop_Qt_5_12_2_GCC_64bit-Debug/src/ROSTemplatesMsgs/ROSTemplatesMsgs_autogen/mocs_compilation.cpp:3:0: /home/build-ultrasound_mapper-Desktop_Qt_5_12_2_GCC_64bit-Debug/src/ROSTemplatesMsgs/ROSTemplatesMsgs_autogen/WKFP3Z6SYI/moc_dbGoalStatus.cpp:57:6: error: ‘dbGoalStatus’ has not been declared void dbGoalStatus::qt_static_metacall(QObject *_o, QMetaObject::Call _c, int _id, void **_a)

/home/build-ultrasound_mapper-Desktop_Qt_5_12_2_GCC_64bit-Debug/src/ROSTemplatesMsgs/ROSTemplatesMsgs_autogen/WKFP3Z6SYI/moc_dbGoalStatus.cpp:65:38: error: ‘dbGoalStatus’ has not been declared QT_INIT_METAOBJECT const QMetaObject dbGoalStatus::staticMetaObject = { { ^~~~~~~~~~~~

/home/labrat/build-ultrasound_mapper-Desktop_Qt_5_12_2_GCC_64bit-Debug/src/ROSTemplatesMsgs/ROSTemplatesMsgs_autogen/WKFP3Z6SYI/moc_dbGoalStatus.cpp:75:20: error: ‘dbGoalStatus’ has not been declared const QMetaObject *dbGoalStatus::metaObject() const ^~~~~~~~~~~~

/home/build-ultrasound_mapper-Desktop_Qt_5_12_2_GCC_64bit-Debug/src/ROSTemplatesMsgs/ROSTemplatesMsgs_autogen/WKFP3Z6SYI/moc_dbGoalStatus.cpp:75:47: error: non-member function ‘const QMetaObject* metaObject()’ cannot have cv-qualifier const QMetaObject *dbGoalStatus::metaObject() const ^~~~~

which seems to point to a linker error.

Emanuele
  • 2,194
  • 6
  • 32
  • 71
  • When you say that "the first layer works ok", do you mean that you can build from the root project, but not from the `ROSTemplatesMsgs`? – JonasVautherin Mar 22 '21 at 22:14
  • The path in the error message - `actionlib_msgs/goalID/dbGoalID.cpp` - doesn't correspond to the path in the code `src/actionlib_msgs/goalID/dbGoalID.cpp`. Smells like you have some **other** project, or something wrong with the cache. – Tsyvarev Mar 22 '21 at 23:21
  • @JonasVautherin, thanks for stopping by and reading. Yes the root project works and cmake parses all the files. However when I try to build the project it stops right at the `ROSTemplatesMsgs` project, specifically when trying to build the subfolders. Which makes me think that I have something missing or not right. – Emanuele Mar 22 '21 at 23:29
  • @Tsyvarev, thanks for stopping by and reading, for the cache I erased it and rebuilt the project several times, I cleaned it and and run cmake from zero before rebuilding it. The project parses correctly but when it comes to build it it stops right at the ``ROSTemplatesMsgs` project that has the subfolders. Also I didn't totally understood what you mean of the path. Can you be a bit more clear please :) ? – Emanuele Mar 22 '21 at 23:33
  • If that is useful I added more detail on the error at terminal, you can see the **EDITS** in the question. Thank you guys for reading I appreciate your time. – Emanuele Mar 22 '21 at 23:39
  • Maybe you think I should add a `CMakeLists.txt` under each folder? Or maybe that is excessive? I think it is excessive but I am running out of ideas.. – Emanuele Mar 22 '21 at 23:42
  • No that's not necessary. What I don't get is if it fails when you run cmake from the root, or if you do something like `cd src/ROSTemplatesMsgs` and try to run cmake from there. Because you declare it as its own project (`project(ROSTemplatesMsgs)`), so I guess you may want to build it standalone. What is the command you run, and from where? – JonasVautherin Mar 23 '21 at 00:10
  • Yes I run it from the root. I am using `Qt5` as IDE and I am just pushing the build button which build the whole project so it will build "ProjectA", "ProjectB" and "ROSTemplatesMsgs". Each project has its own `CMakeLists.txt`. All other projects don't have subfolders. The "ROSTemplatesMsgs" one is the one with subfolders, which means that the `MakeLists.txt` referred to `ROSTemplatesMsgs` is not properly written, but I don't see what is wrong with that. – Emanuele Mar 23 '21 at 00:17
  • The error message started with `CMake Error` is the error emitted by **CMake**. Not sure why you refer to it as a "linker error": this is a **configuration** error, which comes before **building** the project, when files are compiled and linked. BTW, the location noted in that error - `src/ROSTemplatesMsgs/CMakeLists.txt:17 (add_executable)` - definitely does NOT corresponds to line `add_executable(ROSTemplatesMsgs main/main.cpp ${SRCS})` in the `CMakeLists.txt` which you show. Please, post only error messages **relevant** to the code you show in the post. – Tsyvarev Mar 23 '21 at 07:55
  • And please, do not use *blockquote* formatting for the error messages: this formatting doesn't preserves newlines, so messages become harder to read. Use **code** formatting instead. – Tsyvarev Mar 23 '21 at 07:56

0 Answers0