This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug middle-end/71789] New: atomic_int incompatibility warning between C and C++ [-Wlto-type-mismatch]


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71789

            Bug ID: 71789
           Summary: atomic_int incompatibility warning between C and C++
                    [-Wlto-type-mismatch]
           Product: gcc
           Version: 6.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: patrick.pelissier at gmail dot com
  Target Milestone: ---

Trying to use a C library within a C++ program, I get some errors due to the
header of the C library including stdatomic.h
Following the advice of https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60932 I
tried to patch the header with:
                #ifdef __cplusplus
                #include <atomic>
                using namespace std;
                #else
                #include <stdatomic.h>
                #endif
However when I link the final application with LTO, I get some warning.

It can be shown by the following example:

c.c:
                #include "c.h"
                bool f(atomic_int b) {
                  return b == 0;
                }
c.cpp:
                #include "c.h"
                int main(void) {
                  bool b = f(ATOMIC_VAR_INIT(0));
                  return b ? 0 : 1;
                }
c.h:
                #ifndef __C_H
                #define __C_H
                #include <stdbool.h>
                #ifdef __cplusplus
                #include <atomic>
                using namespace std;
                #else
                #include <stdatomic.h>
                #endif
                #ifdef __cplusplus
                extern "C" {
                #endif
                extern bool f(atomic_int n);
                #ifdef __cplusplus
                }
                #endif
                #endif

And the used compilation command:
$ gcc -c -flto -O2 -Wall -W -std=c11 c.c -o c1.o && g++ -c -flto -O2 -Wall -W
-std=c++11 c.cpp -o c2.o && g++ -flto -O2 -Wall c1.o c2.o

gives the following warning:

c.h:18:13: attention : type of ‘f’ does not match original declaration
[-Wlto-type-mismatch]
 extern bool f(atomic_int n);
             ^
c.c:3:6: note : type mismatch in parameter 1
 bool f(atomic_int b)
      ^
/opt/gcc/lib/gcc/x86_64-pc-linux-gnu/6.1.0/include/stdatomic.h:46:21: note :
type ‘atomic_int’ should match type ‘struct atomic_int’
 typedef _Atomic int atomic_int;
                     ^
/opt/gcc/include/c++/6.1.0/atomic:794:25: note : the incompatible type is
defined here
   typedef atomic<int>   atomic_int;
                         ^
c.c:3:6: note : ‘f’ was previously declared here
 bool f(atomic_int b)
      ^
c.c:3:6: note : code may be misoptimized unless -fno-strict-aliasing is used

$ gcc --version
gcc (GCC) 6.1.0

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]