为什么要在CMake中将头文件添加到ADD_LIBRARY / ADD_EXECUTABLE命令中 [英] Why add header files into ADD_LIBRARY/ADD_EXECUTABLE command in CMake
问题描述
我有一个项目使用CMake作为构建工具,并为我和我的同事创建了一个简单的模板。在网上搜索最佳和易于使用的做法时,我遇到了制作图书馆的各种方法。
I had a project which uses CMake as build tool and made a simple template for me and my collegues to use. As I searched for best and easy to use practices online, I've came across different approaches to make a library.
在此模板中,我列出了头文件和源文件放在两个单独的变量中,并且我没有将标头传递给 add_library
命令-只是源文件。然后,我将 set_target_properties
与 PUBLIC_HEADER
变量一起使用以提供头文件列表。
In this template, I've listed header files and source files in two seperate variables, and I'm not passing the headers to add_library
command - just sources. And then I use set_target_properties
with PUBLIC_HEADER
variable to give the header-file list.
到目前为止,它似乎可行,但是我想知道我是否正在使事情变得不必要地复杂。某些在线用户也将头文件提供给add_library命令,甚至不使用 set_target_properties
等。
So far it seems to work, but I wonder if I'm making thing unnecessarily complex. Some people online give header files to add_library command as well and doesn't even use set_target_properties
and such.
简而言之:
- 我们应该将头文件包括到
add_library
还是不应该(作为最佳做法)?以及这两种用法的影响。 - 在add_library / add_executable中添加标头的目的是什么?正如他们似乎在没有它的情况下一样工作(似乎仅转发声明和符号)。
- should we include header files to
add_library
or should we not (as a best practice)? And impacts of the two usage. - what is purpose being served by adding headers in the add_library/add_executable? As they seem working even without it (seems forward declaration and symbols only). confirm on understanding please.
(这是我在m在谈论:)
(Here is the template I'm talking about:)
cmake_minimum_required(VERSION 3.1.0)
project(lae CXX C)
set(CMAKE_CXX_STANDARD 14)
include_directories(
${CMAKE_CURRENT_SOURCE_DIR}
)
set(SOURCE_FILES
...
)
set(HEADER_FILES
...
)
set( PRIVATE_HEADER_FILES
...
)
add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES} )
set( REQUIRED_LIBRARIES
...
)
target_link_libraries(${PROJECT_NAME} ${REQUIRED_LIBRARIES} )
SET_TARGET_PROPERTIES(
${PROJECT_NAME}
PROPERTIES
FRAMEWORK ON
SOVERSION 0
VERSION 0.1.0
PUBLIC_HEADER "${HEADER_FILES}"
PRIVATE_HEADER "${PRIVATE_HEADER_FILES}"
ARCHIVE_OUTPUT_DIRECTORY "lib"
LIBRARY_OUTPUT_DIRECTORY "lib"
OUTPUT_NAME ${PROJECT_NAME}
)
推荐答案
在我们的项目中,使用您的简单方式- add_library
同时包含标头和源。
In our projects we use a "simple" way of yours - add_library
with both headers and sources.
如果仅添加源,则在IDE生成的项目中将看不到标题。
If you add only sources, then you won't see headers in IDE-generated project.
但是,在安装时,我们必须使用两个 install
命令这样做:
However, when installing, we have to do it like that, using two install
commands:
install(TARGETS library_name
LIBRARY DESTINATION lib)
install(FILES ${PUBLIC_HEADERS}
DESTINATION include/library_name)
如果要作为单个命令执行,可以使用<根据您的建议,将code> set_target_properties 与 PUBLIC_HEADER
一起使用。
然后,这种安装
是可能的:
If you want to do it as a single command, you can use set_target_properties
with PUBLIC_HEADER
, as you suggested.
Then, this kind of install
is possible:
install(TARGETS library_name
LIBRARY DESTINATION lib
PUBLIC_HEADER DESTINATION include/library_name)
选择最喜欢的一个并坚持下去。
Choose the one you like the most and stick to it.
这篇关于为什么要在CMake中将头文件添加到ADD_LIBRARY / ADD_EXECUTABLE命令中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!