Targets Linking and Visibility PUBLIC PRIVATE
Define targets, link libraries, and use PUBLIC, PRIVATE, INTERFACE visibility correctly.
Targets Linking and Visibility PUBLIC PRIVATE is a free C++ Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, and your progress syncs across the web and the CoddyKit app. The C++ Academy course includes 4 lessons in total.
Targets are the Unit of Build
Modern CMake centers on targets — executables, libraries, or interface-only sets. Properties attach to targets and propagate via linkage.
Adding Targets
Use add_executable, add_library, or add_library(... INTERFACE).
add_executable(myapp src/main.cpp)
add_library(mylib STATIC src/lib.cpp)
add_library(headers INTERFACE)target_link_libraries
Link a target to libraries. The third argument controls visibility.
target_link_libraries(myapp PRIVATE mylib)PUBLIC PRIVATE INTERFACE Visibility
Three visibility levels — they control whether dependencies propagate to consumers of this target:
- PRIVATE — used to build this target; not exposed
- INTERFACE — used by consumers; not by this target
- PUBLIC — both
Visualizing Propagation
If mylib links fmt as PUBLIC, then any target linking mylib also gets fmt s include directories. PRIVATE means consumers do not see fmt.
target_include_directories
Set include directories per target. Use PUBLIC for headers consumers will #include; PRIVATE for internal headers.
target_include_directories(mylib
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src
)target_compile_definitions
Set preprocessor macros for the target.
target_compile_definitions(mylib PUBLIC MYLIB_VERSION_MAJOR=1)target_compile_features
Request specific language features (e.g., a C++ standard).
target_compile_features(mylib PUBLIC cxx_std_20)Interface Libraries
An INTERFACE library has no compiled output. Use it to bundle compile options, headers, or linkages that consumers should inherit.
add_library(strict_warnings INTERFACE)
target_compile_options(strict_warnings INTERFACE -Wall -Wextra)
# now any target can opt in
target_link_libraries(myapp PRIVATE strict_warnings)Header-Only Libraries
For header-only libraries, declare an INTERFACE library and set the include directory as INTERFACE.
Aliases
Create namespaced aliases for clearer linkage commands.
add_library(MyProject::mylib ALIAS mylib)
target_link_libraries(consumer PRIVATE MyProject::mylib)Linking Order
CMake usually resolves linking order correctly. For tricky circular cases, use the --start-group linker flag or refactor your library structure.
Common Mistake: Global include_directories
Avoid the global include_directories command. Use target_include_directories so headers attach to specific targets only.
Quick Check
What does target_link_libraries(mylib PUBLIC fmt) mean for a target that links mylib?
Recap
Modern CMake uses targets as the unit of work. Set properties with target_* commands and use PUBLIC/PRIVATE/INTERFACE to control how properties propagate to consumers.
Learn C++ with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 51
- Lessons
- 203
Frequently Asked Questions
Is the “Targets Linking and Visibility PUBLIC PRIVATE” lesson free?
Yes — any 3 lessons of the C++ Academy learning path, including “Targets Linking and Visibility PUBLIC PRIVATE”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The C++ Academy course includes 4 lessons in total.
What will I learn in “Targets Linking and Visibility PUBLIC PRIVATE”?
Define targets, link libraries, and use PUBLIC, PRIVATE, INTERFACE visibility correctly. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C++ Academy?
No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.
How long does the “Targets Linking and Visibility PUBLIC PRIVATE” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C++ Academy lesson?
Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- CMake Project Layout and CMakeLists.txt
- Targets Linking and Visibility PUBLIC PRIVATE
- Using vcpkg and Conan for Dependencies
- Generating IDE Files and Cross-Platform Builds