Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forcing Clang to link with C++ runtime

I have a project containing a mixture of C and C++ source. It currently builds with GCC on OS X. The project has bespoke build scripts which invoke the gcc command to compile both the C and C++ source, and separately invoke the linker.

I am now trying to get it building with Clang.

Invoking clang does compile the source files correctly; it distinguishes between .c and .cpp source files, and compiles for the appropriate language in each case. I have problems at link time, though. When the linker is invoked as clang, the C++ runtime libraries are not linked in, causing a build error due to missing symbols.

I can link successfully when I set clang++ as the build tool, but this then causes compile-time errors and warnings; it really doesn't like compiling C source with the C++ compiler.

clang: warning: treating 'c' input as 'c++' when in C++ mode, this behavior is deprecated
...
/usr/include/stdio.h:250:49: error: redefinition of parameter 'restrict'

I have to specify a single tool for the build scripts to use as the compiler/linker, so I need to do a simple substitution of clang in place of gcc. Is there any way I can persuade clang (not clang++) to link with the C++ runtime libraries?

Options such as -stdlib=libc++ don't work.

like image 870
Graham Borland Avatar asked Oct 17 '12 16:10

Graham Borland


1 Answers

You should just be able to use the normal linker flag, same as you'd do for gcc: clang -lc++ or clang -lstdc++ depending on which implementation you want. (and you should want libc++)

like image 171
bames53 Avatar answered Oct 09 '22 17:10

bames53