[2026] CMake Errors: 10 Common CMake Error Messages and How to Fix Them
이 글의 핵심
Fix CMake Error messages: target not found, version mismatch, find_package failures, syntax errors, and out-of-source builds. Practical CMakeLists.txt patterns for C++ projects.
Modern CMake (English): overview · find_package · targets. find_package pain: Could NOT find ….
Introduction: “There are too many CMake errors”
“I don’t know how to fix CMakeLists.txt”
CMake automates C++ build systems, but error messages can feel cryptic, which makes it hard for beginners. 아래 코드는 cmake를 사용한 구현 예제입니다. 코드를 직접 실행해보면서 동작을 확인해보세요.
# Problematic example
add_executable(myapp main.cpp)
target_link_libraries(myapp mylib) # mylib is not defined
# CMake Error: Cannot specify link libraries for target "mylib"
# which is not built by this project.
This article covers:
- 10 frequent CMake errors
- Common CMakeLists.txt mistakes
- find_package failures
- Target dependency issues
- Path configuration mistakes
Table of contents
- Ten common CMake errors
- CMakeLists.txt syntax basics
- find_package errors
- Target dependencies
- Summary
1. Ten common CMake errors
Error 1: CMake version too old
CMake Error: CMake 3.20 or higher is required. You are running version 3.16
Fix 1: Lower cmake_minimum_required 아래 코드는 cmake를 사용한 구현 예제입니다. 코드를 직접 실행해보면서 동작을 확인해보세요.
# High requirement
cmake_minimum_required(VERSION 3.20)
# Match your installed CMake
cmake_minimum_required(VERSION 3.16)
Fix 2: Upgrade CMake 아래 코드는 bash를 사용한 구현 예제입니다. 코드를 직접 실행해보면서 동작을 확인해보세요.
# Ubuntu
sudo apt install cmake
# Or install a newer release
wget https://github.com/Kitware/CMake/releases/download/v3.28.0/cmake-3.28.0-linux-x86_64.sh
sudo sh cmake-3.28.0-linux-x86_64.sh --prefix=/usr/local --skip-license
Error 2: Target not found
아래 코드는 cmake를 사용한 구현 예제입니다. 코드를 직접 실행해보면서 동작을 확인해보세요.
# Problem
target_link_libraries(myapp mylib) # mylib not defined yet
add_executable(myapp main.cpp)
# CMake Error: Cannot specify link libraries for target "mylib"
Fix: Define targets in the right order. 다음은 간단한 cmake 코드 예제입니다. 코드를 직접 실행해보면서 동작을 확인해보세요.
# Correct order
add_library(mylib mylib.cpp)
add_executable(myapp main.cpp)
target_link_libraries(myapp mylib) # mylib already exists
Error 3: Syntax error (unbalanced parentheses)
아래 코드는 cmake를 사용한 구현 예제입니다. 에러 처리를 통해 안정성을 확보합니다. 코드를 직접 실행해보면서 동작을 확인해보세요.
# Problem: missing closing parenthesis
add_executable(myapp
main.cpp
utils.cpp
# CMake Error: Parse error. Expected a newline, got EOF.
Fix: 다음은 간단한 cmake 코드 예제입니다. 코드를 직접 실행해보면서 동작을 확인해보세요.
add_executable(myapp
main.cpp
utils.cpp
)
Error 4: Could NOT find package
find_package(Boost REQUIRED)
# CMake Error: Could NOT find Boost (missing: Boost_INCLUDE_DIR)
Fix: See CMake “Could NOT find” errors.
set(CMAKE_PREFIX_PATH "/usr/local" ${CMAKE_PREFIX_PATH})
find_package(Boost REQUIRED)
# Or use vcpkg
Error 5: Wrong file path
아래 코드는 cmake를 사용한 구현 예제입니다. 코드를 직접 실행해보면서 동작을 확인해보세요.
add_executable(myapp
main.cpp
utils.cpp # file missing
)
# CMake Error: Cannot find source file: utils.cpp
Fix: Verify paths. 아래 코드는 bash를 사용한 구현 예제입니다. 코드를 직접 실행해보면서 동작을 확인해보세요.
ls utils.cpp
# Or use GLOB (use with care in production)
file(GLOB SOURCES "src/*.cpp")
add_executable(myapp ${SOURCES})
Error 6: Undefined variable
target_include_directories(myapp PRIVATE ${MY_INCLUDE_DIR})
# CMake Warning: MY_INCLUDE_DIR is not defined
Fix:
set(MY_INCLUDE_DIR "${CMAKE_SOURCE_DIR}/include")
target_include_directories(myapp PRIVATE ${MY_INCLUDE_DIR})
Error 7: Duplicate target
아래 코드는 cmake를 사용한 구현 예제입니다. 코드를 직접 실행해보면서 동작을 확인해보세요.
add_executable(myapp main.cpp)
add_executable(myapp other.cpp) # same name
# CMake Error: add_executable cannot create target "myapp" because
# another target with the same name already exists.
Fix: Use distinct target names.
add_executable(myapp main.cpp)
add_executable(myapp2 other.cpp)
Error 8: Unknown command (typo)
add_executabel(myapp main.cpp) # typo: executable
# CMake Error: Unknown CMake command "add_executabel".
Fix: Check spelling.
Error 9: Paths with spaces
아래 코드는 cmake를 사용한 구현 예제입니다. 코드를 직접 실행해보면서 동작을 확인해보세요.
set(MY_PATH C:/Program Files/MyLib)
# Becomes split: C:/Program and Files/MyLib
# Fix: quote the path
set(MY_PATH "C:/Program Files/MyLib")
target_include_directories(myapp PRIVATE "${MY_PATH}")
Error 10: Dirty in-source build
CMake Error: The source directory is the same as the binary directory.
In-source builds are not allowed.
Fix: Use an out-of-source build. 아래 코드는 bash를 사용한 구현 예제입니다. 코드를 직접 실행해보면서 동작을 확인해보세요.
# Avoid
cd project/
cmake .
# Prefer
cd project/
mkdir build
cd build
cmake ..
2. CMakeLists.txt structure
Minimal template
다음은 cmake를 활용한 상세한 구현 코드입니다. 각 부분의 역할을 이해하면서 코드를 살펴보시기 바랍니다.
cmake_minimum_required(VERSION 3.16)
project(MyProject)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_executable(myapp
src/main.cpp
src/utils.cpp
)
target_include_directories(myapp PRIVATE
${CMAKE_SOURCE_DIR}/include
)
target_link_libraries(myapp
pthread
)
Useful variables
아래 코드는 cmake를 사용한 구현 예제입니다. 코드를 직접 실행해보면서 동작을 확인해보세요.
${CMAKE_SOURCE_DIR} # Top-level source directory
${CMAKE_BINARY_DIR} # Top-level build directory
${CMAKE_CURRENT_SOURCE_DIR}
${PROJECT_NAME}
${CMAKE_CXX_COMPILER}
Summary
CMake error checklist
- cmake_minimum_required present?
- Targets defined before target_link_libraries?
- Parentheses and quotes balanced?
- Source paths correct?
- find_package succeeded (or paths fixed)?
- Out-of-source build directory?
Quick reference
| Message | Likely cause | Fix |
|---|---|---|
version too old | Old CMake | Upgrade or lower requirement |
target not found | Target order | add_executable / add_library first |
syntax error | Parse issue | Match () and quotes |
Could NOT find | Missing package | CMAKE_PREFIX_PATH, vcpkg |
file not found | Wrong path | Fix paths or working directory |
Rules of thumb
- Prefer out-of-source builds.
- Define targets before linking or including against them.
- Quote paths that may contain spaces.
- On find_package failures, check CMAKE_PREFIX_PATH and toolchain files.
- Consider vcpkg or Conan for dependencies.
Related posts (internal)
Keywords for search
CMake error, CMakeLists.txt, Could NOT find, target not found, C++ build
Practical tips
Debugging
- Read the line number in CMake errors.
- Use
--tracefor verbose logs. - Delete the build directory and reconfigure when state is corrupted.
Performance
- ccache speeds rebuilds.
- Ninja is often faster than Make.
- Parallel builds:
cmake --build . -j8
Reviews
- Comment non-obvious CMake logic.
- Prefer target_* commands over global
include_directories.