将 Boost headers 包含到 .h 文件而不是 .cpp 文件时发生错误
Error occuring when including Boost headers into .h files but not in .cpp files
我正在处理一个奇怪的问题。我有一个代码,我需要使用 Boost typeindex 框架实现几个宏。由于我的宏仅包含在 header 文件中,因此我希望 #include <boost/type_index.hpp>
仅包含在 header 文件中,如下所示:
#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
header 移动到包含 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
被错误地包含在 DataSet.h
中,而 DataSet.h
是 API 的一部分。因此,当尝试使用库编译可执行文件中的代码时编译失败,而不是在库本身的编译过程中。
我正在处理一个奇怪的问题。我有一个代码,我需要使用 Boost typeindex 框架实现几个宏。由于我的宏仅包含在 header 文件中,因此我希望 #include <boost/type_index.hpp>
仅包含在 header 文件中,如下所示:
#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
header 移动到包含 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
被错误地包含在 DataSet.h
中,而 DataSet.h
是 API 的一部分。因此,当尝试使用库编译可执行文件中的代码时编译失败,而不是在库本身的编译过程中。