Using clang to generate .o files for .i files generated by gcc, errors occur

The code example is very simple.

#include <stdio.h>

int main() {
        printf("hello, world");
}
  1. Generate the .i file.

gcc -E test.cpp -o test.cpp.ii

  1. generate .o files for .i files

clang++ -c test.cpp.ii -o test.cpp.o

The following error message is displayed.

In file included from test.cpp:1:
/usr/include/stdio.h:189:48: error: '__malloc__' attribute takes no arguments
  __attribute__ ((__malloc__)) __attribute__ ((__malloc__ (fclose, 1))) ;
                                               ^
/usr/include/stdio.h:201:49: error: '__malloc__' attribute takes no arguments
   __attribute__ ((__malloc__)) __attribute__ ((__malloc__ (fclose, 1))) ;
                                                ^
/usr/include/stdio.h:223:77: error: use of undeclared identifier '__builtin_free'; did you mean '__builtin_frexp'?
   noexcept (true) __attribute__ ((__malloc__)) __attribute__ ((__malloc__ (__builtin_free, 1)));
                                                                            ^
/usr/include/stdio.h:223:77: note: '__builtin_frexp' declared here
/usr/include/stdio.h:223:65: error: '__malloc__' attribute takes no arguments
   noexcept (true) __attribute__ ((__malloc__)) __attribute__ ((__malloc__ (__builtin_free, 1)));

btw, When using gcc to generate .o files from .i files, everything works fine.
Specifically, my question is:

  1. attribute ((malloc)) is a feature unique to GCC support?
  2. In this case, how to make clang generate .o files correctly

I don’t think that it’s possible to make Clang compile sources preprocessed by GCC in general case, because all GCC- and Clang-specific code hidden behind e.g. #ifdef __clang__ is expanded in exactly the wrong way. Even if your code doesn’t have them, libraries you build upon might very well do.

1 Like