Installing using CMake¶
In this tutorial you will learn
- How to build and install software with CMake
- How to make self-installed libraries and binaries available
- How to create your own module for a self-installed application
CMake is an extensible, open-source software build system. CMake uses simple
CMakeLists.txt configuration files placed in each source directory to
generate standard Makefile build files. CMake generates a native build
environment that will compile source code, create libraries, generate wrappers
and build executable binaries in arbitrary combinations.
Preparations¶
In this tutorial we'll use CMake to install a simple C++ Hello World
application.
-
It is recommended to use the fast local disk when compiling software on CSC supercomputers to move I/O load away from the parallel file system. Go there and download the source code from Allas:
-
Extract the package and move into the
hello-cmakedirectory: -
Most codes ship with a
READMEorINSTALLfile outlining the installation procedure. When compiling other codes, start by reading these carefully. In this case, theREADMEjust points to this tutorial page. -
Before compiling, one should ensure all the dependencies needed by the software are available. Remember to always first check whether these can be found on the CSC supercomputers as pre-installed modules. This example depends on the Boost library, so check if it is available:
-
Load the module with:
Building with CMake¶
When your build generates files, they have to go somewhere. An in-source
build puts them in your source tree, while an out-of-source build puts them
in a completely separate directory. The latter alternative, which is covered
below, is typically recommended, as you can build multiple variants of the code
in separate directories.
-
Load the
cmakemodule: -
Create and move to a
builddirectory in the root of the source code: -
It is recommended to install your own software under the
/projappldirectory of your project. The installation directory can be specified using the CMake flag-DCMAKE_INSTALL_PREFIX=<path to install dir>. CMake also requires you to specify the source code root directory, so run thecmakecommand as: -
If you get errors, try to fix the problems. For example, if you did not load the
boostmodule, you'll get an errorCould NOT find Boost. Often it is easiest to delete everything in thebuilddirectory and start from the beginning after fixing all issues. -
After running
cmake, runmaketo compile the application:makecan also be run in parallel. For example, to run using 8 cores, usemake -j 8. However, do not compile in parallel on the login node, but use an interactive session instead. See CSC's usage policy for details. -
Finally, install into the specified directory under
/projapplwith: -
Check what was installed and where:
Test the installed application¶
-
Your own software installations are not automatically added to your
PATH, which means that the installed binaries (commands) cannot be used without specifying the full path. To access the commands directly from anywhere, the directory containing the binaries should be added toPATH: -
Try to run the program by typing:
-
You probably got an error like:
-
This happens because the program depends on a library file
libhello.soand does not know where to look for it. This is a quite common issue with self-installed software. To fix the situation, add thelibpath to theLD_LIBRARY_PATHenvironment variable: -
Retry running the program. It should now work!
Bonus: Create your own module for the program¶
Users can create their own modules for self-installed applications. See the
"Module system" section for more details about
the CSC module system.
CSC uses the Lmod module system
in which the Lua language is used to write modulefiles.
-
Create a directory for your own modulefiles:
-
In this directory, create a directory for the "Hello World" program and move there:
-
Let's assume this is version 0.0.1 of the "Hello World" program. Create a file
0.0.1.luaand paste the contents below to it (using e.g.nano). Remember to change<project>and<user>to match your CSC project and username:-- This is an example modulefile for hello-cmake module -- Edit <project> and <user> below as needed to match the installation root directory local root = '/projappl/<project>/<user>/hello-cmake' local version = '0.0.1' load('boost/1.79.0') prepend_path('PATH', pathJoin(root, 'bin')) prepend_path('LD_LIBRARY_PATH', pathJoin(root, 'lib')) if (mode() == 'load') then LmodMessage('Hello World version', version, 'loaded.') end -
The example highlights a few Lmod/Lua features:
- Comments are specified with
--. localis used to declare local variables, in this case for the version number and installation path.load()is used to load modules the program depends on.PATHandLD_LIBRARY_PATHare modified usingprepend_path(), and thepathJoin()function is used together with therootvariable to compose the full paths to thebinandlibdirectories.- A message is printed to the standard output when the module is loaded
using an
ifclause andLmodMessage.
- Comments are specified with
-
To make your own modules available you need to modify your
MODULEPATHvariable. This can be done by hand or using themodule use <path>command, and is required each time you start a new shell session: -
Now that the module system knows where to search for your own modules, you can check if the module you created is available:
-
Load the module and check if you can run the
hicommand:
More information¶
- If you get stuck when compiling your own software, don't hesitate to ask for help from the CSC Service Desk.
- Documentation on how to compile on Roihu and LUMI.