CMakeを用いたC++のHello Worldプログラムの実行
はじめに
まずは能書き垂れる前に、サンプルソースをさっさと動かします。
環境構築
Ubuntu18.04LTS
# install cmake package
sudo apt-get install cmake準備するファイル
~/sample/
    |-CMakeLists.txt
    |-main.cpp
    |-hello.hpp
    |-hello.cpp
# mkdir project dir under ~/ 
mkdir sample
cd sample
touch CMakeLists.txt main.cpp hello.hpp hello.cpp# CMake Version
cmake_minimum_required(VERSION 2.8)
# Project name & Language
project(helloworld CXX)
# source file & generated file
add_executable(a.out main.cpp hello.cpp)#include "hello.hpp"
int main() {
    hello();
}#ifndef HELLO_H
#define HELLO_H
void hello();
#endif#include <iostream>
#include "hello.hpp"
void hello() {
    std::cout << "Hello!" << std::endl;
}コンパイル
コンパイルします。
mkdir build
cd build
cmake ..
make実行結果です。
$:~/sample/build$ make
Scanning dependencies of target hello
[ 33%] Building CXX object CMakeFiles/hello.dir/main.cpp.o
[ 66%] Linking CXX executable hello
[100%] Built target hello
実行後、こんなにたくさんファイルができました。
~$ tree sample/
sample/
├── build
│   ├── CMakeCache.txt
│   ├── CMakeFiles
│   │   ├── 3.10.2
│   │   │   ├── CMakeCXXCompiler.cmake
│   │   │   ├── CMakeDetermineCompilerABI_CXX.bin
│   │   │   ├── CMakeSystem.cmake
│   │   │   └── CompilerIdCXX
│   │   │       ├── a.out
│   │   │       ├── CMakeCXXCompilerId.cpp
│   │   │       └── tmp
│   │   ├── cmake.check_cache
│   │   ├── CMakeDirectoryInformation.cmake
│   │   ├── CMakeOutput.log
│   │   ├── CMakeTmp
│   │   ├── feature_tests.bin
│   │   ├── feature_tests.cxx
│   │   ├── hello.dir
│   │   │   ├── build.make
│   │   │   ├── cmake_clean.cmake
│   │   │   ├── CXX.includecache
│   │   │   ├── DependInfo.cmake
│   │   │   ├── depend.internal
│   │   │   ├── depend.make
│   │   │   ├── flags.make
│   │   │   ├── hello.cpp.o
│   │   │   ├── link.txt
│   │   │   ├── main.cpp.o
│   │   │   └── progress.make
│   │   ├── Makefile2
│   │   ├── Makefile.cmake
│   │   ├── progress.marks
│   │   └── TargetDirectories.txt
│   ├── cmake_install.cmake
│   ├── hello
│   └── Makefile
├── CMakeLists.txt
├── hello.cpp
├── hello.hpp
└── main.cpp
実行
~/sample/build$ ./hello
Hello World !