First steps in C++

To get started with SMTK, let’s create a project that does the bare minimum; we’ll just print the SMTK version number and exit.

Including SMTK headers and calling methods

The code to do this just calls a static method on smtk::common::Version:

1#include <iostream>
2
3#include "smtk/common/Version.h"
4
5int main()
6{
7  std::cout << "Compiled with SMTK version " << smtk::common::Version::number() << "\n";
8  return 0;
9}

All of SMTK’s headers are prefixed by “smtk,” and because the version number is useful across all of SMTK and not just particular subsystems, it is put in the “common” directory. Usually — but not always — the subdirectories indicate the C++ namespace that a class lives in. Exceptions to this rule are classes in the extension directory, which are grouped separately because they appear in other libraries that have additional dependencies.

Compiling the example

To compile the program above, we need to link to SMTK’s main library, named smtkCore. The following CMakeLists.txt will set up a project for us:

1cmake_minimum_required(VERSION 3.12)
2project(smtk_version)
3
4find_package(smtk)
5add_executable(print_version print_version.cxx)
6target_link_libraries(print_version smtkCore)

The find_package directive tells CMake to find SMTK on your system and import its settings. SMTK provides settings in a file named SMTKConfig.cmake and it is usually stored in /usr/local/lib/cmake/SMTK on Unix and Mac OS X systems or C:\SMTK\lib\cmake\SMTK on Windows. When CMake asks for SMTK_DIR, you should provide it with the directory containing SMTKConfig.cmake.

Then, the target_link_libraries directive tells CMake not only to link to smtkCore, but also to add compiler directives to all source code in the executable specifying the location of header files. This directive also adds any transitive dependencies of smtkCore to the print_version program.