All Projects → pablospe → Cmake Example Library

pablospe / Cmake Example Library

Licence: mit
How to install a library with cmake

Labels

Projects that are alternatives of or similar to Cmake Example Library

Kinectazuredkprogramming
Samples about Kinect Azure DK programming
Stars: ✭ 92 (-8.91%)
Mutual labels:  cmake
Cqxq
让XQ支持CQ插件
Stars: ✭ 97 (-3.96%)
Mutual labels:  cmake
Gr Paint
An OFDM Spectrum Painter for GNU Radio
Stars: ✭ 99 (-1.98%)
Mutual labels:  cmake
Dcmjs
dcmjs is a javascript cross-compile of dcmtk (dcmtk.org).
Stars: ✭ 92 (-8.91%)
Mutual labels:  cmake
Gr Tutorial
A tutorial OOT module for GNU Radio
Stars: ✭ 96 (-4.95%)
Mutual labels:  cmake
Cgcmake
CMake modules for common applications related to computer graphics
Stars: ✭ 98 (-2.97%)
Mutual labels:  cmake
Raspi Toolchain
Stars: ✭ 90 (-10.89%)
Mutual labels:  cmake
Vfxcmake
Cmake Find modules for common vfx software, and general Cmake utility code
Stars: ✭ 100 (-0.99%)
Mutual labels:  cmake
Avp Slam Sim
A basic implementation(not official code) of AVP-SLAM(IROS 2020) in simulation. https://arxiv.org/abs/2007.01813
Stars: ✭ 97 (-3.96%)
Mutual labels:  cmake
Samples
DEPRECATED. MediaSDK samples. Main development repo located at https://github.com/Intel-Media-SDK/MediaSDK/tree/master/samples
Stars: ✭ 99 (-1.98%)
Mutual labels:  cmake
Kindd
A kindful dd, written in qt-quick.
Stars: ✭ 93 (-7.92%)
Mutual labels:  cmake
Examples
VLC-Qt Examples
Stars: ✭ 96 (-4.95%)
Mutual labels:  cmake
Mdspan
Production-quality reference implementation of mdspan
Stars: ✭ 99 (-1.98%)
Mutual labels:  cmake
Webdav Client Cpp
☁️ C++ WebDAV Client provides easy and convenient to work with WebDAV-servers.
Stars: ✭ 92 (-8.91%)
Mutual labels:  cmake
Openrct2
An open source re-implementation of RollerCoaster Tycoon 2 🎢
Stars: ✭ 10,115 (+9914.85%)
Mutual labels:  cmake
Hatchit
A Free and Open Source 3D Game Engine written in C++
Stars: ✭ 91 (-9.9%)
Mutual labels:  cmake
Cmake fortran template
A template directory structure for a Fortran project using CMake as the build system.
Stars: ✭ 97 (-3.96%)
Mutual labels:  cmake
Covise
Collaborative Visualization and Simulation Environment, OpenCOVER and OddLOT
Stars: ✭ 101 (+0%)
Mutual labels:  cmake
Qv2ray
⭐ Linux / Windows / macOS 跨平台 V2Ray 客户端 | 支持 VMess / VLESS / SSR / Trojan / Trojan-Go / NaiveProxy / HTTP / HTTPS / SOCKS5 | 使用 C++ / Qt 开发 | 可拓展插件式设计 ⭐
Stars: ✭ 12,886 (+12658.42%)
Mutual labels:  cmake
Jni By Examples
🎇Fun Java JNI By Examples - with CMake and C++ (or C, of course!) ‼️ Accepting PRs
Stars: ✭ 99 (-1.98%)
Mutual labels:  cmake

cmake-example-library

CMake library example that can be found using find_package().

Update: now using modern cmake (version >= 3.9), since commit 46f0b93.

Features

  • The main advantage of this example is that it is auto-generated. You only need to change the project name, and add the files that need to be compiled in foo/CMakeLists.txt.

  • Autogenetared library version file: #include <foo/version.h>

  • FOO_DEBUG added on Debug. See foo/foo.cpp#L7-L11.

  • CMAKE_DEBUG_POSTFIX = 'd' (allowing Debug and Release to not collide). See cmake/SetEnv.cmake#L17.

  • Static library as default (BUILD_SHARED_LIBS=OFF). See cmake/SetEnv.cmake#L19-L21.

  • cmake-gui shows possible options for CMAKE_BUILD_TYPE (Release, Debug, etc.). For multi-config generator, CMAKE_CONFIGURATION_TYPES is set instead of CMAKE_BUILD_TYPE. See cmake/SetEnv.cmake#L23-L48.

  • Uninstall target. See cmake/SetEnv.cmake#L104-L109.

  • Always full RPATH (for shared libraries). See cmake/SetEnv.cmake#L111-L132.

  • CMake Registry: export package to CMake registry such that it can be easily found by CMake even if it has not been installed to a standard directory. See cmake/SetEnv.cmake#L135. Possible options:

    -DCMAKE_REGISTRY_FOLDER="OFF"            (disable CMake registry [default])
    -DCMAKE_REGISTRY_FOLDER="INSTALL_FOLDER"
    -DCMAKE_REGISTRY_FOLDER="BUILD_FOLDER"
    

Usage

In this example, Foo is the library and Bar a binary (which uses the library).

find_package(Foo <VERSION> REQUIRED)
target_link_libraries(... Foo::foo)

See more details below.

How to compile?

Assume the following settings:

project(Foo)
...
set(LIBRARY_NAME foo)   # [optional] generated automatically (in lowercase)
set(LIBRARY_FOLDER foo) # [optional] generated automatically (in lowercase)

Example of a local installation:

> mkdir _build && cd _build
> cmake -DCMAKE_INSTALL_PREFIX=../_install ..
> cmake --build . --target install -j 8
  (equivalent to  'make install -j8' in linux)

Installed files:

> tree ../_install

├── bin
│   └── bar
├── include
│   └── foo
│       ├── foo.h
│       └── version.h
└── lib
    ├── cmake
    │   └── Foo
    │       ├── FooConfig.cmake
    │       ├── FooConfigVersion.cmake
    │       ├── FooTargets.cmake
    │       ├── FooTargets-debug.cmake
    │       └── FooTargets-release.cmake
    ├── libfoo.a                             (Release)
    └── libfood.a                            (Debug)

Uninstall library:

> make uninstall

Compilation scripts

Please check the following files for more complex compilation examples:

Static vs Shared

By default, a static library will be generated. Modify BUILD_SHARED_LIBS in order to change this behavior. For example,

> cd _build/
> cmake -DBUILD_SHARED_LIBS=ON ..

How to use the library (internally in subfolders)?

See the example of internal subfolder.

How to use the library (as dependency) in an external project?

See the example of external project. Once the library is intalled, cmake would be able to find it using find_package(...) command.

cmake_minimum_required(VERSION 3.9)
project(Bar)

find_package(Foo 1.2.3 REQUIRED)

add_executable(bar bar.cpp)
target_link_libraries(bar PRIVATE Foo::foo)

Requirements will propagate automatically:

  • Foo::foo will link automatically,
  • headers can be included by C++ code like #include <foo/foo.h>,
  • FOO_DEBUG=1 added on Debug,
  • FOO_DEBUG=0 added otherwise.

How to use the library as submodule (using add_subdirectory)?

If Foo library is intended to be use as a Git submodule:

> git submodule add https://github.com/<user>/Foo Foo

In the CMakeLists.txt where the Foo submodule will be used, add the command add_subdirectory(...):

[...]

# Add 'Foo' library as submodule
add_subdirectory(Foo)

# Propagate usage requirements from Foo linked library
target_link_libraries(<target> PRIVATE Foo::foo)

[...]

How to create a library from this example?

Follow these steps:

  • Copy files in a new folder, or clone with git:

    git clone [email protected]:pablospe/cmake-example-library.git

  • Change project name in the top-level CMakeLists.txt.

  • Add .cpp and .h files in foo/CMakeLists.txt.

  • [Optional] Set variables: LIBRARY_NAME and LIBRARY_FOLDER. If it is not set explicitally, project name in lowercase will be used. See cmake/SetEnv.cmake file for implementation details.

  • [Optional] 'example_internal/' folder can be removed, it is the 'bar' example. In this case, remove the 'add_subdirectory(example_internal)' too.

Documentation

Some ideas from:

Modern CMake tutorials (youtube):

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].