Tuesday Coding Tip 33 — Imported CMake targets

●5 ●35 ●98
calendar_today ago • schedule2 min read
— Originally published at medium.com

Tuesday coding tips are super short posts about various tidbits mainly from C++, but also from other programming languages I use. You can also follow the #TuesdayCodingTips hashtag on Mastodon and Linkedin.


Handling dependencies with just CMake boils down to two approaches. Either you link a dependency as a CMake project, which makes it easy to use, but you have to build it yourself. Or, if your dependency doesn’t come with a well implemented FindPackage/Library helper, you link directly against prebuilt binaries, but that makes your CMake code messier and more complicated.

# dependencies.cmake
set ( DGM_FSM_VERSION "1.2.0" )
set ( DGM_FSM_URL "https://github.com/nerudaj/dgm-fsm-lib/releases/download/v${DGM_FSM_VERSION}/dgm-fsm-lib-${DGM_FSM_VERSION}-windows-vc17-x64.zip" )

include ( FetchContent )

FetchContent_Declare ( DGM_FSM
  URL "${DGM_FSM_URL}"
  DOWNLOAD_EXTRACT_TIMESTAMP TRUE
)

FetchContent_MakeAvailable ( DGM_FSM )

add_library ( Dep_fsm IMPORTED STATIC )

set_property(
  TARGET Dep_fsm
  APPEND PROPERTY
  # Restricts configurations to just Debug and Release,
  # removing MinSizeRel and RelWithDebInfo
  IMPORTED_CONFIGURATIONS DEBUG RELEASE
)

set_target_properties ( Dep_fsm PROPERTIES
  IMPORTED_LOCATION_DEBUG "${dgm_fsm_SOURCE_DIR}/lib/libfsm-d.lib"
  IMPORTED_LOCATION_RELEASE "${dgm_fsm_SOURCE_DIR}/lib/libfsm.lib"
  INTERFACE_INCLUDE_DIRECTORIES "${dgm_fsm_SOURCE_DIR}/include"
)

To alleviate some of that pain, you can use IMPORTED libraries. These allow you to create linkable targets for prebuilt binaries that you can use with a one simple call. Imported libraries don’t compile anything on themselves, but otherwise are usable as regular targets — link libraries, include directories or set properties to them.

# CMakeLists.txt
cmake_minimum_required ( VERSION 3.26 )

include ( dependencies.cmake )

project ( DEMO )

add_executable ( ${PROJECT_NAME}
  "${CMAKE_CURRENT_SOURCE_DIR}/src/Main.cpp"
)

# If this target was a library, anybody linking against
# it would automatically link Dep_fsm as well
target_link_libraries ( ${PROJECT_NAME}
  PUBLIC Dep_fsm
)

NOTE: The repo has moved since original posting of that article. Don't try to test the code verbatim.

If you’d like to know more about C++ dependency management in CMake, I compiled most approaches in this two-parter article:

Want more #cpp & #cpp deep dives?

Join 6,122+ developers sharing real benchmarks, code breakdowns, and projects. Free forever.

Part 33 of 33 in Tuesday Coding Tips

More Posts

Tuesday Coding Tip 06 - Explicit template instantiation

Jakub Neruda - Apr 7

Tuesday Coding Tip 02 - Template with type-specific API

Jakub Neruda - Mar 10

Tuesday Coding Tip 31 — Deducing this

Jakub Neruda - Nov 17

Tuesday Coding Tip 27 — Aliasing and ‘using’ in C++

Jakub Neruda - Oct 20

Tuesday Coding Tip 17 — Wrapping C APIs

Jakub Neruda - Jun 30
chevron_left
2.2k Points • 138 Badges
Brno, Czech Republic • linkedin.com/in/jakub-neruda
47Posts
40Comments
25Connections
Experienced C++ developer, team lead and hobby gamedev. I enjoy writing stuff about C++, clean code, clean architecture, and game development.

Commenters (This Week)

13 comments
2 comments
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!