user3314570 user3314570 - 2 months ago 8
C++ Question

Creating an executable encapsulating 2 other projects

I'm working on Windows in Eclipse C++. I have this structure in my project:

subProject A
-> creates a static library (works fine)
subProject B encapsulating subproject A
-> creates a static library (works fine)
subProject TestA
-> creates an executable (works fine)
subProject TestB
|dev
|src
| CmakeList.txt
|CmakeList.txt
-> creates an executable (doesn't work)


This is the CmakeList.txt in the dev directory for the TestB executable:

set(${PROJECT_NAME}_INCLUDE_DIRS
$(CMAKE_CURRENT_SOURCE_DIR}/src
CACHE STRING "" FORCE)

include_directories(${${PROJECT_NAME}_INCLUDE_DIRS})

file(GLOB_RECURSE ${PROJECT_NAME}_HEADER_FILES FOLLOW_SYMLINKS
./*.hpp
./*.h)

file(GLOB_RECURSE ${PROJECT_NAME}_SORUCE_FILES FOLLOW_SYMLINKS
./*.cpp
./*.c)

set(${PROJECT_NAME}_FILES ${${PROJECT_NAME}_HEADER_FILES}
${${PROJECT_NAME}_SOURCE_FILES})

# Additional includes and link directories and dependencies
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../install/${CMAKE_BUILD_TYPE}/A/include)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../install/${CMAKE_BUILD_TYPE}/B/include)

if(${CMAKE_BUILD_TYPE} STREQUAL "Release")
set(${PROJECT_NAME}_EXTRA_LIBRARIES ${${PROJECT_NAME}_EXTRA_LIBRARIES}
A
B
pthread)
elseif(${CMAKE_BUILD_TYPE} STREQUAL "Debug")
set(${PROJECT_NAME}_EXTRA_LIBRARIES ${${PROJECT_NAME}_EXTRA_LIBRARIES}
A
B
pthread)
endif()

# Add a library to the project
link_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../install/${CMAKE_BUILD_TYPE}/A/lib)
link_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../install/${CMAKE_BUILD_TYPE}/B/lib)

add_executable(${PROJECT_NAME} ${${PROJECT_NAME}_FILES})

target_link_libraries(${PROJECT_NAME} ${${PROJECT_NAME}_EXTRA_LIBRARIES})

install(FILES ${${PROJECT_NAME}_HEADER_FILES}
DESTINATION ${CMAKE_INSTALL_PREFIX}/include/
PERSMISSIONS OWNER_READ GROUP_READ WORLD_READ)

install(TARGETS ${PROJECT_NAME}
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
RUNTIME DESTINATION bin)


In my B subproject, in a file, I have this:

#include "A.hpp"

...
A algo;
A.method(parameters)


When I build my TestB, I have this weird error appearing in the B project on the 2 lines above which makes my build fail:

undefined reference to A::A()
undefined reference to A::method(parameter)


When I comment these lines and rebuild the B project and the TestB project it works fine, I have a library in the B project and a .exe in the TestB.

I don't understand where is my problem.

Answer

B depends on A, so add the dependency in B's CMakeLists.txt to A with target_link_libraries. Even if it's a static library and there won't be any actual linking when you build B, this will anyway give a hint to CMake that every time you link B to an executable, you have also have to link afterwards to A. A has to come after B in the link command line, so that the symbols needed by B are known when you get to link to A. See here for more details.

If you are looking for a quick fix instead, just change:

set(${PROJECT_NAME}_EXTRA_LIBRARIES ${${PROJECT_NAME}_EXTRA_LIBRARIES}
                     A
                     B
                     pthread)

to this

set(${PROJECT_NAME}_EXTRA_LIBRARIES ${${PROJECT_NAME}_EXTRA_LIBRARIES}
                     B
                     A
                     pthread)
Comments