gpt4 book ai didi

c++ - 检查库是否包含C++

转载 作者:太空狗 更新时间:2023-10-29 19:52:46 26 4
gpt4 key购买 nike

我有一个函数需要不同的库,具体取决于插入其中的值,并且为了防止日志中出现不同的错误消息,我想检查是否包含所需的库,如果不包含则打印更具体的错误消息标准的。

有没有检查是否包含库的方法?像这样的东西:

   if(#include <iostream>)
{
//do this
}

编辑:

更多细节....我需要检查库以了解我是否可以在我的代码中使用某些变量类型,例如了解我是否可以使用字符串类型或者我是否应该使用类型 char[]相反(如果使用 cstring 库)

最佳答案

您可以检查是否定义了给定库的特定 header 或宏定义。

例如:

#ifdef BOOST_BUILD
#include "somebooststuff.h"
using namespace boost;
#else
#include "somestdstuff.h"
using namespace std;
#endif

其中 BOOST_BUILD 是 makefile 中定义的增强版本。

现在,如果您想检查例如 std::string 的 header 是否包含在内,您可以打开此 header 并确定它包含哪些定义。例如在我的机器头上

#include <string>

包含:

//
// ISO C++ 14882: 21 Strings library
//

#ifndef _GLIBCXX_STRING
#define _GLIBCXX_STRING 1

#pragma GCC system_header

#include <bits/c++config.h>
#include <bits/stringfwd.h>
#include <bits/char_traits.h> // NB: In turn includes stl_algobase.h
#include <bits/allocator.h>
//...

所以我可以检查 std::string 是否已使用以下方法定义:

#ifdef _GLIBCXX_STRING
//do this
#else
// do this
#endif

关于c++ - 检查库是否包含C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20248167/

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