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.
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++)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With