简体   繁体   English

我应该如何在此 CMakeLists.txt 文件中添加 C++ header 文件?

[英]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.我添加了源文件,现在需要在上面的文件中添加header个文件。

Is the above file correct?以上文件是否正确?

How should I add header files in the above CMakeLists.txt file?我应该如何在上面的CMakeLists.txt文件中添加header 个文件?

Should I use target_sources() with PUBLIC qualifier or add_libraries() ?我应该将target_sources()PUBLIC限定符或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.对于构建逻辑,无论您是否将 header 文件添加到源列表都没有区别。 I recommend adding them n.netheless, since this results in IDEs like Visual Studio listing those files as sources for the target.我建议添加它们 n.netheless,因为这会导致像 Visual Studio 这样的 IDE 将这些文件列为目标的源。 You could list those header files in the same place you translation units (.cc files).您可以在与翻译单元(.cc 文件)相同的位置列出这些 header 文件。

add_library(...
    AtomTypes.cc
    AtomTypes.hh
    ...
)

Should I use target_sources() with PUBLIC qualifier or add_libraries() ?我应该将target_sources()PUBLIC限定符或add_libraries()一起使用吗?

Whether you're using target_sources or add_libraries to add sources or headers is your choice.您可以选择使用target_sources还是add_libraries添加源或标头。 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.一般来说,您应该使用target_sources(... PRIVATE...) ,因为列出的任何具有PUBLICINTERFACE可见性的来源也将成为链接目标的一部分。

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.我个人倾向于在创建目标的命令中列出始终属于目标的源和标头,并使用target_sources作为添加有条件包含的源的方法。 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})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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