Search code examples
c++cmakestatic-libraries

create static library of files with .inl extension


I am trying to create a static library of files with .inl extension, but getting failed. If I change the extension to .cpp then it builds.

CMakeLists.txt

cmake_minimum_required(VERSION 3.0.0)
project(CarSimulator VERSION 0.1.0 LANGUAGES C CXX)

include(CTest)
enable_testing()

add_library(printer
   print.inl print.hpp
)
set_property(TARGET printer PROPERTY CXX_STANDARD 20)

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)

print.hpp

#ifndef PRINT_H
#define PRINT_H

template<typename T>
void print(const T& t);

#include "print.cpp"

#endif

print.inl

#include <iostream>

template<typename T>
void print(const T& t) {
   std::cout << t << std::endl;
}

Error

CMake Error: Cannot determine link language for target "printer".
CMake Error: CMake can not determine linker language for target: printer
-- Generating done
CMake Generate step failed.  Build files cannot be regenerated correctly.

Solution

  • CMake has some predefined rules for the determining the linker-language based on the source files extension.
    .cpp is a known extension for C++, but aparently .inl isn't.

    You can use the following command to set the language explicitly to C++:

    set_target_properties(printer PROPERTIES LINKER_LANGUAGE CXX)
    

    Having said that it seems like your library is actually a header only one:
    In order to consume it in another project you only need to use #include print.hpp (which in turn #includes the .inl file).
    If this is indeed the case - it is not clear why you need a CMake for it at all: you can simply deliver the source files as is.