You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* The main **advantage** of this example is that it is _auto-generated_.
13
+
You only need to change the _project name_, and add the files that need to
14
+
be compiled in [foo/CMakeLists.txt](foo/CMakeLists.txt).
15
+
16
+
* Autogenetared library version file: `#include <foo/version.h>`
17
+
18
+
*`FOO_DEBUG` added on Debug. See [foo/foo.cpp#L7-L11](foo/foo.cpp#L7-L11).
19
+
20
+
*`CMAKE_DEBUG_POSTFIX = 'd'` (allowing `Debug` and `Release` to not collide).
21
+
See [cmake/SetEnv.cmake#L17](cmake/SetEnv.cmake#L17).
22
+
23
+
* Static library as default (`BUILD_SHARED_LIBS=OFF`).
24
+
See [cmake/SetEnv.cmake#L19-L21](cmake/SetEnv.cmake#L19-L21).
25
+
26
+
*`CMAKE_BUILD_TYPE` possible options in `cmake-gui`.
27
+
For multi-config generator, `CMAKE_CONFIGURATION_TYPES` is set instead of
28
+
`CMAKE_BUILD_TYPE`.
29
+
See [cmake/SetEnv.cmake#L23-L48](cmake/SetEnv.cmake#L23-L48).
30
+
31
+
*`Uninstall` target.
32
+
See [cmake/SetEnv.cmake#L104-L109](cmake/SetEnv.cmake#L104-L109).
33
+
34
+
* Always full RPATH (for shared libraries).
35
+
See [cmake/SetEnv.cmake#L111-L132](cmake/SetEnv.cmake#L111-L132).
36
+
37
+
38
+
### Usage
39
+
40
+
Once the library is installed (see
41
+
"[how to compile?](https://github.com/pablospe/cmake-example-library/tree/moderm_cmake#how-to-compile)"), it can be found externally with
42
+
`find_package(...)`:
43
+
44
+
find_package(Foo <VERSION> REQUIRED)
45
+
target_link_libraries(... Foo::foo)
46
+
47
+
See [more details](https://github.com/pablospe/cmake-example-library/tree/moderm_cmake#how-to-use-the-library-as-dependency-in-an-external-project) below.
48
+
17
49
18
50
### How to create a library from this example?
19
51
@@ -25,12 +57,14 @@ Follow these steps:
25
57
26
58
* Change project name in the top-level `CMakeLists.txt`.
27
59
60
+
* Add `.cpp` and `.h` files in `foo/CMakeLists.txt`.
61
+
28
62
*[Optional] Set variables: `LIBRARY_NAME` and `LIBRARY_FOLDER`.
29
63
If it is not set explicitally, project name in lowercase will be used.
30
64
See `cmake/SetEnv.cmake` file to see the implementation details.
31
65
32
-
*[Optional] 'example_internal/' folder can be removed, it is the 'bar' example.
33
-
In this case, remove the 'add_subdirectory(example_internal)' too.
66
+
*[Optional] 'example_internal/' folder can be removed, it is the 'bar'
67
+
example. In this case, remove the 'add_subdirectory(example_internal)' too.
34
68
35
69
### How to compile?
36
70
@@ -43,14 +77,14 @@ Assume the following settings:
43
77
44
78
Example of a local installation:
45
79
46
-
> mkdir build
47
-
> cd build
48
-
> cmake -DCMAKE_INSTALL_PREFIX=../installed ..
49
-
> make install
80
+
> mkdir _build && cd _build
81
+
> cmake -DCMAKE_INSTALL_PREFIX=../_install ..
82
+
> cmake --build . --target install -j 8
83
+
(equivalent to 'make install -j8' in linux)
50
84
51
85
Installed files:
52
86
53
-
> tree ../installed
87
+
> tree ../_install
54
88
55
89
├── bin
56
90
│ └── bar
@@ -59,32 +93,92 @@ Installed files:
59
93
│ ├── foo.h
60
94
│ └── version.h
61
95
└── lib
62
-
├── CMake
96
+
├── cmake
63
97
│ └── Foo
64
98
│ ├── FooConfig.cmake
65
99
│ ├── FooConfigVersion.cmake
66
100
│ ├── FooTargets.cmake
67
-
│ └── FooTargets-noconfig.cmake
68
-
└── libfoo.so
101
+
│ ├── FooTargets-debug.cmake
102
+
│ └── FooTargets-release.cmake
103
+
├── libfoo.a (Release)
104
+
└── libfood.a (Debug)
69
105
70
106
Uninstall library:
71
107
72
108
> make uninstall
73
109
110
+
111
+
#### Compilation scripts
112
+
113
+
Please check the following files for more complex compilation examples:
114
+
-[build_linux.sh](build_linux.sh)
115
+
-[build_win.sh](build_win.sh)
116
+
-[build_ninja.sh](build_ninja.sh)
117
+
118
+
119
+
### Static vs Shared
120
+
121
+
By default, a static library will be generated. Modify `BUILD_SHARED_LIBS` in
122
+
order to change this behavior. For example,
123
+
124
+
> cd _build/
125
+
> cmake -DBUILD_SHARED_LIBS=ON ..
126
+
127
+
128
+
74
129
### How to use the library (internally in subfolders)?
75
130
76
131
See the [example of internal subfolder](example_internal/).
77
132
133
+
78
134
### How to use the library (as dependency) in an external project?
79
135
80
-
cmake_minimum_required(VERSION 2.6)
136
+
See the [example of external project](example_external/).
137
+
Once the library is intalled, cmake would be able to find it using
138
+
`find_package(...)` command.
139
+
140
+
cmake_minimum_required(VERSION 3.9)
81
141
project(Bar)
82
142
83
-
find_package(Foo REQUIRED)
84
-
include_directories(${FOO_INCLUDE_DIRS})
143
+
find_package(Foo 1.2.3 REQUIRED)
85
144
86
145
add_executable(bar bar.cpp)
87
-
target_link_libraries(bar ${FOO_LIBRARIES})
146
+
target_link_libraries(bar PRIVATE Foo::foo)
88
147
89
-
See the [example of external project](example_external/).
148
+
Requirements will propagate automatically:
149
+
*`Foo::foo` will link automatically,
150
+
* headers can be included by C++ code like `#include <foo/foo.h>`,
151
+
*`FOO_DEBUG=1` added on Debug,
152
+
*`FOO_DEBUG=0` added otherwise.
153
+
154
+
155
+
### How to use the library as submodule (using add_subdirectory)?
156
+
157
+
If `Foo` library is intended to be use as a Git submodule:
[Git, CMake, Conan - How to ship and reuse our C++ projects](https://www.youtube.com/watch?v=S4QSKLXdTtA)
0 commit comments