gpt4 book ai didi

how to include boost headers in own header-only library(如何将Boost标头包含在自己的仅标头库中)

转载 作者:bug小助手 更新时间:2023-10-27 20:35:46 27 4
gpt4 key购买 nike



i am using Jetbrains CLion 2017.3 and the bundled CMake version 3.9.6 with mingw64 5.0 version/g++ 7.1.
Although reading the "Mastering CMake" ( i am new to CMake !) i have many difficulties to understand the basics. Since 3 days i am searching for a CMake solution to create a own header-only library that uses the boost (1.66.0 ) libraries.

我使用的是JetBrains Clion 2017.3和捆绑的CMake版本3.9.6和mingw645.0版本/g++7.1。虽然正在阅读《掌握CMake》(我是新手!)我要理解这些基本知识有很多困难。3天以来,我一直在寻找一种CMake解决方案来创建自己的仅包含头文件的库,该库使用Boost(1.66.0)库。



Using my CMakeLists.txt results in finding the boost libraries, but i cannot include boost headers in a header file from my current source directory.

使用我的CMakeLists.txt可以找到boost库,但是我不能在当前源目录的头文件中包含boost头文件。



My current source diretory contains the "CMakeLists.txt" and the header file
"test_boost.h".
If i try to include boost headers in the header file "test_boost.h", boost headers cannot be found !

我当前的源代码目录包含“CMakeLists.txt”和头文件“test_boost.h”。如果我尝试在头文件“test_boost.h”中包含Boost标头,则找不到Boost标头!



What i am doing wrong ?

我做错了什么?



My CMakeLists.txt :

我的CMakeLists.txt:



cmake_minimum_required(VERSION 3.9)
project(headerOnlyLib1)

set(CMAKE_CXX_STANDARD 11)

set(ENV{BOOST_ROOT} "C:/dev/boost/mingw/boost_1_66_0/boost")
set(Boost_USE_STATIC_LIBS ON) # only find static libs
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(BOOST 1.66 REQUIRED)
IF (Boost_FOUND)
message(STATUS "BOOST FOUND !")
ELSE()
message(STATUS "BOOST NOT Found !")
endif()
add_library(headerOnlyLib INTERFACE)
target_include_directories(headerOnlyLib INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
target_include_directories(headerOnlyLib INTERFACE ${Boost_INCLUDE_DIRS})
target_link_libraries(headerOnlyLib ${Boost_LIBRARIES})

更多回答

I think it should be set(BOOST_ROOT "C:/dev/boost/mingw/boost_1_66_0")

我认为应该设置它(BOOST_ROOT“C:/dev/Boost/mingw/Boost_1_66_0”)

优秀答案推荐

Short answer: You can't.

简短的回答是:你不能。



A "header-only library" is just that, one or more headers, only. It's not something that is linked or really stand-alone.

一个“header-only library”仅仅是一个或多个headers。这不是一个相互联系或独立的东西。



If your header-only library have dependencies, then the users of your library also have those dependencies and need to include them in their own build.

如果您的只有头的库有依赖项,那么您的库的用户也有这些依赖项,并且需要将它们包括在他们自己的构建中。



I think that you can, but you need to be more specific in defining your boost dependencies.

我认为你可以,但是你需要更具体地定义你的boost依赖项。



For example, the CMakeLists.txt file here depends upon boost::system for boost::asio. The dependency is defined as follows:

例如,这里的CMakeLists.txt文件依赖于Boost::System for Boost::ASIO。依赖关系的定义如下:



find_package(Boost REQUIRED COMPONENTS system)
if(Boost_FOUND)
target_include_directories(${PROJECT_NAME} PRIVATE ${Boost_INCLUDE_DIRS})

# Boost::asio is header only but it requires Boost::system
target_link_libraries(${PROJECT_NAME} INTERFACE Boost::system)
.
.
.
endif(Boost_FOUND)


In your case, the target is Boost::boost for header-only dependencies, see FindBoost. So the relevant part becomes:

在您的例子中,目标是Boost::Boost以获取仅有头的依赖项,请参见FindBoost。因此,相关部分变成:



find_package(Boost 1.66 REQUIRED COMPONENTS boost)
IF (Boost_FOUND)
message(STATUS "BOOST FOUND !")
target_include_directories(headerOnlyLib INTERFACE ${Boost_INCLUDE_DIRS})
ELSE()
message(STATUS "BOOST NOT Found !")
endif()


I recommend watching Daniel Pfeifer's talk at C++ Now 2017 for more information.

A lot has changed since "Mastering CMake" was written...

我推荐观看Daniel Pfeifer在C++Now 2017上的演讲,以了解更多信息。自从《精通CMake》写出来之后发生了很大的变化。



Here is a minimal CMake example to include header-only BOOST PropertyTree library

下面是一个包含仅包含头的Boost PropertyTree库的最小CMake示例



  • CMakeLists.txt


cmake_minimum_required(VERSION 3.15)
set(PROJECT_NAME test)

project(${PROJECT_NAME})

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(Boost 1.82 REQUIRED)
include_directories("${Boost_INCLUDE_DIRS}")
add_executable(${PROJECT_NAME} "src/main.cpp")


  • src/main.cpp


#include <boost/property_tree/ptree.hpp>

int main() {
boost::property_tree::ptree root;
root.put("A", "B");
}


  • Compile with


mkdir build
cd build
cmake ..
make

更多回答

i think your answer means that,in the case of header-only libraries, the are no binaries involved. That i have understood. But is there no possibility in CMake to include other library header files ( here boost header files) in own header files belonging to the own header-only library?

我认为你的回答意味着,在只有头的库的情况下,不涉及二进制文件。我已经明白了。但是,CMake是否不可能在属于自己的Header only库的头文件中包含其他库的头文件(这里是boost头文件)?

@user3664264 No that's not really how header file works. Well you can use the preprocessor on your header file, which will create a really big header file that includes all headers you include. It's usually not a good idea, since your code becomes obfuscated and hard to find. It might also be problem with licensing. So really, distribute your library as a standalone set of header files, then have Boost as an explicit dependency that others need to use.

@user3664264不,头文件不是这样工作的。您可以对您的头文件使用预处理器,这将创建一个非常大的头文件,其中包括您包含的所有头文件。这通常不是一个好主意,因为您的代码变得很混乱,很难找到。这也可能是许可方面的问题。因此,实际上,将您的库作为一组独立的头文件分发,然后将Boost作为其他人需要使用的显式依赖项。

Thank you for your clarification. i think now i have understood your previous answer.

谢谢你的澄清。我想我现在已经明白你之前的回答了。

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com