0

I'm trying to compile a small project which has both BOOST and PETSc dependencies. I couldn't configure my top level CMakeList in a way to include and link these external libraries to my project. It seems that I can achieve this objective by configuring each CMakeList of each library / executable that I've built, however I'm looking for a more practical way to do so.

Is there any?

As requested, this is my top level CMakeList:

CMAKE_MINIMUM_REQUIRED(VERSION 3.2)
# WHERE THE FILES WILL BE
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/libs)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/shared_libs)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/apps) 
# BOOST MANAGEMENT
find_package(Boost)
if(Boost_FOUND)
    include_directories(${Boost_INCLUDE_DIR})
endif()
# COMPILER SETTINGS
set(CMAKE_C_COMPILER        "mpicc") 
set(CMAKE_CXX_COMPILER      "mpicxx") # both compilers are in the path
set(CMAKE_CXX_FLAGS_DEBUG   "${CMAKE_CXX_FLAGS_DEBUG} -Wall -Wextra -pedantic -std=c++11")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -pedantic -std=c++11")
# SET BUILD TYPE
set(CMAKE_BUILD_TYPE "RELEASE")
set(CMAKE_BUILD_DIR "Release")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}/${CMAKE_BUILD_DIR})
# ABOUT THE PROJECT
project(MinorTest)
set(VERSION_MAJOR "1")
set(VERSION_MINOR "0")
set(VERSION_PATCH "0")
set(VERSION "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}")
# ADD SUBDIRECTORIES
add_subdirectory(MuParserLib)
add_subdirectory(UtilsLib)
#add_subdirectory(UtilsTest)
add_subdirectory(ScriptSystemLib)
add_subdirectory(ScriptSystemTest)

The following errors are shown when linking the executable from ScriptSystemTest project:

/usr/lib/gcc/x86_64-pc-linux-gnu/6.3.1/../../../../lib/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
CMakeFiles/ScriptSystemTest.dir/source/TestScriptSystem.cpp.o: In function `init_unit_test_suite(int, char**)':
/home/felipe/Libraries/boost_1_63_0/boost/test/unit_test_suite.hpp:346: undefined reference to `boost::unit_test::framework::master_test_suite()'
CMakeFiles/ScriptSystemTest.dir/source/TestScriptSystem.cpp.o: In function `TestParser::test_method()':
/home/felipe/Workspace/Z_TestEFv/ScriptSystemTest/source/TestScriptSystem.cpp:20: undefined reference to `boost::unit_test::unit_test_log_t::set_checkpoint(boost::unit_test::basic_cstring<char const>, unsigned long, boost::unit_test::basic_cstring<char const>)'
/home/felipe/Workspace/Z_TestEFv/ScriptSystemTest/source/TestScriptSystem.cpp:20: undefined reference to `boost::test_tools::tt_detail::report_assertion(boost::test_tools::assertion_result const&, boost::unit_test::lazy_ostream const&, boost::unit_test::basic_cstring<char const>, unsigned long, boost::test_tools::tt_detail::tool_level, boost::test_tools::tt_detail::check_type, unsigned long, ...)'

Finally, this is the ScriptSystemTest CMakeList:

project(ScriptSystemTest)
#
include_directories(${CMAKE_SOURCE_DIR}/${PROJECT_NAME}/include)
include_directories(${CMAKE_SOURCE_DIR}/UtilsLib/include)
include_directories(${CMAKE_SOURCE_DIR}/ScriptSystemLib/include)
include_directories(${CMAKE_SOURCE_DIR}/MuParserLib/include)
#
link_directories(${CMAKE_SOURCE_DIR}/UtilsLib/source)
link_directories(${CMAKE_SOURCE_DIR}/ScriptSystemLib/source)
link_directories(${CMAKE_SOURCE_DIR}/MuParserLib/source)
# SEARCH FOR .CPP FILES
file(GLOB ${PROJECT_NAME}_sources ${CMAKE_SOURCE_DIR}/${PROJECT_NAME}/source/ *.cpp)
#
add_executable(${PROJECT_NAME} ${${PROJECT_NAME}_sources})
#
target_link_libraries(${PROJECT_NAME} UtilsLib)
target_link_libraries(${PROJECT_NAME} ScriptSystemLib)
target_link_libraries(${PROJECT_NAME} MuParserLib)
#
install(
    TARGETS ${PROJECT_NAME}
    RUNTIME DESTINATION apps
) 
  • `find_package()` is the key. Do that before the `add_subdirectory()`. Also make sure that you have exactly 1 `PROJECT()`. – drescherjm Jan 31 '17 at 20:17
  • Could you please add a [mcve]? If I understand your problem correctly, you should look at the [CMAKE_CXX_STANDARD_INCLUDE_DIRECTORIES](https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_STANDARD_INCLUDE_DIRECTORIES.html) and [CMAKE_CXX_STANDARD_LIBRARIES](https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_STANDARD_LIBRARIES.html) global variables (which are applied to all targets in your CMake project). – Florian Feb 01 '17 at 11:36
  • @drescherjm - Yes, I try to use find_package(). What do you mean by exactly one project? – Felipe Giacomelli Feb 01 '17 at 16:38
  • Only in the top level CMakeLists.txt you have a project(). Having project() in the other CMakeLists.txt resets some of your project variables instead of inheriting them from the top level CMakeLists.txt – drescherjm Feb 01 '17 at 17:40
  • @drescherjm - I see. However, in that case I would have to use find_package() in each project that depends on boost inside my main project. – Felipe Giacomelli Feb 02 '17 at 18:03

1 Answers1

1

Turning my comment into an answer

Extending CMAKE_CXX_STANDARD_LIBRARIES you can do:

find_package(Boost REQUIRED COMPONENTS unit_test_framework)

if(Boost_FOUND)
    include_directories(${Boost_INCLUDE_DIRS})
    list(APPEND CMAKE_CXX_STANDARD_LIBRARIES ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY_RELEASE})
endif()

add_subdirectory(ScriptSystemTest)

Or you go the long way of overwriting/extending add_executable() itself:

find_package(Boost REQUIRED COMPONENTS unit_test_framework)

macro(add_executable _target)
    _add_executable(${_target} ${ARGN})
    target_link_libraries(${_target} Boost::unit_test_framework)
endmacro()

add_subdirectory(ScriptSystemTest)
Florian
  • 39,996
  • 9
  • 133
  • 149