Using the library

This chapter describes how you can use libpninexus from within your own C++ project. As already mentioned in the installation chapter, libpninexus uses cmake as its build tool. In addition it also exports a cmake-package providing access to all the installation details.

For all the following examples we use the following minimal C++ program as an example

#include <h5cpp/hdf5.hpp>
#include <pni/nexus.hpp>

using namespace pni;

int main()
{
   hdf5::file::File file = nexus::create_file("hello.nxs",hdf5::file::AccessFlags::Truncate);

   return 0;
}

which we assume is the content of a file named hello.cpp.

Building with CMake

This is most probably the easierst case. As libpninexus also installs a cmake-package which provides an imported target using the library is fairly easy. For the code example above a sufficient CMakeLists.txt file would look like this

cmake_minimum_required(VERSION 3.5.0)
project(hello LANGUAGES C CXX VERSION 0.0.1)

find_package(pninexus REQUIRED)

add_executable(hello hello.cpp)
target_link_libraries(hello pninexus)

pninexus in the target_link_library command is an imported target which contains all the information required to build and link with libpninexus. This includes

  • the location of the library as well as all its dependencies

  • the location of the header files.

Build from the command line

Todo

Need to finish this section

$> g++ -std=c++11 -otest test.cpp $(pkg-config --cflags --libs pninexus)

There are two important remarks we have to make here. The first is the -std=c++11 option. This tells the compiler to use the new C++11 standard which is absolutely mandatory if you want to use libpninexus. the pkg-config command at the end of the command line includes all the necessary compiler and linker flags to build and link the code.

Build with a Makefile

Todo

Need to finish this section

pkg-config can be used in a Makefile by putting the following at the top of your Makefile

CPPFLAGS=-O2 -g -std=c++11 $(shell pkg-config --cflags pninexus)
LDFLAGS=$(shell pkg-config --libs pninexus)