Demo
1 | . |
main.c1
2
3
4
5
6
7
8#include <stdio.h>
#include "testFunc.h"
int main(void)
{
printf("Hello World\n");
return 0;
}
CMakeLists.txt1
2
3
4
5cmake_minimum_required (VERSION 2.8)
project (demo)
add_executable(main main.c)
1 | [root@8bdf5f3b23af demo]# cmake . |
command generated MakeFile
, and some auto generate file.1
2
3
4
5
6
7.
|-- CMakeCache.txt
|-- CMakeFiles
|-- CMakeLists.txt
|-- Makefile
|-- cmake_install.cmake
`-- main.c
1 | [root@8bdf5f3b23af demo]# make |
make and execute
1 | [root@8bdf5f3b23af demo]# ./main |
Multi-source filie under one directory
1 | . |
testFunc.c1
2
3
4
5
6
7
8
9
10
11/*
* testFunc.c
*/
#include <stdio.h>
#include "testFunc.h"
void func(int data)
{
printf("data is %d\n", data);
}
testFunc.h1
2
3
4
5
6
7
8
9
10/*
* testFunc.h
*/
#ifndef _TEST_FUNC_H_
#define _TEST_FUNC_H_
void func(int data);
#endif
main.c1
2
3
4
5
6
7
8#include <stdio.h>
#include "testFunc.h"
int main(void)
{
func(100);
return 0;
}
CMakeLists.txt1
2
3
4
5cmake_minimum_required (VERSION 2.8)
project (demo)
add_executable(main main.c testFunc.c)
add testFunc.c
to executable param
make and execute
1 | [root@8bdf5f3b23af multsource]# ./main |
if we have a lot of source file, add_executable
is troublesome.aux_source_directory
can take files under directory as a source.
add another function1
2
3
4
5
6
7
8.
|-- CMakeLists.txt
|-- main
|-- main.c
|-- testFunc.c
|-- testFunc.h
|-- testFunc1.c
`-- testFunc1.h
modify CMakeLists1
2
3
4
5
6
7cmake_minimum_required (VERSION 2.8)
project (demo)
aux_source_directory(. SRC_LIST)
add_executable(main ${SRC_LIST})
make and execute1
2
3[root@8bdf5f3b23af multsource1]# ./main
data is 100
data is 200
Multi-source filie under multi-directory
1 | . |
group function by two directory test_func
test_func1
modify CMakeLists.txt
1
2
3
4
5
6
7
8
9
10cmake_minimum_required (VERSION 2.8)
project (demo)
include_directories (test_func test_func1)
aux_source_directory (test_func SRC_LIST)
aux_source_directory (test_func1 SRC_LIST1)
add_executable (main main.c ${SRC_LIST} ${SRC_LIST1})
result is same as above.
Formal structure
1 | . |
- put source file to
src
- put head file to
include
build
used to store middle filebin
used to store executable file
outside CMakeLists.txt
1
2
3
4
5cmake_minimum_required (VERSION 2.8)
project (demo)
add_subdirectory (src)
inside CMakeLists.txt
1
2
3
4
5
6
7aux_source_directory (. SRC_LIST)
include_directories (../include)
add_executable (main ${SRC_LIST})
set (EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
EXECUTABLE_OUTPUT_PATH
: executable file pathPROJECT_SOURCE_DIR
: project root
result is same as above.
Dynamic and static libraries
1 | . |
outside CMakeLists.txt
1
2
3
4
5cmake_minimum_required (VERSION 2.8)
project (demo)
add_subdirectory (lib_testFunc)
inside CMakeLists.txt
1
2
3
4
5
6
7
8
9aux_source_directory (. SRC_LIST)
add_library (testFunc_shared SHARED ${SRC_LIST})
add_library (testFunc_static STATIC ${SRC_LIST})
set_target_properties (testFunc_shared PROPERTIES OUTPUT_NAME "testFunc")
set_target_properties (testFunc_static PROPERTIES OUTPUT_NAME "testFunc")
set (LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
add_library
: generate dynamic or static libraries, (1. libname; 2. dynamic or static, static in default; 3. source file path)set_target_properties
: setting output name, can set other options like lib versionLIBRARY_OUTPUT_PATH
: default path of library file
in build
run cmake .
and make
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15.
|-- CMakeLists.txt
|-- build
| |-- CMakeCache.txt
| |-- CMakeFiles
| |-- Makefile
| |-- cmake_install.cmake
| `-- lib_testFunc
|-- lib
| |-- libtestFunc.a
| `-- libtestFunc.so
`-- lib_testFunc
|-- CMakeLists.txt
|-- testFunc.c
`-- testFunc.h
Link by libraries
1 | . |
outside CMakeLists.txt
1
2
3
4
5
6
7cmake_minimum_required (VERSION 2.8)
project (demo)
add_subdirectory (lib_testFunc)
add_subdirectory (src)
inside CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12aux_source_directory (. SRC_LIST)
# find testFunc.h
include_directories (../lib_testFunc)
link_directories (${PROJECT_SOURCE_DIR}/lib)
add_executable (main ${SRC_LIST})
target_link_libraries (main testFunc)
set (EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
link_directories
: Add non-standard shared library search pathtarget_link_libraries
: Link the target file with the library file
Compile options
1 | . |
main.cpp1
2
3
4
5
6
7
8#include <iostream>
int main(void)
{
auto data = 100;
std::cout << "data: " << data << "\n";
return 0;
}
CMakeLists.txt1
2
3
4
5
6
7
8
9cmake_minimum_required (VERSION 2.8)
project (demo)
set (EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
add_compile_options(-std=c++11 -Wall)
add_executable(main main.cpp)
Compile control
1 | . |
outside CMakeLists.txt
1
2
3
4
5
6
7
8
9cmake_minimum_required(VERSION 2.8)
project(demo)
option(MYDEBUG "enable debug compilation" OFF)
set (EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
add_subdirectory(src)
add an option MYDEBUG
inside CMakeLists.txt
1
2
3
4
5
6
7
8
9cmake_minimum_required (VERSION 2.8)
add_executable(main1 main1.c)
if (MYDEBUG)
add_executable(main2 main2.c)
else()
message(STATUS "Currently is not in debug mode")
endif()
user MYDEBUG
to decide whether or not compile main2
cmake .. -DMYDEBUG=ON && make
1 | . |