Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | ||||
| 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
| 25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- geofencing
- UIButton
- github
- Code
- Firebase
- Session
- Swift
- Xcode
- rxswift
- window
- 웹뷰
- iOS16
- Realm
- Apple
- SwiftUI
- error
- mac
- JPA
- shorebird
- MacOS
- 한글
- 이미지
- darkmode
- 개발자
- 맥
- FLUTTER
- appstore
- IOS
- Archive
- Git
Archives
- Today
- Total
EEYatHo 앱 깎는 이야기
CMake 본문
1. CMake란 무엇인가?
CMake는 크로스 플랫폼 빌드 시스템을 생성하는 도구.
운영체제마다 빌드 방식이 다른데, CMake는 하나의 설정 파일(CMakeLists.txt)로 Linux는 Makefile, Windows는 Visual Studio 프로젝트, macOS는 Xcode 프로젝트를 자동으로 만들어줌
한 번 정의하면 어디서나 빌드 가능하게 해주는 도구
2. CMake 사용 이유
- 플랫폼 독립성: OS와 컴파일러에 맞는 빌드 파일 자동 생성
- 대규모 프로젝트 관리: 서브 프로젝트, 외부 라이브러리, 테스트 등을 체계적으로 관리
- 오픈소스 표준: OpenCV, LLVM, Qt 같은 대형 프로젝트들이 모두 CMake 기반
3. 기본 사용법
설치 및 실행 흐름
// CMakeLists.txt파일 위치에서 시작.
mkdir build
cd build
cmake .. // 상위 디렉토리의 CMakeLists.txt를 읽고 빌드 파일 생성
cmake --build . // 실제 컴파일 실행
4. CMakeLists.txt 기본 구조
cmake_minimum_required(VERSION 3.20) # 최소 요구 버전
project(MyApp LANGUAGES CXX) # 프로젝트 이름/언어 지정
set(CMAKE_CXX_STANDARD 17) # C++17 사용
add_executable(myapp main.cpp foo.cpp) # 실행 파일 생성
5. 모던 CMake 핵심 개념
타깃 기반 설정
CMake 3.x부터는 **타깃 단위 관리(target_*)**가 권장
add_library(mylib src/mylib.cpp)
target_include_directories(mylib PUBLIC include)
target_compile_definitions(mylib PRIVATE DEBUG_MODE=1)
target_link_libraries(mylib PUBLIC fmt::fmt)
- PRIVATE: 현재 타깃에만 적용
- PUBLIC: 현재 이 타깃을 사용하는 곳에도 전파
- INTERFACE: 사용자에게만 전파 (헤더 온리 라이브러리에서 유용)
6. 외부 라이브러리 연동
1) find_package
find_package(fmt CONFIG REQUIRED)
target_link_libraries(myapp PRIVATE fmt::fmt)
2) FetchContent
include(FetchContent)
FetchContent_Declare(fmt
GIT_REPOSITORY https://github.com/fmtlib/fmt.git
GIT_TAG 11.0.2
)
FetchContent_MakeAvailable(fmt)
7. 테스트(CTest)
enable_testing()
add_executable(mytest tests/test.cpp)
target_link_libraries(mytest PRIVATE mylib)
add_test(NAME mytest COMMAND mytest)
실행: ctest --output-on-failure
8. 설치 & 배포
install(TARGETS mylib myapp
RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
)
install(DIRECTORY include/ DESTINATION include)
→ cmake --install build --prefix out 로 설치 가능
9. 자주 쓰는 팁
- Out-of-Source 빌드 권장: build/ 폴더 따로 두기
- include_directories(), add_definitions() 전역 사용은 지양 → target_* 사용
- 빌드 옵션은 Generator Expression으로 관리:
target_compile_definitions(myapp PRIVATE
$<$<CONFIG:Debug>:DEBUG_BUILD>
)
10. 기타
작은 프로젝트에서는 간단히 add_executable, target_link_libraries만 알아도 충분
규모가 커질수록 find_package, install, CTest 같은 기능을 점진적으로 활용
'Window' 카테고리의 다른 글
| vcpkg로 onnxruntime-gpu 설치 시 Invalid empty pathname 에러 (0) | 2025.08.21 |
|---|
Comments