CMake Errors: 10 Common CMake Error Messages and How to Fix
이 글의 핵심
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](/en/blog/cpp-cmake/ · [find_package](/en/blog/cpp-cmake-find-package/ · [targets](/en/blog/cpp-cmake-targets/. find_package pain: [Could NOT find …](/en/blog/cpp-error-03-cmake-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.
# 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
# High requirement
cmake_minimum_required(VERSION 3.20)
# Match your installed CMake
cmake_minimum_required(VERSION 3.16)
Fix 2: Upgrade CMake
# 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
# 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.
# 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)
# Problem: missing closing parenthesis
add_executable(myapp
main.cpp
utils.cpp
# CMake Error: Parse error. Expected a newline, got EOF.
Fix:
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
add_executable(myapp
main.cpp
utils.cpp # file missing
)
# CMake Error: Cannot find source file: utils.cpp
Fix: Verify paths.
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
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
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.
# Avoid
cd project/
cmake .
# Prefer
cd project/
mkdir build
cd build
cmake ..
2. CMakeLists.txt structure
Minimal template
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_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)
- CMake intro
- CMake “Could NOT find”
- CMake link errors
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.
Closing
Most CMake issues boil down to syntax and target ordering. Use out-of-source builds, define targets first, and fix find_package paths (or use a package manager). Master these patterns and CMake becomes a reliable part of your workflow. Next: Explore modern CMake patterns (target-based APIs) in your project’s documentation or follow-up posts.
More related posts
자주 묻는 질문 (FAQ)
Q. 이 내용을 실무에서 언제 쓰나요?
A. Fix CMake Error messages: target not found, version mismatch, find_package failures, syntax errors, and out-of-source bu… 실무에서는 위 본문의 예제와 선택 가이드를 참고해 적용하면 됩니다.
Q. 선행으로 읽으면 좋은 글은?
A. 각 글 하단의 이전 글 또는 관련 글 링크를 따라가면 순서대로 배울 수 있습니다. C++ 시리즈 목차에서 전체 흐름을 확인할 수 있습니다.
Q. 더 깊이 공부하려면?
A. cppreference와 해당 라이브러리 공식 문서를 참고하세요. 글 말미의 참고 자료 링크도 활용하면 좋습니다.
같이 보면 좋은 글 (내부 링크)
이 주제와 연결되는 다른 글입니다.
- [C++ CMake Complete Guide | Cross-Platform Builds](/en/blog/cpp-cmake/
- [CMake find_package for C++ | Boost· OpenSSL](/en/blog/cpp-cmake-find-package/
- [CMake Targets in C++ | PUBLIC/PRIVATE](/en/blog/cpp-cmake-targets/
- [CMake “Could NOT find …”: find_package Failures,](/en/blog/cpp-error-03-cmake-could-not-find/
- CMake 입문 | 수십 개 파일 컴파일할 때 필요한 빌드 자동화 (CMakeLists.txt 기초)
- C++ CMake 링크 에러 LNK2019 | 원인과 해결 [#49-2]
이 글에서 다루는 키워드 (관련 검색어)
C++, CMake, Build errors, CMakeLists, Troubleshooting, C++ build 등으로 검색하시면 이 글이 도움이 됩니다.