CMake - add_executable

mohadang·2022년 8월 13일
0

CMake

목록 보기
20/24
post-thumbnail

add_executable 을 사용하여 프로젝트에 포함 시킬 소스파일을 추가할 수 있다.

CMake 버전의 Hello World

// main.cc

#include <iostream>

int main() {
  std::cout << "hello" << std::endl;
  return 0;
}
# CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
project(foo)

add_executable(foo main.cc)
$ cmake -B _build
$ cmake --build _build
$ _build/foo
hello

add_executable 중복 추가 불가 1

cmake_minimum_required(VERSION 2.8)
project(foo)

add_executable(foo main.cc)
add_executable(foo a.cc)    # 문제 발생, foo는 이미 추가 했기에 또다시 추가 불가능
red@DESKTOP-G15ND3V:~/cgold$ cmake -B _build
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Error at CMakeLists.txt:5 (add_executable):
  add_executable cannot create target "foo" \ # 이미 추가되었음
  because another target with the    
  
  same name already exists.  The existing target is an executable created in
  source directory "/home/red/cgold".  See documentation for policy CMP0002
  for more details.


-- Configuring incomplete, errors occurred!
See also "/home/red/cgold/_build/CMakeFiles/CMakeOutput.log".

add_executable 중복 추가 불가 2

  • 파일 위치를 달리 하여도 문제는 여전하다
cmake_minimum_required(VERSION 2.8)
project(foo)

add_subdirectory(boo)
add_subdirectory(bar)
# boo/CMakeLists.txt

add_executable(foo main.cpp)    # 파일 위치를 달리 하여도 문제 발생
# bar/CMakeLists.txt

add_executable(foo main.cpp)    # 파일 위치를 달리 하여도 문제 발생
profile
mohadang

0개의 댓글