First we can specify the directories of header files by include_directories()
, then we need to specify the corresponding source files of the target executable by add_executable()
, and be sure there's exactly one main()
function in the source files.
Following is a simple example, all the files are assumed placed in the directory PROJECT_SOURCE_DIR
.
main.cpp
#include "foo.h"
int main()
{
foo();
return 0;
}
foo.h
void foo();
foo.cpp
#include <iostream>
#include "foo.h"
void foo()
{
std::cout << "Hello World!\n";
}
CMakeLists.txt
cmake_minimum_required(VERSION 2.4)
project(hello_world)
include_directories(${PROJECT_SOURCE_DIR})
add_executable(app main.cpp foo.cpp) # be sure there's exactly one main() function in the source files
We can follow the same procedure in the above example to build our project. Then executing app
will print
>./app
Hello World!