Complete CMake Error Resolution Complete Guide
이 글의 핵심
Detailed analysis of CMake build error causes and solutions. Practical troubleshooting guide from compiler detection failures, library linking errors, d...
🎯 What You’ll Learn (Reading Time: 50 minutes)
TL;DR: Learn systematic approaches to resolve CMake build errors. Provides solutions for all common production issues from compiler detection failures to linker errors. What You’ll Learn:
- ✅ Master CMake error message interpretation and debugging strategies
- ✅ Acquire skills to resolve compiler/library detection failures
- ✅ Understand linker error and dependency problem resolution patterns
- ✅ Improve cross-platform build issue response capabilities Real-World Applications:
- 🔥 Build C++ project build systems
- 🔥 Integrate external libraries (Boost, OpenCV, etc.)
- 🔥 Stabilize CI/CD pipeline builds
- 🔥 Configure cross-platform builds Difficulty: Intermediate | Practical Examples: 20 | Production Level
Core Message
A complete guide to understanding root causes of CMake build errors and resolving them systematically. Provides frequently encountered error patterns and proven solutions from production.
Production Experience: Sharing know-how and debugging techniques accumulated from resolving CMake build issues across dozens of projects.
Introduction: “Why Does CMake Fail?”
Common Error Scenarios
Scenario 1: Cannot Find Compiler
CMake Error: Could not find CMAKE_CXX_COMPILER
Scenario 2: Library Linking Failure
undefined reference to `boost::system::error_category::~error_category()'
Scenario 3: Dependency Version Conflict
CMake Error: Could NOT find OpenSSL (missing: OPENSSL_CRYPTO_LIBRARY)
Scenario 4: Cross-Platform Build Failure
Builds on Windows but fails on Linux.
Table of Contents
- CMake Error Classification System
- Compiler-Related Errors
- Library Finding Errors
- Linker Errors
- Dependency Management Errors
- Cross-Platform Errors
- Cache and Configuration Errors
- Advanced Debugging Techniques
- Prevention and Best Practices
1. CMake Error Classification System
Error Classification by Stage
flowchart TD
A[CMake Execution] --> B{Configure Stage}
B -->|Failure| C[Configure Error]
B -->|Success| D{Generate Stage}
D -->|Failure| E[Generate Error]
D -->|Success| F{Build Stage}
F -->|Failure| G[Build Error]
F -->|Success| H[Success]
C --> C1[Compiler Detection Failure]
C --> C2[find_package Failure]
C --> C3[CMakeLists.txt Syntax Error]
E --> E1[Generator Error]
E --> E2[Toolchain Error]
G --> G1[Compile Error]
G --> G2[Linker Error]
G --> G3[Dependency Error]
Error Severity
| Severity | Type | Example | Resolution Difficulty |
|---|---|---|---|
| FATAL_ERROR | Immediate halt | CMake Error | High |
| SEND_ERROR | Continue then fail | CMake Warning (dev) | Medium |
| WARNING | Warning only | Policy CMP0xxx | Low |
| STATUS | Info message | -- Found OpenSSL | - |
2. Compiler-Related Errors
Error 1: Cannot Find Compiler
Symptoms:
CMake Error: Could not find CMAKE_C_COMPILER
CMake Error: Could not find CMAKE_CXX_COMPILER
Causes:
- Compiler not installed
- Not in PATH environment variable
- CMake referencing wrong path Solutions:
Linux/macOS
# Check GCC installation
which gcc g++
# Install if missing
# Ubuntu/Debian
sudo apt-get install build-essential
# macOS
xcode-select --install
# Explicitly specify to CMake
cmake -DCMAKE_C_COMPILER=/usr/bin/gcc \
-DCMAKE_CXX_COMPILER=/usr/bin/g++ \
..
Windows
# Check Visual Studio installation
where cl.exe
# Run from Visual Studio Developer Command Prompt
# Or explicitly specify to CMake
cmake -G "Visual Studio 17 2022" -A x64 ..
# When using MinGW
cmake -G "MinGW Makefiles" \
-DCMAKE_C_COMPILER=C:/mingw64/bin/gcc.exe \
-DCMAKE_CXX_COMPILER=C:/mingw64/bin/g++.exe \
..
Error 2: Compiler Version Mismatch
Symptoms:
CMake Error: The C++ compiler does not support C++17
Solution:
# CMakeLists.txt
cmake_minimum_required(VERSION 3.15)
project(MyProject)
# Specify C++ standard
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
# Check compiler version
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS "7.0")
message(FATAL_ERROR "GCC 7.0 or higher required (current: ${CMAKE_CXX_COMPILER_VERSION})")
endif()
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS "19.14")
message(FATAL_ERROR "MSVC 2017 15.7 or higher required")
endif()
endif()
Error 3: Cross-Compilation Configuration Error
Symptoms:
CMake Error: TRY_RUN() invoked in cross-compiling mode
Solution:
# toolchain.cmake
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR arm)
set(CMAKE_C_COMPILER arm-linux-gnueabihf-gcc)
set(CMAKE_CXX_COMPILER arm-linux-gnueabihf-g++)
# Pre-set TRY_RUN results for cross-compilation
set(HAVE_SOME_FEATURE 1 CACHE BOOL "Feature check result")
# Usage
cmake -DCMAKE_TOOLCHAIN_FILE=toolchain.cmake ..
3. Library Finding Errors
Error 4: find_package Failure
Symptoms:
CMake Error at CMakeLists.txt:10 (find_package):
Could NOT find Boost (missing: Boost_INCLUDE_DIR)
Causes:
- Library not installed
- Installed in path CMake cannot find
- Version mismatch Solutions:
Method 1: Specify Installation Path
# Boost example
cmake -DBoost_ROOT=/usr/local/boost_1_75_0 \
-DBoost_INCLUDE_DIR=/usr/local/boost_1_75_0/include \
-DBoost_LIBRARY_DIR=/usr/local/boost_1_75_0/lib \
..
Method 2: Use CMAKE_PREFIX_PATH
cmake -DCMAKE_PREFIX_PATH="/usr/local;/opt/local" ..
Method 3: Modify CMakeLists.txt
# Output debug info
set(Boost_DEBUG ON)
# Change from required to optional
find_package(Boost 1.70 COMPONENTS system filesystem)
if(NOT Boost_FOUND)
message(WARNING "Boost not found, some features disabled")
set(ENABLE_BOOST_FEATURES OFF)
else()
message(STATUS "Boost version: ${Boost_VERSION}")
message(STATUS "Boost include: ${Boost_INCLUDE_DIRS}")
message(STATUS "Boost libraries: ${Boost_LIBRARIES}")
endif()
# Alternative: Use pkg-config
find_package(PkgConfig)
if(PKG_CONFIG_FOUND)
pkg_check_modules(BOOST QUIET boost)
if(BOOST_FOUND)
include_directories(${BOOST_INCLUDE_DIRS})
link_directories(${BOOST_LIBRARY_DIRS})
endif()
endif()
Error 5: Library Version Conflict
Symptoms:
CMake Error: Could NOT find OpenSSL: Found unsuitable version "1.0.2",
but required is at least "1.1.0"
Solution:
# Specify version range
find_package(OpenSSL 1.1.0 REQUIRED)
# Or try multiple versions
find_package(OpenSSL 3.0)
if(NOT OpenSSL_FOUND)
find_package(OpenSSL 1.1 REQUIRED)
endif()
# Output version info
message(STATUS "OpenSSL version: ${OPENSSL_VERSION}")
message(STATUS "OpenSSL include: ${OPENSSL_INCLUDE_DIR}")
message(STATUS "OpenSSL libraries: ${OPENSSL_LIBRARIES}")
Error 6: Config vs Module Mode Confusion
Symptoms:
CMake Warning: Could not find a package configuration file provided by "MyLib"
Understanding:
- Config mode: Find
MyLibConfig.cmakefile (provided by library) - Module mode: Find
FindMyLib.cmakefile (provided by CMake) Solution:
# Force Config mode
find_package(MyLib CONFIG REQUIRED)
# Force Module mode
find_package(MyLib MODULE REQUIRED)
# Add custom Find module path
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/modules")
# Manual finding
find_path(MYLIB_INCLUDE_DIR mylib.h
PATHS /usr/local/include /opt/local/include
)
find_library(MYLIB_LIBRARY mylib
PATHS /usr/local/lib /opt/local/lib
)
if(MYLIB_INCLUDE_DIR AND MYLIB_LIBRARY)
set(MYLIB_FOUND TRUE)
add_library(MyLib::MyLib UNKNOWN IMPORTED)
set_target_properties(MyLib::MyLib PROPERTIES
IMPORTED_LOCATION "${MYLIB_LIBRARY}"
INTERFACE_INCLUDE_DIRECTORIES "${MYLIB_INCLUDE_DIR}"
)
endif()
4. Linker Errors
Error 7: undefined reference
Symptoms:
undefined reference to `boost::system::error_category::~error_category()'
undefined reference to `pthread_create'
Causes:
- Library not linked
- Link order error
- Static/dynamic library mixing Solutions:
Method 1: Explicit Linking
# Link libraries
target_link_libraries(myapp PRIVATE
Boost::system
Boost::filesystem
pthread
)
# Or specify directly
target_link_libraries(myapp PRIVATE
${Boost_LIBRARIES}
pthread
)
Method 2: Adjust Link Order
# ❌ Wrong order
target_link_libraries(myapp
lib_a
lib_b # lib_b depends on lib_a
)
# ✅ Correct order (dependencies later)
target_link_libraries(myapp
lib_b
lib_a
)
Method 3: Whole Archive Linking
# Include all symbols from static library
if(UNIX)
target_link_libraries(myapp PRIVATE
-Wl,--whole-archive
mystaticlib
-Wl,--no-whole-archive
)
elseif(APPLE)
target_link_libraries(myapp PRIVATE
-Wl,-force_load
mystaticlib
)
elseif(MSVC)
target_link_libraries(myapp PRIVATE
/WHOLEARCHIVE:mystaticlib
)
endif()
Error 8: Duplicate Symbol
Symptoms:
duplicate symbol '_myFunction' in:
obj1.o
obj2.o
Causes:
- Function definition in header (without inline)
- Same source file compiled multiple times
- Library linked multiple times Solutions:
// ❌ Wrong header (mylib.h)
void myFunction() { // Defined multiple times
// ...
}
// ✅ Correct header
inline void myFunction() { // Add inline
// ...
}
// Or declaration only
void myFunction(); // Definition in .cpp
# Remove duplicate links
target_link_libraries(myapp PRIVATE
mylib
# mylib # Remove duplicate
)
# Or propagate with INTERFACE
add_library(mylib INTERFACE)
target_link_libraries(mylib INTERFACE
dependency
)
target_link_libraries(myapp PRIVATE
mylib # dependency automatically included
)
Error 9: Cannot Find Library (runtime)
Symptoms:
./myapp: error while loading shared libraries: libmylib.so.1:
cannot open shared object file: No such file or directory
Solutions:
Linux
# Method 1: Set LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
./myapp
# Method 2: Update ldconfig
sudo echo "/usr/local/lib" > /etc/ld.so.conf.d/mylib.conf
sudo ldconfig
# Method 3: Set RPATH (CMake)
# CMakeLists.txt
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
# Or use $ORIGIN (relative to executable)
set_target_properties(myapp PROPERTIES
INSTALL_RPATH "$ORIGIN/../lib"
)
macOS
# Set DYLD_LIBRARY_PATH
export DYLD_LIBRARY_PATH=/usr/local/lib:$DYLD_LIBRARY_PATH
# Or use install_name_tool
install_name_tool -change \
libmylib.dylib \
@rpath/libmylib.dylib \
myapp
Windows
# Copy DLL to same directory as executable
add_custom_command(TARGET myapp POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
$<TARGET_FILE:mylib>
$<TARGET_FILE_DIR:myapp>
)
5. Dependency Management Errors
Error 10: FetchContent Failure
Symptoms:
CMake Error: Failed to download https://github.com/user/repo/archive/v1.0.tar.gz
Solution:
include(FetchContent)
# Increase timeout
set(FETCHCONTENT_QUIET OFF)
FetchContent_Declare(
mylib
GIT_REPOSITORY https://github.com/user/mylib.git
GIT_TAG v1.0.0
GIT_SHALLOW TRUE # Fast without history
TIMEOUT 60 # 60 second timeout
)
# Error handling
FetchContent_MakeAvailable(mylib)
if(NOT mylib_POPULATED)
message(FATAL_ERROR "Failed to fetch mylib")
endif()
# Or manually
FetchContent_GetProperties(mylib)
if(NOT mylib_POPULATED)
FetchContent_Populate(mylib)
add_subdirectory(${mylib_SOURCE_DIR} ${mylib_BINARY_DIR})
endif()
Error 11: ExternalProject Build Failure
Symptoms:
CMake Error: External project failed to build
Solution:
include(ExternalProject)
ExternalProject_Add(
mylib_external
URL https://example.com/mylib-1.0.tar.gz
URL_HASH SHA256=abc123...
# Build configuration
CMAKE_ARGS
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
-DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}/external
-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
# Enable logging
LOG_DOWNLOAD ON
LOG_CONFIGURE ON
LOG_BUILD ON
LOG_INSTALL ON
# Parallel build
BUILD_COMMAND ${CMAKE_COMMAND} --build . --parallel
)
# Add dependency
add_dependencies(myapp mylib_external)
# Link library
target_link_libraries(myapp PRIVATE
${CMAKE_BINARY_DIR}/external/lib/libmylib.a
)
target_include_directories(myapp PRIVATE
${CMAKE_BINARY_DIR}/external/include
)
Error 12: vcpkg/Conan Integration Failure
Symptoms:
CMake Error: Could not find vcpkg toolchain file
Solutions:
vcpkg
# Install vcpkg
git clone https://github.com/Microsoft/vcpkg.git
cd vcpkg
./bootstrap-vcpkg.sh # Linux/macOS
# .\bootstrap-vcpkg.bat # Windows
# Install packages
./vcpkg install boost openssl
# CMake integration
cmake -DCMAKE_TOOLCHAIN_FILE=/path/to/vcpkg/scripts/buildsystems/vcpkg.cmake ..
# CMakeLists.txt
if(DEFINED ENV{VCPKG_ROOT} AND NOT DEFINED CMAKE_TOOLCHAIN_FILE)
set(CMAKE_TOOLCHAIN_FILE "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake"
CACHE STRING "")
endif()
project(MyProject)
find_package(Boost REQUIRED)
find_package(OpenSSL REQUIRED)
Conan
# conanfile.txt
[requires]
boost/1.75.0
openssl/1.1.1k
[generators]
cmake_find_package
# Install
conan install . --build=missing
# CMake
cmake -DCMAKE_PREFIX_PATH=$(pwd) ..
6. Cross-Platform Errors
Error 13: Path Separator Issues
Symptoms:
CMake Error: File not found: C:\path\to\file (Windows)
CMake Error: File not found: /path/to/file (Linux)
Solution:
# ❌ Hardcoded path
set(MY_PATH "C:/Users/me/project")
# ✅ Platform independent
set(MY_PATH "${CMAKE_SOURCE_DIR}/subdir")
# ✅ Path joining
file(TO_CMAKE_PATH "${MY_PATH}" MY_PATH)
# ✅ Convert to native path
file(TO_NATIVE_PATH "${MY_PATH}" MY_NATIVE_PATH)
# ✅ File copy (automatic path conversion)
configure_file(
"${CMAKE_SOURCE_DIR}/config.h.in"
"${CMAKE_BINARY_DIR}/config.h"
)
Error 14: Platform-Specific Compile Option Conflicts
Symptoms:
cl.exe: error: unknown argument: '-Wall'
gcc: error: unrecognized command line option '/W4'
Solution:
# Platform-specific options
if(MSVC)
target_compile_options(myapp PRIVATE
/W4 # Warning level 4
/WX # Warnings as errors
/permissive- # Standards compliance
)
else()
target_compile_options(myapp PRIVATE
-Wall
-Wextra
-Werror
-pedantic
)
endif()
# Or use generator expressions
target_compile_options(myapp PRIVATE
$<$<CXX_COMPILER_ID:MSVC>:/W4>
$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wall>
)
# Platform-specific definitions
if(WIN32)
target_compile_definitions(myapp PRIVATE
_WIN32_WINNT=0x0601 # Windows 7
NOMINMAX # Disable min/max macros
)
elseif(UNIX)
target_compile_definitions(myapp PRIVATE
_GNU_SOURCE
)
endif()
Error 15: Library Extension Differences
Symptoms:
CMake Error: Cannot find libmylib.so (Linux)
CMake Error: Cannot find mylib.dll (Windows)
Solution:
# ❌ Hardcoded extension
find_library(MYLIB_LIBRARY mylib.so)
# ✅ Without extension
find_library(MYLIB_LIBRARY mylib)
# ✅ Platform-specific handling
if(WIN32)
set(LIB_SUFFIX ".dll")
elseif(APPLE)
set(LIB_SUFFIX ".dylib")
else()
set(LIB_SUFFIX ".so")
endif()
# ✅ Use CMake variables
set(CMAKE_FIND_LIBRARY_PREFIXES "lib" "")
set(CMAKE_FIND_LIBRARY_SUFFIXES ".so" ".a" ".dll" ".lib")
7. Cache and Configuration Errors
Error 16: Cached Configuration Issues
Symptoms:
CMake Error: Variable was cached but is now different
Solution:
# Method 1: Delete cache
rm CMakeCache.txt
rm -rf CMakeFiles/
# Method 2: Completely rebuild
rm -rf build/
mkdir build && cd build
cmake ..
# Method 3: Delete cache in CMake GUI
cmake-gui .
# File -> Delete Cache
# Force update cache variable
set(MY_VAR "new_value" CACHE STRING "Description" FORCE)
# Clear cache variable
unset(MY_VAR CACHE)
# Conditional cache setting
if(NOT DEFINED MY_VAR)
set(MY_VAR "default" CACHE STRING "Description")
endif()
Error 17: Generator Mismatch
Symptoms:
CMake Error: Error: generator : Ninja
Does not match the generator used previously: Unix Makefiles
Solution:
# Delete build directory and regenerate
rm -rf build/
mkdir build && cd build
# Specify Generator
cmake -G "Ninja" ..
# Or
cmake -G "Unix Makefiles" ..
# Or
cmake -G "Visual Studio 17 2022" ..
# List available Generators
cmake --help
Error 18: Policy Warnings
Symptoms:
CMake Warning (dev): Policy CMP0054 is not set
Solution:
# Specify minimum version (automatically sets policies)
cmake_minimum_required(VERSION 3.15)
# Explicit policy setting
cmake_policy(SET CMP0054 NEW)
# Multiple policy settings
if(POLICY CMP0054)
cmake_policy(SET CMP0054 NEW)
endif()
# Policy scope
cmake_policy(PUSH)
cmake_policy(SET CMP0054 OLD)
# ... code ...
cmake_policy(POP)
8. Advanced Debugging Techniques
Enable Debugging Options
# Verbose output
cmake --debug-output ..
# Trace variables
cmake --trace ..
# Trace specific variables only
cmake --trace-expand --trace-source=CMakeLists.txt ..
# Set log level
cmake --log-level=DEBUG ..
CMakeLists.txt Debugging
# Output variables
message(STATUS "CMAKE_SOURCE_DIR: ${CMAKE_SOURCE_DIR}")
message(STATUS "CMAKE_BINARY_DIR: ${CMAKE_BINARY_DIR}")
message(STATUS "CMAKE_CXX_COMPILER: ${CMAKE_CXX_COMPILER}")
# Output all variables
get_cmake_property(_variableNames VARIABLES)
foreach(_variableName ${_variableNames})
message(STATUS "${_variableName}=${${_variableName}}")
endforeach()
# Output target properties
get_target_property(MYAPP_SOURCES myapp SOURCES)
message(STATUS "myapp sources: ${MYAPP_SOURCES}")
get_target_property(MYAPP_LINK_LIBS myapp LINK_LIBRARIES)
message(STATUS "myapp link libraries: ${MYAPP_LINK_LIBS}")
# Conditional debugging
option(CMAKE_DEBUG "Enable CMake debugging" OFF)
if(CMAKE_DEBUG)
message(STATUS "=== Debug Info ===")
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
message(STATUS "C++ flags: ${CMAKE_CXX_FLAGS}")
message(STATUS "Link flags: ${CMAKE_EXE_LINKER_FLAGS}")
endif()
Build Log Analysis
# Verbose build log
cmake --build . --verbose
# Or when using make
make VERBOSE=1
# When using Ninja
ninja -v
# Save to log file
cmake --build . --verbose > build.log 2>&1
# Build specific target only
cmake --build . --target myapp --verbose
Check Linker Commands
# Output linker commands
set(CMAKE_VERBOSE_MAKEFILE ON)
# Or
set_target_properties(myapp PROPERTIES
LINK_WHAT_YOU_USE TRUE
)
# Generate linker map file
if(UNIX)
target_link_options(myapp PRIVATE
-Wl,-Map=myapp.map
)
elseif(MSVC)
target_link_options(myapp PRIVATE
/MAP:myapp.map
)
endif()
9. Prevention and Best Practices
1) Write Robust CMakeLists.txt
# Specify minimum version (latest recommended)
cmake_minimum_required(VERSION 3.15)
# Project information
project(MyProject
VERSION 1.0.0
DESCRIPTION "My awesome project"
LANGUAGES CXX
)
# C++ standard settings
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
# Build type default
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE)
endif()
# Unify output directories
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
# Provide options
option(BUILD_SHARED_LIBS "Build shared libraries" OFF)
option(BUILD_TESTS "Build tests" ON)
option(ENABLE_WARNINGS "Enable compiler warnings" ON)
# Find dependencies (with error handling)
find_package(Boost 1.70 COMPONENTS system filesystem)
if(NOT Boost_FOUND)
message(FATAL_ERROR "Boost not found. Install: sudo apt-get install libboost-all-dev")
endif()
# Define target
add_executable(myapp src/main.cpp)
# Set target properties
target_include_directories(myapp PRIVATE
${CMAKE_SOURCE_DIR}/include
${Boost_INCLUDE_DIRS}
)
target_link_libraries(myapp PRIVATE
Boost::system
Boost::filesystem
)
# Compile options
if(ENABLE_WARNINGS)
if(MSVC)
target_compile_options(myapp PRIVATE /W4)
else()
target_compile_options(myapp PRIVATE -Wall -Wextra)
endif()
endif()
# Install rules
install(TARGETS myapp
RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
)
2) Dependency Management Strategy
# Method 1: FetchContent (simple, fast)
include(FetchContent)
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG release-1.11.0
)
FetchContent_MakeAvailable(googletest)
# Method 2: find_package + Fallback
find_package(fmt 8.0)
if(NOT fmt_FOUND)
message(STATUS "fmt not found, fetching...")
FetchContent_Declare(
fmt
GIT_REPOSITORY https://github.com/fmtlib/fmt.git
GIT_TAG 8.1.1
)
FetchContent_MakeAvailable(fmt)
endif()
# Method 3: Conditional dependencies
if(ENABLE_FEATURE_X)
find_package(LibX REQUIRED)
target_link_libraries(myapp PRIVATE LibX::LibX)
target_compile_definitions(myapp PRIVATE FEATURE_X_ENABLED)
endif()
3) Error Prevention Checklist
# 1. Check variable existence
if(NOT DEFINED MY_VAR)
message(FATAL_ERROR "MY_VAR is not defined")
endif()
# 2. Check file existence
if(NOT EXISTS "${CMAKE_SOURCE_DIR}/config.h.in")
message(FATAL_ERROR "config.h.in not found")
endif()
# 3. Check target existence
if(TARGET mylib)
target_link_libraries(myapp PRIVATE mylib)
else()
message(WARNING "mylib target not found")
endif()
# 4. Check compiler features
include(CheckCXXCompilerFlag)
check_cxx_compiler_flag(-std=c++17 HAS_CXX17)
if(NOT HAS_CXX17)
message(FATAL_ERROR "Compiler does not support C++17")
endif()
# 5. Check header files
include(CheckIncludeFileCXX)
check_include_file_cxx("filesystem" HAS_FILESYSTEM)
if(NOT HAS_FILESYSTEM)
message(FATAL_ERROR "<filesystem> header not found")
endif()
# 6. Check function existence
include(CheckFunctionExists)
check_function_exists(pthread_create HAS_PTHREAD)
if(NOT HAS_PTHREAD)
message(FATAL_ERROR "pthread_create not found")
endif()
4) Build Script Automation
#!/bin/bash
# build.sh
set -e # Stop on error
# Color definitions
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color
# Function definitions
error() {
echo -e "${RED}Error: $1${NC}" >&2
exit 1
}
success() {
echo -e "${GREEN}$1${NC}"
}
# Check dependencies
command -v cmake >/dev/null 2>&1 || error "CMake not found"
command -v ninja >/dev/null 2>&1 || error "Ninja not found"
# Create build directory
BUILD_DIR="build"
rm -rf "$BUILD_DIR"
mkdir -p "$BUILD_DIR"
cd "$BUILD_DIR"
# CMake configuration
cmake -G Ninja \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
...|| error "CMake configuration failed"
# Build
cmake --build . --parallel || error "Build failed"
# Test
if [ -f "bin/myapp_test" ]; then
./bin/myapp_test || error "Tests failed"
success "All tests passed!"
fi
success "Build completed successfully!"
Summary
Key Points
- Error Classification
- Configure errors: Compiler, find_package failures
- Generate errors: Generator, Toolchain issues
- Build errors: Compile, linker errors
- Major Error Patterns
- Compiler detection failure → Check PATH, explicit specification
- find_package failure → Specify installation path, CMAKE_PREFIX_PATH
- Linker errors → target_link_libraries, check order
- Cross-platform → Platform-specific branching, generator expressions
- Debugging Techniques
--debug-output,--traceoptions- Use message(STATUS)
- VERBOSE build logs
- Prevention Strategies
- Clear error messages
- Specify dependency versions
- Platform-independent code
- Automated build scripts
Quick Reference
# Delete cache
rm CMakeCache.txt
# Verbose output
cmake --debug-output ..
# Build log
cmake --build . --verbose
# Change Generator
cmake -G "Ninja" ..
# Specify variables
cmake -DCMAKE_PREFIX_PATH=/usr/local ..
# Specify compiler
cmake -DCMAKE_CXX_COMPILER=g++-11 ..
Checklist
- Specify CMake minimum version
- Set C++ standard
- Specify dependency versions
- Handle platform-specific branching
- Clear error messages
- Automate build scripts
- Delete cache when issues occur
- Check VERBOSE logs
References
- CMake Documentation
- CMake Discourse
- Professional CMake
- Effective CMake One-Line Summary: CMake errors are classified into Configure/Generate/Build stages, most can be resolved by specifying compiler/library paths, deleting cache, and checking VERBOSE logs, and can be prevented with platform-independent code and clear error messages.
Frequently Asked Questions (FAQ)
Q. When would I use this in practice?
A. Detailed analysis of CMake build error causes and solutions. Practical troubleshooting guide from compiler detection fai…
Q. What should I read before this?
A. Follow the previous article or related articles links at the bottom of each post to learn in sequence.
Q. Where can I study this more deeply?
A. Check cppreference and the relevant library’s official documentation. The reference links at the end of the article are also worth using.
Related Articles (Internal Links)
Other articles related to this topic.
- CMake 에러 완벽 해결 가이드 | 빌드 실패·의존성·링커 에러 트러블슈팅
- CMake find_package for C++ | Boost· OpenSSL
- CMake Errors: 10 Common CMake Error Messages and How to Fix
Keywords Covered in This Article (Related Search Terms)
This article covers CMake, Build, Error, Troubleshooting, C++, Debugging.