How to include C11 headers when compiling C++ with GCC?

天大地大妈咪最大 提交于 2019-12-22 11:35:52

问题


In a C++ project, I'm using a C library which includes some C11 headers. It won't compile with GCC. See this simple code:

// main.cc
#include <stdatomic.h>

int main()
{
    return 0;
}

Running gcc main.cc -lstdc++, it complains: error: ‘_Atomic’ does not name a type. However, clang main.cc -lstdc++ works like a charm. I'm wondering what makes the difference, and how can I compile it with gcc?


回答1:


To wrap C headers that use atomics, you may use the other spelling of _Atomic and define a macro that transforms this to valid C++:

#ifndef __cplusplus
# include <stdatomic.h>
#else
# include <atomic>
# define _Atomic(X) std::atomic< X >
#endif

int foo(_Atomic(unsigned)* toto);

Both atomics interfaces have been developed in sync between the two committees, so besides syntax problems these should be binary compatible on any reasonable platform that provides C and C++.



来源:https://stackoverflow.com/questions/45342776/how-to-include-c11-headers-when-compiling-c-with-gcc

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!