main.cpp
#include "hello.hpp"
int main(){
    hello();
}
hello.hpp
#ifndef HELLO_H
#define HELLO_H
void hello();
#endif
hello.cpp
#include <iostream>
#include "hello.hpp"
void hello() {
    std::cout << "Hello!" << std::endl;
}
### g++でビルドする場合
$ g++ -c main.cpp hello.cpp
$ g++ -o a.out main.o hello.o
$ ./a.out
Hello!
### CMake
CMakeLists.txtを作成する
CMakeLists.txt
# CMakeバージョン
cmake_minimum_required(VERSION 3.13)
# プロジェクト名と使用する言語
project(test_cmake CXX)
add_executable(a.out main.cpp hello.cpp)
CMakeを使ってビルドする際は、ビルド専用のディレクトリを作成し、その中で行う
$ ls
CMakeLists.txt  hello.cpp  hello.hpp  main.cpp
$ cmake -S . -B build
$ tree
.
├── build
│   ├── CMakeCache.txt
│   ├── CMakeFiles
│   │   ├── 3.22.1
│   │   │   ├── CMakeCXXCompiler.cmake
│   │   │   ├── CMakeDetermineCompilerABI_CXX.bin
│   │   │   ├── CMakeSystem.cmake
│   │   │   └── CompilerIdCXX
│   │   │       ├── a.out
│   │   │       ├── CMakeCXXCompilerId.cpp
│   │   │       └── tmp
│   │   ├── a.out.dir
│   │   │   ├── build.make
│   │   │   ├── cmake_clean.cmake
│   │   │   ├── compiler_depend.make
│   │   │   ├── compiler_depend.ts
│   │   │   ├── DependInfo.cmake
│   │   │   ├── depend.make
│   │   │   ├── flags.make
│   │   │   ├── link.txt
│   │   │   └── progress.make
│   │   ├── cmake.check_cache
│   │   ├── CMakeDirectoryInformation.cmake
│   │   ├── CMakeOutput.log
│   │   ├── CMakeTmp
│   │   ├── Makefile2
│   │   ├── Makefile.cmake
│   │   ├── progress.marks
│   │   └── TargetDirectories.txt
│   ├── cmake_install.cmake
│   └── Makefile
├── CMakeLists.txt
├── hello.cpp
├── hello.hpp
└── main.cpp