为什么要在 CMake 中将头文件添加到 ADD_LIBRARY/ADD_EXECUTABLE 命令中 [英] Why add header files into ADD_LIBRARY/ADD_EXECUTABLE command in CMake

查看:50
本文介绍了为什么要在 CMake 中将头文件添加到 ADD_LIBRARY/ADD_EXECUTABLE 命令中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目,它使用 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_propertiesPUBLIC_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.

(这是我正在谈论的模板:)

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

如果您想将其作为单个命令执行,您可以按照您的建议使用 set_target_propertiesPUBLIC_HEADER.那么,这种install是可能的:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆