Coder Social home page Coder Social logo

cmake-library-example's Introduction

Building a library with CMake

This example shows how to build a library with CMake.

The library, foo, can either be built from source or distributed pre-built.

Building with foo from source

This is the default. The regular CMake way:

mkdir build && cd build
cmake .. 
make -j
./cmake-library-example

Building with pre-built foo

Sometimes, you might want to distribute the functionality of a program, but not its source. Distributing its headers and pre-compiled library might be helpful here.

How does this work? The library has its own CMakeLists that specifies the appropriate add_library() commands. The CMakeLists.txt file declares what the library provides through install() commands. Now, we can install the built library in a local path (the prefix). Incidentally, we can choose the CMAKE_INSTALL_PREFIX to be foo's source directory. Now, the depending program can use find_package(foo) when it sets the CMAKE_PREFIX_PATH to include the install prefix of the library (which we chose to be the same as its source directory).

After installation of foo:

cmake-library-example
├── build (intermediate build directory)
└── foo-component (foo library, also install prefix)
    ├── include (library headers)
    └── lib (pre-compiled library)
        └── cmake (cmake config files)
            └── foo

First step: Typically, foo will be compiled on a machine that has access to its sourcecode.

mkdir -p build/foo-component && cd build/foo-component
cmake ../../foo-component -DCMAKE_INSTALL_PREFIX=../../foo-component
make -j install
# or run the convenience script
./build-foo.sh

Second step: Then, the library is distributed with the original foo source redacted, and the foo-component/lib directory included.

mkdir build && cd build
cmake .. -DWITH_PREBUILT_FOO=1
make -j
./cmake-library-example

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.