CMake EXCLUDE_FROM_ALL argument ignored when running ‘make all’

This is not a bug. CMake does not include the foo lib in the all target directly. It does however add the foo target as dependency to TestProject and since TestProject is included in the all target, foo gets built too, if you build all.

This needs to be done this way. How else would you be able to link the foo function into TestProject?

If you want to use a lib that is not built as part of all, you need to use a imported lib. Doing this is and including the lib in the project seems a bit nonsensical though, but here you go.

add_library(foo STATIC EXCLUDE_FROM_ALL
    "src/foo.cpp"
)
cmake_minimum_required(VERSION 3.10)

project(TestProject
    LANGUAGES CXX
)

# Include the foo library
add_subdirectory("foo" EXCLUDE_FROM_ALL)

# Create the main executable
add_executable(TestProject
    "src/main.cpp"
)

# add copy of foo as imported lib
add_library(foo_imported STATIC IMPORTED)
target_include_directories(foo_imported INTERFACE $<TARGET_PROPERTY:foo,INTERFACE_INCLUDE_DIRECTORIES>)
set_target_properties(foo_imported PROPERTIES IMPORTED_LOCATION $<TARGET_FILE:foo>)


target_link_libraries(TestProject
    PRIVATE foo_imported
)

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top