Skip to content

Commit a8fd99f

Browse files
committed
Add CMAKE_README
1 parent fb3be71 commit a8fd99f

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

CMAKE_README.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# How to use CMake
2+
3+
This document gives an overview of the basics of cmake and how to use
4+
it to make libraries and executables for `pod-embedded`.
5+
6+
## Cmake basics
7+
8+
CMake projects are specified using a `CMakeFiles.txt` in each
9+
library/exectutable directory.
10+
11+
### Basic CMakeFiles.txt layout
12+
A top level CMakeFiles.txt always contains a `cmake_minimum_required`
13+
and a `project` declaration. The rest is populated with `add_subdirectory`,
14+
calls, which include CMakeFiles.txts from subdirectories.
15+
16+
### Variables
17+
CMake varaibles are specified using `set(VARIABLE_NAME value...)`, which
18+
sets the variable `VARIABLE_NAME` to any subsequent values (multiple values
19+
will make a list.)
20+
21+
Eg:
22+
23+
```
24+
#set importantDirectory to /usr/local/bin
25+
set(importantDirectory /usr/local/bin)
26+
27+
#set libraries to list containing middleware, drivers, utils
28+
set(libraries middleware
29+
drivers
30+
utils)
31+
```
32+
33+
The fundametnal primitives when defining a cmake project are
34+
executables and libraries.
35+
36+
## Making a library
37+
38+
To create a library that is imported by other executables, we use the following
39+
40+
```
41+
#Define the project for the library
42+
project(foobarlib VERSION 1.0
43+
DESCRIPTION "The foobar library"
44+
LANGUAGES CXX)
45+
46+
#Define the library target called foobar with all of its source directories
47+
#Files specified with respect to CMakeLists.txt for the library
48+
add_library(foobar src/somefile.c
49+
src/bin.c
50+
src/baz.c)
51+
52+
#Add any header files by specifying the include directory, in this case
53+
#called include
54+
target_include_directories(foobar PUBLIC include)
55+
56+
#add any library dependencies
57+
target_link_libraries(foobar PUBLIC someotherlibrary verynicedependency)
58+
target_link_libraries(foobar PRIVATE thisonelibrary)
59+
60+
#export library to cmake file so it can be imported elsewhere
61+
export(TARGETS foobar FILE FoobarConfig.cmake)
62+
```
63+
64+
In this example we define a project called `foobarlib` in its own CMakeLists.txt
65+
and define a library called `foobar` with its respective source files.
66+
Then any header files are added by specifying an include directory with `target_include_directories(...)`. Then we link any other libraries `foobar` depends on with `target_link_libraries(...)`. Finally we export the library using `export(...)` so the library can be imported elsewhere in the project.
67+
68+
### PUBLIC vs PRIVATE vs INTERFACE
69+
Whenever a dependency is specified for a project, you must specify if that dependency
70+
is `PUBLIC`, `PRIVATE` or `INTERFACE`. This changes the availability of the dependencies
71+
you're adding to any project that depends on the library you're writing.
72+
73+
For example, if library `foo` depends on library `bar` publically, then
74+
any library that depends on `foo` will be linked with headers from `bar`.
75+
76+
The rule of thumb is:
77+
78+
* If your source files depend on the library, make it `PRIVATE`.
79+
* If your header files depend on the library, make it `INTERFACE`
80+
* If both of the above are true, make it `PUBLIC`
81+
82+
Dependencies for executables should always be `PRIVATE`.
83+
84+
## Making an executable
85+
86+
To make an executable, simply use
87+
88+
```
89+
add_executable(binaryName, sourceFile1.c
90+
sourceFile2.c
91+
sourceFileN.c)
92+
```
93+
94+
and specify includes and dependencies with `target_include_directories(...)` and `target_link_libraries(...)` in the same way as with libraries.
95+

0 commit comments

Comments
 (0)