将 Boost 标头包含在 .h 文件中但不包含在.cpp文件中时出错
Error occuring when including Boost headers into .h files but not in .cpp files
我正在处理一个奇怪的问题。我有一个代码,我需要使用 Boost 类型索引框架实现几个宏。由于我的宏仅包含在头文件中,因此我希望#include <boost/type_index.hpp>
仅包含在头文件中,如下所示:
#include <stdexcept>
#include <boost/type_index.hpp>
#ifdef L4N_DEBUG
#define ERR_MSG(msg) std::string(boost::typeindex::type_id_with_cvr<decltype(*this)>().pretty_name()) + "::" + __func__ + "(): " + msg + " (" +__FILE__ + ":" + std::to_string(__LINE__) + ")"
#else
#define ERR_MSG(msg) std::string(boost::typeindex::type_id_with_cvr<decltype(*this)>().pretty_name()) + "::" + __func__ + "(): " + msg
#endif // L4N_DEBUG
#define THROW_RUNTIME_ERROR(msg) std::runtime_error(ERR_MSG(msg))
#define THROW_LOGIC_ERROR(msg) std::logic_error(ERR_MSG(msg))
#define THROW_INVALID_ARGUMENT_ERROR(msg) std::invalid_argument(ERR_MSG(msg))
#define THROW_NOT_IMPLEMENTED_ERROR(msg) std::logic_error(ERR_MSG("This function is not implented." + msg))
但是我收到错误
/home/martin/MyProject/include/../src/DataSet/../Exception/Exceptions.h:9:10: fatal error: boost/type_index.hpp: No such file or directory
当我将 include 指令从Exceptions.h
标头移动到包含Exceptions.h
的.cpp
文件时,错误消失了。当然,我指定了这样的包含路径:
add_library(
MyLib
${LIB_TYPE}
Neuron/Neuron.cpp
Neuron/NeuronBinary.cpp
Neuron/NeuronConstant.cpp
Neuron/NeuronLinear.cpp
Neuron/NeuronLogistic.cpp
Network/NeuralNetwork.cpp
Network/NeuralNetworkSum.cpp
NetConnection/ConnectionFunctionGeneral.cpp
NetConnection/ConnectionFunctionIdentity.cpp
LearningMethods/ParticleSwarm.cpp
LearningMethods/GradientDescent.cpp
DataSet/DataSet.cpp
ErrorFunction/ErrorFunctions.cpp
Solvers/DESolver.cpp
Exception/Exceptions.cpp
CSVReader/CSVReader.cpp
CrossValidator/CrossValidator.cpp
NormalizationStrategy/NormalizationStrategy.cpp
)
target_include_directories(
lib4neuro
PUBLIC
${ROOT_DIR}/include
PRIVATE
${EXPRTK_INCLUDE_DIR}
${SRC_DIR}
${Boost_INCLUDE_DIRS}
)
有没有办法也为.h
文件而不仅仅是.cpp
正确指定包含路径?
最终,更仔细地阅读整个错误消息很有帮助:
In file included from /home/martin/MyLib/include/../src/DataSet/DataSet.h:17,
from /home/martin/MyLib/include/myapi.h:10,
from /home/martin/MyLib/src/examples/net_test_1.cpp:12:
/home/martin/4Neuro/include/../src/DataSet/../Exception/Exceptions.h:9:10: fatal error: boost/type_index.hpp: No such file or directory
#include <boost/type_index.hpp>
正如我们所看到的,Exceptions.h
被错误地包含在 API 的一部分DataSet.h
中。因此,在尝试使用库编译可执行文件中的代码时,编译失败,而不是在编译库本身期间。
相关文章:
- 读取大文件(>2GB)(文本文件包含以太网数据)并通过不同参数随机访问数据的最佳方法是什么?
- C++文件包含多行
- CMake 如何强制third_party库将项目文件包含在当前项目源目录中?
- 是否可以将要"ShellExecute"的文件包含在 dll 中?如果是这样,"ShellExecute"中的文件位置应该是什么?
- C++ 逐行从文件(包含空格)读取数据
- 在 C++ 中,.h 文件包含文件但不起作用
- 生成的资源文件包含奇怪的包含文件
- 可变参数类的文件包含问题
- 为什么文件命令声明可执行文件包含"debug_info"
- 可以将标头文件包含在多个源文件中,而无需重复保护
- 为什么将.HPP文件包含在.h文件中
- Qt C++:将所有库文件包含在项目文件夹中
- 有条件地将文件包含在C++标头中
- 将多个原型文件包含在一个项目中会导致protobuf_AssignDescriptorsOnce()已经有一个主体
- 有没有办法将文件包含在C++中的所有项目文件中
- 将外部.asm文件包含到C++代码中
- 什么是"pch.h",为什么需要将其作为第一个头文件包含在内?
- 该头文件包含DirectX 12中的ThrowIfFailed()
- QT C :函数的多个定义当Header文件包含在MainWindow.h中时
- QT创建者错误地处理标头文件包含来自Directory