0

The C++ library compiles with or with out header file in CMakeLists.txt. Below one compiles with header in CMakeLists.txt

cmake_minimum_required(VERSION 3.0.0)

add_library(HeaderTest HeaderTest.cpp HeaderTest.h) ####

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

The library HeaderTest compiles with out HeaderTest.h in CMakeLists.txt ie the below CMakeLists.txt also compiles the library

cmake_minimum_required(VERSION 3.0.0)

add_library(HeaderTest HeaderTest.cpp) ###

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

I understand it is because cpp files includes the header files and header files get compiled when the include pre-processor in cpp file is expanded. But is there an advantage in including header files in CMakeLists.txt? Insights on this is appreciated.

starball
  • 20,030
  • 7
  • 43
  • 238
user641247
  • 35
  • 6

1 Answers1

0

But is there an advantage in including header files in CMakeLists.txt?

For add_library and add_executable, it can cause CMake to generate certain IDE-integrated buildsystems like Visual Studio to display those header files in a perhaps more-desirable way, but to my knowledge, that's it. It doesn't have impact on whether the buildsystem itself functions properly or not with respect to doing builds.

If you have cmake_minimum_require(VERSION 3.23) and want to do something that has possible usefulness beyond that, take a look at the target_sources(... FILE_SET HEADERS ...) command, which can have usefulness for doing installs of public headers for targets.

starball
  • 20,030
  • 7
  • 43
  • 238
  • I have been using vs code. It does not make any issues if i remove the header from CMakeLists.txt. I opened the folder on Visual Studio. It as well does display the files and built it. Can you specify what issues we might encounter? – user641247 Apr 27 '23 at 02:10
  • I put this answer [here](https://stackoverflow.com/a/76109228/11107541) after learning there was a dup target. My answer there states: "_Whether you do this or not is up to you. I'm not aware of any clear downsides to specifying headers in add_library or add_executable_" – starball Apr 27 '23 at 02:11