[c++] CMake
CMake๋?
- makefile ์ ์ง์ ์ ์ธ ์์ฑ์์ ํ๋ฐ์๊ตญ ๋ ๋์๊ฐ ๋ช ๋ น์ด๋ฅผ ํตํด ์๋์ผ๋ก makefile์ ๋ง๋ค์ด์ฃผ๋ ๋ฐฉ์
์ง์ ์ ์ธ ์ปดํ์ผ ๋ฐฉ์
>> g++ main.cpp -o main
>> ./main
<< HELLO WORLD!!
CMake ์ฌ์ฉ๋ฒ
Cmake ์ค์น
>> brew install cmake
>> cmake --version
<< cmake version 3.31.0
Cmake ํ ์คํธ
์ค์น ํ์ ๊ฐ๋จํ๊ฒ main.cpp ํ์ผ์ Cmake๋ฅผ ํตํด ๋น๋ํด๋ณด์.
CMake๋ฅผ ์ฌ์ฉํ๊ธฐ์ํด์๋ CmakeLists.txt๋ผ๋ ํ์ผ์ ์์ฑํด์ค์ผํ๋ค.
CmakeLists.txt์๋ ๋น๋๋ฅผ ์งํํ๊ธฐ์ํด ํ์ํ ์ ๋ณด๋ค์ ๋ด์์ ์ด๋ป๊ฒ ๋น๋๋ฅผ ์งํํ ๊ฒ์ธ์ง ์๋ ค์ฃผ๋ ์์
์ด ํ์ํ๋ค.
( ex: ์ด๋ค ํ์ผ์ ๋น๋ํ ๊ฒ์ธ์ง.. , ์คํํ์ผ์ ์ด๋ฆ์ ๋ฌด์์ผ๋ก ํ ๊ฒ์ธ์ง.. , ํ๋ก์ ํธ์ ์ด๋ฆ์ ๋ฌด์์ธ์ง.. ๋ฑ๋ฑ )
ํ์ฌ ๋๋ ํ ๋ฆฌ ๊ตฌ์กฐ
Cmake-test
- main.cpp
- (CmakeLists.txt๋ฅผ ์์ฑํด์ผํ ์์น)
CMakeLists.txt
# CMakeLists.txt
project(hello_world)
add_executable(main main.cpp)
์์ CMakeLists.txt๋ ๋น๋๋ฅผ ํ๊ธฐ์ํ ์์ฃผ ๊ธฐ๋ณธ์ ์ธ ๋ด์ฉ๋ค์ ๋ด๊ณ ์๋ค.
project(hello_world)
- project ๋ช ์ ํตํด ํ๋ก์ ํธ์ ์ด๋ฆ, ๋ฒ์ , ์ธ์ด ๋ฑ์ ์ธ์ํ ์ ์๋๋ก ์ง์ ํด์ค ์ ์๋ค.
- project๋ฅผ ์ง์ ํ์ง์๋๋ค๊ณ ํด์ ์ปดํ์ผ์ด ์๋๋๊ฒ์ ์๋์ง๋ง ํธํ์ฑ์ด๋ ์์กด์ฑ ๋ฑ์ผ๋ก ์ธํด ์ง์ ํ๋๊ฒ์ด ๊ถ์ฅ๋๋ค.
add_executable(main main.cpp)
- ์ด๋ ํ ํ์ผ์ ๋น๋ํญ๋ชฉ์ ํฌํจ์ํฌ ๊ฒ์ด๊ณ ์คํํ์ผ์ ์ด๋ฆ์ ๋ฌด์์ผ๋ก ํ ๊ฒ์ธ์ง ์ง์ ํด์ค๋ค.
- ์์ ๊ฐ์ ๊ฒฝ์ฐ๋ ์คํํ์ผ์ ์ด๋ฆ์ main ์ผ๋ก ํ ๊ฒ์ด๊ณ ๋น๋ ํ์ผ๋ค ํญ๋ชฉ์๋ main.cpp๋ง ํฌํจํ๋ค๋ ๋ด์ฉ์ ๋ด๊ณ ์๋ค.
( ์ถ๊ฐ์ ์ผ๋ก ํ์ผ๋ค์ ๋ฃ์ด์ค์ผํ๋ ์ํฉ์ด๋ผ๋ฉด ๋ค์ฌ์ฐ๊ธฐ๋ฅผ ํตํด ์ฐ๋๊ฒ์ด ๊ฐ๋ฅํ๋ค. )
ex)
add_executable(main
main.cpp
test.cpp
)
๋น๋ ํ ์คํํด๋ณด๊ธฐ
์ด์ ๋ CMakeLists.txt ์ ๋ฐํ์ผ๋ก CMake ํ ๋ด์ฉ๋ค์ด ๋ด๊ธธ ๋๋ ํ ๋ฆฌ๋ฅผ ์ง์ ํ ์์ฑํด์ค์ผํ๋ค.
main.cpp ํ์ผ์ด ์๋ ์์น์์ ๋ฐ๋ก ์์ฑ์ ํด๋ ๋๊ธดํ์ง๋ง ๊ทธ๋ฌ๋ฉด ๋๋ ํ ๋ฆฌ ๊ตฌ์กฐ๊ฐ ๋๋ฌด ์ง์ ๋ถํด์ง๊ธฐ๋๋ฌธ์ CMake build ๋ด์ฉ๋ค์ด ๋ด๊ธธ ๋๋ ํ ๋ฆฌ๋ฅผ ์์ฑํ๋ ๊ฒ์ ๊ถ์ฅํ๋ค.
๋๋ ํ ๋ฆฌ๊ตฌ์กฐ
Cmake-test
- main.cpp
- CmakeLists.txt
- (build ๋๋ ํ ๋ฆฌ ์์ฑ์์น)
๋ณดํธ์ ์ผ๋ก๋ ์์๊ฐ์ด CmakeLists.txt ํ์ผ์ด ์๋ ์์น์ ๋๋ ํ ๋ฆฌ๋ฅผ ์์ฑํ์ฌ ๋ด์ฉ๋ค์ ๋ด๋ํธ์ด๋ค.
<< mkdir build
(๋๋ ํ ๋ฆฌ์ ์ด๋ฆ์ ๋ค๋ฅธ๊ฒ์ผ๋ก ํด๋ ์๊ด X)
<< cd build
<< cmake ..
(build ๋๋ ํ ๋ฆฌ ์์ ์์ผ๋ cmake ๋ช
๋ น์ด๋ .. ๊ฒฝ๋ก์ ์๋ cmakelists.txt ๋ฅผ ์ฐธ์กฐํ๊ฒ ๋ค๋ ์๋ฏธ)
<< ls
CMakeCache.txt CMakeFiles Makefile cmake_install.cmake
<< make
<< ./main
>> HELLO WORLD!!
CMake์์๋ ์ด๊ฒ๋ฟ์๋๋ผ ๋ค์ํ๊ฒ ๋น๋ ์ ์ด๋ ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ๊ฐ์ด ๋น๋ํ ๊ฒ์ธ์ง, CMake๋ฅผ ํ ๋ ค๋ฉด ํ์ํ ์ต์ ๋ฒ์ ๋ฑ๋ฑ ๋ค์ํ ์กฐ๊ฑด๋ค์ ์ถ๊ฐํ ์ ์๋ค.
์ฐธ๊ณ ์๋ฃ
https://gist.github.com/luncliff/6e2d4eb7ca29a0afd5b592f72b80cb5c?permalink_comment_id=3300758