Reason to add empty source file to a STATIC library?

Viewed 908

I've seen this quite a lot in C++, that developers add an empty source file to the library in CMake. One example is here, with the empty source file found here. The CMake file has this line:

# build the library
add_library(${PROJECT_NAME} STATIC src/dependency-tracker.cc)

This is only the case if there are no other source files in the src folder, so the library would be 'header only'. Why do they do this?


The directory structure i was referring to:

.
├── CMakeLists.txt
├── include
│   └── okvis
│       └── kinematics
│           ├── implementation
│           │   └── Transformation.hpp   <- header only implementation
│           ├── operators.hpp
│           └── Transformation.hpp
├── src
│   └── dependency-tracker.cc   <- empty source file
└── test
    ├── runTests.cpp
    └── TestTransformation.cpp
1 Answers

The project specifies CMake 2.8.11 as a minimal requirement:

cmake_minimum_required(VERSION 2.8.11)

This version lacks for INTERFACE library type, which nowadays is a standard representation of a header-only library. (Support for INTERFACE libraries firstly appeared in CMake 3.0).

Without INTERFACE library available, a normal library with a single empty source file looks like a good alternative.

I don't know why dependency-tracker.cc name choosen for the empty source file. Probably, this name has some special meaning for the project's developers.

Related