Search code examples
c++cmakeshared-librariesstatic-librariescmake-custom-command

Shipping a library-only package with CMake


I want to have my own little library and collect any useful code that I manage to write in the future. My idea is to have it structured like lib-boost and whenever I start a new project, I also use this library if necessary. Lets call this "lib-ezpz"

I also want it to be modular that is every category has its own lib file (e.g. libEzPzFileIO.lib for file stuff and LibEzPzMath.lib for math stuff.

Here is my folder structure:

Folder Structure

The problems that I am looking for answers for:

  1. After building the project, only the compiled library end up in build folder but I also need to ship the HEADERs as well. Like on Linux or windows when one says make install and then the lib and header files get copied into /usr/lib or something
  2. I want to have a good folder structure in the output too...also every library should end up in its own subdirectory... but at the moment cmake just drops everything in the root folder of build dir
  3. My HEADER ONLY math template library compiles fine but there are NO resulting library ending up in the build directory

And here are file contents:

fileio/parser.h

#pragma once
#include <iostream>
#include <vector>

namespace EzPz::FileIO::Parser {
  std::vector<std::vector<std::string>> parseCsvFile();
}

fileio/parser.cpp

#include "parser.h"

namespace EzPz::FileIO::Parser {
  std::vector<std::vector<std::string>> parseCsvFile() {
    return {
        {"line one", "haha", "hoho"},
        {"line two", "hihi", "weewee"}
    };
  }
}

math/basicops.h

#pragma once

namespace EzPz::Math::BasicOperations {
  template<typename T>
  T addValues(const T& lhs, const T& rhs) {
    return (lhs + rhs);
  }
}

** And here is the CMAKE file **

cmake_minimum_required(VERSION 3.0)
project(EzPz)

set(CMAKE_CXX_STANDARD 17)

# Library for FILE IO Oeprations
add_library(EzPzFileIo fileio/parser.cpp)

# Library for MATH stuff (header only)
add_library(EzPzMath STATIC math/basicops.h)
set_target_properties(EzPzMath PROPERTIES LINKER_LANGUAGE CXX)
target_compile_definitions(EzPzMath PRIVATE "haha")

Solution

    1. After building the project, only the compiled library end up in build folder but I also need to ship the HEADERs as well. Like on Linux or windows when one says make install and then the lib and header files get copied into /usr/lib or something

    The build directory is not really a good place to work with things to ship. You should be installing things. CMake supports what you're looking for. See the relevant tutorial chapter, the relevant Mastering CMake book chapter, and the relevant reference documentation for the install command.

    Take also a look at CPack (for packaging). Mastering CMake chapter, reference docs.

    1. I want to have a good folder structure in the output too...also every library should end up in its own subdirectory... but at the moment cmake just drops everything in the root folder of build dir

    I mean... define "good". There's no good or bad. There's suitable and unsuitable, works and doesn't work. If you want to control the output directories of targets in the build directory, use the various target properties with "OUTPUT_DIRECTORY" in their name (whichever one is suitable for the type of target you apply it to).

    1. My HEADER ONLY math template library compiles fine but there are NO resulting library ending up in the build directory

    Headers don't need to be copied to the build directory to be included. You just specify the include directories in the CMake config, and what you specify is what gets used without any copying done behind your back. There's no need to copy them, so CMake doesn't do anything to make it so.