C++ complex implementation fails to compile

Create issue
Issue #1374 closed
Frank Löffler created an issue

When compiling I get:

COMPILING /home/knarf/Cactus/arrangements/CactusCoastal/Funwave/src/declare.cc
In file included from /usr/include/c++/4.7/complex:46:0,
                 from /home/knarf/Cactus/src/include/cctk_Types.h:40,
                 from /home/knarf/Cactus/src/include/cctk_Groups.h:34,
                 from /home/knarf/Cactus/arrangements/CactusCoastal/Funwave/src/cctk_declare.h:9,
                 from /home/knarf/Cactus/configs/sim/build/Funwave/declare.cc:1:
/usr/include/c++/4.7/cmath: In function ‘constexpr float std::abs(float)’:
/usr/include/c++/4.7/cmath:90:16: error: declaration of C function ‘constexpr float std::abs(float)’ conflicts with
/usr/include/c++/4.7/cmath:84:3: error: previous declaration ‘constexpr double std::abs(double)’ here
/usr/include/c++/4.7/cmath: In function ‘constexpr long double std::abs(long double)’:
/usr/include/c++/4.7/cmath:94:22: error: declaration of C function ‘constexpr long double std::abs(long double)’ conflicts with
/usr/include/c++/4.7/cmath:90:3: error: previous declaration ‘constexpr float std::abs(float)’ here
/usr/include/c++/4.7/cmath:94:22: error: declaration of C function ‘constexpr long double std::abs(long double)’ conflicts with
/usr/include/c++/4.7/cmath:84:3: error: previous declaration ‘constexpr double std::abs(double)’ here
/usr/include/c++/4.7/cmath: At global scope:
/usr/include/c++/4.7/cmath:98:3: error: template with C linkage

This seems to be caused by cctk_Groups.h going into "C" linkage, then including cctk_Types.h which includes <complex> when compiled with C++. The system complex implementation however (at least in CXX0X mode) uses templates, which aren't allowed with "C" linkage.

I use gnu g++ version 4.7 with -std=gnu++0x.

Applying the following patch to src/include/cctk_Types.h solves the problem for me:

Index: cctk_Types.h
===================================================================
--- cctk_Types.h    (revision 5021)
+++ cctk_Types.h    (working copy)
@@ -37,7 +37,9 @@
 /* Declarations for complex types */

 #ifdef __cplusplus
+extern "C++" {
 #  include <complex>
+}
 #endif

 #ifdef HAVE_CCTK_REAL16

Keyword:

Comments (8)

  1. Frank Löffler reporter
    • changed status to open
    • removed comment

    We might want to apply this as well to the release, thus using ET_2013_05 milestone.

  2. Erik Schnetter
    • removed comment

    It is considered bad style to #include another file with anything but default linkage. The #include statement in cctk_Groups.h should be moved outside the extern "C" block.

  3. Frank Löffler reporter
    • removed comment

    That would correspond to the following, which also works for me:

    Index: cctk_Groups.h
    ===================================================================
    --- cctk_Groups.h   (revision 5021)
    +++ cctk_Groups.h   (working copy)
    @@ -26,13 +26,13 @@
    
     /* Prototypes */
    
    +#include "cctk_Types.h"
    +
     #ifdef __cplusplus 
     extern "C" 
     {
     #endif
    
    -#include "cctk_Types.h"
    -
     int         CCTK_DecomposeName(const char *fullname, 
                                    char **implementation, 
                                    char **name);
    
  4. Frank Löffler reporter
    • removed comment

    Committed to the development version in r5022. Will be committed to the release branch in a little while (thus, leaving ticket open).

  5. Log in to comment