1

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.

user3314570
  • 237
  • 4
  • 14
  • If you use `A`, you need to link against the static library that project A produces. – StoryTeller - Unslander Monica Sep 05 '16 at 09:43
  • I didn't quite understand where to link it and how. In my CmakeList.txt? Isn't the `link_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../install/${CMAKE_BUILD_TYPE}/A/lib)` command already linking it? – user3314570 Sep 05 '16 at 09:45
  • No idea, is that where the static library is built and located? I also suggest you try to turn the two `link_directories` commands into one. That's what the [cmake documentation does](https://cmake.org/cmake/help/v3.0/command/link_directories.html). It may be that the second call is throwing away the first. – StoryTeller - Unslander Monica Sep 05 '16 at 09:51

1 Answers1

1

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)
Community
  • 1
  • 1
Antonio
  • 19,451
  • 13
  • 99
  • 197