CPP Build Systems & Package Management 2 — Questions and Answers
Question 1: In CMake, what does the `target_include_directories` command's `PUBLIC` keyword signify?
- Directories are only used when compiling the target itself
- Directories are added to the target and any target that links to it (Correct answer)
- Directories are only propagated to targets that link to this target
- Directories are hidden from all consumers
Correct answer: Directories are added to the target and any target that links to it
PUBLIC means the include directories apply both to the target itself and to any downstream targets that link against it.
Question 2: Which CMake generator is commonly used to produce Ninja build files?
- -G "Unix Makefiles"
- -G "Ninja" (Correct answer)
- -G "NMake Makefiles"
- -G "Visual Studio 17"
Correct answer: -G "Ninja"
The `-G "Ninja"` flag tells CMake to generate Ninja build files instead of Makefiles.
Question 3: What is the purpose of `vcpkg integrate install` on Windows?
- Installs all packages in the manifest
- Makes vcpkg libraries automatically available to MSBuild and Visual Studio projects (Correct answer)
- Removes integration with Visual Studio
- Downloads the vcpkg binary
Correct answer: Makes vcpkg libraries automatically available to MSBuild and Visual Studio projects
`vcpkg integrate install` wires vcpkg into MSBuild so installed libraries are found automatically in Visual Studio.
Question 4: In a Makefile, what does the special target `.PHONY` accomplish?
- Marks a target as requiring root permissions
- Declares targets that are not actual files, preventing conflicts with same-named files (Correct answer)
- Enables parallel builds for listed targets
- Forces a rebuild of all dependencies
Correct answer: Declares targets that are not actual files, preventing conflicts with same-named files
`.PHONY` tells make that a target name is a label for a recipe, not a real file, so it always runs.
Question 5: Which CMake command sets a compile definition only for a specific target without affecting others?
- add_definitions()
- target_compile_definitions() (Correct answer)
- set_property(GLOBAL)
- cmake_compile_options()
Correct answer: target_compile_definitions()
`target_compile_definitions()` scopes preprocessor definitions to a single named target.
Question 6: What does `conan install . --build=missing` do when a prebuilt binary is not available?
- Skips packages with no binary and continues
- Downloads source and builds the package from source (Correct answer)
- Raises an error and halts
- Installs an older cached version
Correct answer: Downloads source and builds the package from source
`--build=missing` instructs Conan to compile any package for which no compatible prebuilt binary exists.
Question 7: In CMake, what is the difference between `add_library(mylib STATIC ...)` and `add_library(mylib SHARED ...)`?
- STATIC creates a .dll and SHARED creates a .lib
- STATIC creates an archive linked at compile time; SHARED creates a dynamic library linked at runtime (Correct answer)
- Both produce identical output on Linux
- SHARED is only valid on Windows
Correct answer: STATIC creates an archive linked at compile time; SHARED creates a dynamic library linked at runtime
STATIC produces a `.a`/`.lib` archive embedded in the executable, while SHARED produces a `.so`/`.dll` loaded at runtime.
In CMake, what does the `target_include_directories` command's `PUBLIC` keyword signify?