Browse Source

singles: add include guards for the implementation parts.

With seemingly innocent code like below, it happened that the
implementation part from MagnumMath.hpp got included twice because
MagnumMathBatch.hpp includes MagnumMath.hpp as a dependency, leading to
duplicate symbol definitions. This should be a completely valid use
case, so I'm adding guards to ensure the implementation is always only
added if not already.

    #define MAGNUM_MATH_IMPLEMENTATION
    #include <MagnumMath.hpp>
    #include <MagnumMathBatch.hpp>

A corresponding test case is going to get added to the singles repo.
pull/674/head
Vladimír Vondruš 1 year ago
parent
commit
454e9aec58
  1. 12
      src/singles/MagnumMath.hpp
  2. 13
      src/singles/MagnumMathBatch.hpp
  3. 13
      src/singles/MagnumMeshTools.hpp

12
src/singles/MagnumMath.hpp

@ -447,7 +447,17 @@ typedef Math::Frustum<Double> Frustumd;
// TODO: DynamicMatrixIntegration (separate library because of StridedArrayView)
#include "Magnum/EigenIntegration/GeometryIntegration.h"
#endif
#ifdef MAGNUM_MATH_IMPLEMENTATION
/* The extra guard has to be here to prevent double definitions in cases like
#define MAGNUM_MATH_IMPLEMENTATION
#include <MagnumMath.hpp>
#include <MagnumMathBatch.hpp>
where MagnumMathBatch.hpp contains `#include <MagnumMath.hpp>` again. Note
that even the stb_* libs don't handle this -- including them twice with the
implementation macro defined *will* lead to double definitions. */
#if defined(MAGNUM_MATH_IMPLEMENTATION) && !defined(MagnumMath_hpp_implementation)
#define MagnumMath_hpp_implementation
// {{ includes }}
#include "Magnum/Math/Functions.cpp"
#include "Magnum/Math/Packing.cpp"

13
src/singles/MagnumMathBatch.hpp

@ -67,6 +67,17 @@
// TODO ColorBatch once it's more useful
#include "Magnum/Math/FunctionsBatch.h"
#include "Magnum/Math/PackingBatch.h"
#ifdef MAGNUM_MATH_BATCH_IMPLEMENTATION
/* The extra guard has to be here to prevent double definitions in cases like
#define MAGNUM_MATH_BATCH_IMPLEMENTATION
#include <MagnumMathBatch.hpp>
#include <SomeOtherLib.h>
where a hypothetical SomeOtherLib.h contains `#include <MagnumMathBatch.hpp>`
again. Note that even the stb_* libs don't handle this -- including them
twice with the implementation macro defined *will* lead to double
definitions. */
#if defined(MAGNUM_MATH_BATCH_IMPLEMENTATION) && !defined(MagnumMathBatch_hpp_implementation)
#define MagnumMathBatch_hpp_implementation
#include "Magnum/Math/PackingBatch.cpp"
#endif

13
src/singles/MagnumMeshTools.hpp

@ -109,7 +109,18 @@ template<class T> using Array4 = StaticArray<4, T>;
// TODO FlipNormals?
// TODO GenerateNormals?
// TODO RemoveDuplicates?
#ifdef MAGNUM_MESHTOOLS_IMPLEMENTATION
/* The extra guard has to be here to prevent double definitions in cases like
#define MAGNUM_MESHTOOLS_IMPLEMENTATION
#include <MagnumMeshTools.hpp>
#include <SomeOtherLib.h>
where a hypothetical SomeOtherLib.h contains `#include <MagnumMeshTools.hpp>`
again. Note that even the stb_* libs don't handle this -- including them
twice with the implementation macro defined *will* lead to double
definitions. */
#if defined(MAGNUM_MESHTOOLS_IMPLEMENTATION) && !defined(MagnumMeshTools_hpp_implementation)
#define MagnumMeshTools_hpp_implementation
// {{includes}}
#if !defined(CORRADE_ASSERT_UNREACHABLE) && !defined(NDEBUG)
#include <cassert>

Loading…
Cancel
Save