简体   繁体   中英

How should I add C++ header files in this CMakeLists.txt file?

在此处输入图像描述

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() ?

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 n.netheless, 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() with PUBLIC qualifier or add_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})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM