Search code examples
c++boostcmakeheader-files

Error occuring when including Boost headers into .h files but not in .cpp files


I'm dealing with a strange problem. I have a code, where I need to implement several macros using Boost typeindex framework. As my macros are only included in the header file, I'd like #include <boost/type_index.hpp> to be included in the header file only like this:

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

But I'm getting the error

/home/martin/MyProject/include/../src/DataSet/../Exception/Exceptions.h:9:10: fatal error: boost/type_index.hpp: No such file or directory

The error disappears, when I move the include directive from Exceptions.h header to the .cpp files, where Exceptions.h is included. Of course, I'm specifying the include paths like this:

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

Is there any way to specify the include paths correctly also for .h files and not only for .cpp?


Solution

  • Eventually it was helpful to read the whole error message more carefully:

    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>
    

    As we can see, Exceptions.h was incorrectly included in DataSet.h which is a part of API. Therefore the compilation failed when trying to compile the code in the executables using the library, not during the compilation of the library itself.