add_executable 을 사용하여 프로젝트에 포함 시킬 소스파일을 추가할 수 있다.
// 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
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".
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) # 파일 위치를 달리 하여도 문제 발생