kerrytazi
kerrytazi

Reputation: 583

How to build only one target for dependency?

I want to build an application under Windows using CMake + Visual Studio with a lot of dependencies, such as zlib. All of them are static libraries.

I've tried ADD_SUBDIRECTORY and this works pretty well but instead of building only depending target (zlibstatic) it builds all of them.

How to remove unused targets (with their solutions) or choose only one? Mainly I'm searching for feature to define only needed targets.

Part of my CMakeLists.txt:

ADD_SUBDIRECTORY("${CMAKE_CURRENT_SOURCE_DIR}/deps/zlib")
TARGET_INCLUDE_DIRECTORIES(MyProject PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/deps/zlib")
TARGET_LINK_LIBRARIES(MyProject zlibstatic)

Upvotes: 5

Views: 2072

Answers (2)

kerrytazi
kerrytazi

Reputation: 583

I finally figured out how to do it.

MyProject
├───build  <- here I call cmake
├───deps
│   └───zlib
│       └───CMakeLists.txt
├───inc
├───src
└───CMakeLists.txt
# Include project but remove all tartgets
ADD_SUBDIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/deps/zlib EXCLUDE_FROM_ALL)

# Use only specific one target
ADD_DEPENDENCIES(MyProject zlibstatic)

# Include dirs from zlib source directory and from output directory becuse it generates some headers
TARGET_INCLUDE_DIRECTORIES(MyProject PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}/deps/zlib
    ${CMAKE_CURRENT_BINARY_DIR}/deps/zlib
)

# Just to create beautiful structure in project tree
SET_PROPERTY(TARGET zlibstatic PROPERTY FOLDER Deps)

# Link after all
TARGET_LINK_LIBRARIES(MyProject zlibstatic)

Upvotes: 2

Damian
Damian

Reputation: 4651

I suggest you use vcpkg or conan instead to resolve your dependent library issue this is much cleaner and works well except for header only libraries.

You can of cause do that manually but than you loose the nice cmake setup.

Upvotes: 0

Related Questions