CMakeLists.txt
add_library(systems
AtomTypes.cc
AtomTypingVariants.cc
BuildPolymerChain.cc
CartesianAtoms.cc
CartesianChains.cc
SimpleAtomTyping.cc
)
add_subdirectory(surpass)
target_link_libraries(systems
PRIVATE
surpass
)
I added source files, and now I need to add header files in the above file.
Is the above file correct?
How should I add header files in the above CMakeLists.txt
file?
Should I use target_sources()
with PUBLIC
qualifier or add_libraries()
?
CodePudding user response:
For the build logic it doesn't make a difference whether you're adding the header files to the source list or not. I recommend adding them nonetheless, since this results in IDEs like Visual Studio listing those files as sources for the target. You could list those header files in the same place you translation units (.cc files).
add_library(...
AtomTypes.cc
AtomTypes.hh
...
)
Should I use
target_sources()
withPUBLIC
qualifier oradd_libraries()
?
Whether you're using target_sources
or add_libraries
to add sources or headers is your choice. In general you should be using target_sources(... PRIVATE ...)
though, since any sources listed with PUBLIC
or INTERFACE
visibility will become part of linking targets as well.
Personally I tend to list sources and headers that are always part of a target in the command creating the target and use target_sources
as a way to add sources that are conditionally included. You could also create list variables for this purpose though.
add_library(foo STATIC
a.cpp
a.hpp
b.cpp
b.hpp
c.hpp
)
if (WIN32)
target_sources(foo PRIVATE c.win.cpp)
else()
target_sources(foo PRIVATE c.other.cpp)
endif()
or
set(SOURCES
a.cpp
b.cpp
)
set(HEADERS
a.hpp
b.hpp
c.hpp
)
if (WIN32)
list(APPEND SOURCES c.win.cpp)
else()
list(APPEND SOURCES c.other.cpp)
endif()
add_library(foo STATIC ${HEADERS} ${SOURCES})