CMake - 기초

markyang92·2024년 9월 3일
0

cmake

목록 보기
1/2
post-thumbnail
  • main.c
#include <stdio.h>

int main(void)
{
    printf("Hello, World!\n");
    return 0;
}

CMakeLists.txt

  • CMakeLists.txt에 기술한다.

cmake_minimum_required

cmake_minimum_required(VERSION 3.22)
  • cmake_minimum_required()
    • CMake minimum version 명시
$ cmake --version
cmake version 3.22.1

project

  • project()
    • 프로젝트 버전 명시
    • Languages 명시 가능
프로젝트의 이름을 설정합니다.

project(<PROJECT-NAME> [<language-name>...]) 
project(<PROJECT-NAME> [VERSION <major>[.<minor>[.<patch>[.<tweak>]]]] [DESCRIPTION <project-description-string>] [HOMEPAGE_URL <url-string>] [LANGUAGES <language-name>...])
    
    
project(CProject VERSION 1.0.0)
proejct(CProject VERSION 1.0.0 LANGUAGES C CXX)
  • 주의 C++CXX로 해야함
    • C++로 하면 CXX_COMPILER가 아닌 C++_COMPILER라고 없는 것 찾음

add_executable

지정된 소스 파일을 사용하여 프로젝트에 실행 파일을 추가합니다.

add_executable(<name> <options>... <sources>...) :target: normal Add an executable target called ``<name>`` to be built from the source files listed in the command invocation. The options are: ``WIN32`` Set the :prop_tgt:`WIN32_EXECUTABLE` target property automatically. See documentation of that target property for details. ``MACOSX_BUNDLE`` Set the :prop_tgt:`MACOSX_BUNDLE` target property automatically. See documentation of that target property for details. ``EXCLUDE_FROM_ALL`` Set the :prop_tgt:`EXCLUDE_FROM_ALL` target property automatically. See documentation of that target property for details.
add_executable(<name> IMPORTED [GLOBAL]) :target: IMPORTED Add an :ref:`IMPORTED executable target <Imported Targets>` to reference an executable file located outside the project. The target name may be referenced like any target built within the project, except that by default it is visible only in the directory in which it is created, and below. The options are: ``GLOBAL`` Make the target name globally visible.
add_executable(<name> ALIAS <target>) :target: ALIAS Creates an :ref:`Alias Target <Alias Targets>`, such that ``<name>`` can be used to refer to ``<target>`` in subsequent commands. The ``<name>`` does not appear in the generated buildsystem as a make target. The ``<target>`` may not be an ``ALIAS``.

add_executable(main main.cc)

빌드 테스트

  • CMakeLists.txt
cmake_minimum_required(VERSION 3.22)

project(CProject VERSION 1.0.0 LANGUAGES C C++)

add_executable(main main.cc)

빌드 수행

  • build 디렉토리 생성 후
$ cd build
$ cmake ..
# 상위 디렉토리에 있는 CMakeLists.txt 수행


  • 정확하게는 아래 처럼 할 수 있음
$ cmake -S <path-to-source> -B <path-to-build>
$ cmake -S .. -B . # option 1
$ cmake .. # option 2

  • Makefile 생성 확인

빌드 수행

--build <위치>: <위치>에 대해 빌드 수행

  • make 명령 쓰거나, $ cmake --build . 로 빌드 수행
$ cmake --build .


--build <위치> --target <target>: <target> 수행

  • make clean을 수행하고 싶다면?
    • --target clean
$ cmake --build . --target clean

업데이트

$ cmake . : update

  • CMake project를 파싱한적 있다면, 아래의 명령으로 업데이트 가능하다.
build $ cmake .
-- Configuring done
-- Generating done
-- Build files have been written to: /home/markyang/test-c/build

-G: Generator 설정

* Unix Makefiles               = Generates standard UNIX makefiles.
  Ninja                        = Generates build.ninja files.
  Ninja Multi-Config           = Generates build-<Config>.ninja files.
  Watcom WMake                 = Generates Watcom WMake makefiles.
  CodeBlocks - Ninja           = Generates CodeBlocks project files.
  CodeBlocks - Unix Makefiles  = Generates CodeBlocks project files.
  CodeLite - Ninja             = Generates CodeLite project files.
  CodeLite - Unix Makefiles    = Generates CodeLite project files.
  Eclipse CDT4 - Ninja         = Generates Eclipse CDT 4.0 project files.
  Eclipse CDT4 - Unix Makefiles= Generates Eclipse CDT 4.0 project files.
  Kate - Ninja                 = Generates Kate project files.
  Kate - Unix Makefiles        = Generates Kate project files.
  Sublime Text 2 - Ninja       = Generates Sublime Text 2 project files.
  Sublime Text 2 - Unix Makefiles
                               = Generates Sublime Text 2 project files.
  • Unix Makefiles
cd build
cmake -S .. -B . -G "Unix Makefiles" # option 1
cmake .. -G "Unix Makefiles" # option 2

  • MSVC
cd build
cmake -S .. -B . -G "Visual Studio 16 2019" # option 1
cmake .. -G "Visual Studio 16 2019" # option 2

  • Ninja
cd build
cmake -S .. -B . -G "Ninja" # option 1
cmake .. -G "Ninja" # option 2

profile
pllpokko@alumni.kaist.ac.kr

0개의 댓글