0

So I have a library which has two files mgparser.hpp (in include) and mgparser.cpp (in src), since I am using templated functions, I have to include the definition of the function at the declaration of the templated type. For that reason in the end of my mgparser.hpp I have added #include "mgparser.cpp". The CMake code looks something like this

cmake_minimum_required(VERSION 3.1) 
project(magahttpparser VERSION 0.1)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

add_library(mgparser STATIC 
    ${CMAKE_SOURCE_DIR}/include/mgparser.hpp
    # ${CMAKE_SOURCE_DIR}/src/mgparser.cpp
)
set_target_properties(mgparser PROPERTIES LINKER_LANGUAGE CXX)
target_compile_options(mgparser PUBLIC -Wall)
set_target_properties(mgparser PROPERTIES POSITION_INDEPENDENT_CODE ON)

target_include_directories(mgparser PUBLIC ${CMAKE_SOURCE_DIR}/include)
target_include_directories(mgparser PUBLIC ${CMAKE_SOURCE_DIR}/src)

Since the mgparser.cpp is included in the header file, there is no need for it to be compiled one more time, so I have commented it out, but this results in library file to not be formed. A simple fix I have found for this is just create a empty source file random.cpp in src, and then add it in the add_library. Is there any reason for this behaviour, if so is there any proper fix to this?

Bad_Panda
  • 427
  • 1
  • 5
  • 11
  • 3
    If you're not giving the library any source files to compile (headers don't count), why would you expect anything to be compiled? Also, if your header includes the cpp file, then the cpp file also needs to be distributed. – Stephen Newell May 26 '21 at 13:07
  • Normally, a header-only library is represented in CMake using INTERFACE library target. As stated in the comment above, this target is not compiled, but your can link with that target and even install it. – Tsyvarev May 26 '21 at 13:49

0 Answers0