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?