In the previous section, we learned the basics about how to create a CMake project with BLT, how to configure the project and how to build, and test BLT’s built-in third party libraries.
We now move on to creating CMake targets using two of BLT’s core macros: blt_add_library and blt_add_executable.
We begin with a simple executable that calculates by numerical integration,
example_1. We then extract that code into a library, which we link
into a new executable,
Example 1: Stand-alone Executable¶
This example is as basic as it gets. After setting up a BLT CMake project, like the Bare Bones project in the previous section, we can start using BLT’s macros.
Creating a stand-alone executable is as simple as calling the following macro:
blt_add_executable( NAME example_1 SOURCES example_1.cpp )
This tells CMake to create an executable, named
example_1, with one source file,
You can create this project yourself or you can run the already provided
tutorial/calc_pi project. For ease of use, we have combined many examples
into this one CMake project. You can create the executable
by running the following commands:
cd <BLT repository>/docs/tutorial/calc_pi mkdir build cd build cmake -DBLT_SOURCE_DIR=../../.. .. make
This is one of the core macros that enables BLT to simplify our CMake-based project. It unifies many CMake calls into one easy to use macro while creating a CMake executable target with the given parameters. It also greatly simplifies the usage of internal and external dependencies. The full list of supported parameters can be found on the blt_add_executable API documentation.
Example 2: Executable with a Library¶
This example is a bit more exciting. This time we are creating a library that calculates the value of pi and then linking that library into an executable.
First, we create the library with the following BLT code:
blt_add_library( NAME calc_pi HEADERS calc_pi.hpp calc_pi_exports.h SOURCES calc_pi.cpp )
Just like before, this creates a CMake library target that will get built to
Next, we create an executable named
example_2 and link in the previously
created library target:
blt_add_executable( NAME example_2 SOURCES example_2.cpp DEPENDS_ON calc_pi)
DEPENDS_ON parameter properly links the previously defined library
into this executable without any more work or extra CMake function calls.
This is another core BLT macro. It creates a CMake library target and associates
the given sources and headers along with handling dependencies the same way as
blt_add_executable does. It defaults to building a static library unless
you override it with
SHARED or with the global CMake option
The full list of supported parameters can be found on the blt_add_library