Clang 3.4 stdlib.h compile error - gcc

I am running into a rather hairy compile issue with Clang 3.4. The code compiles just fine with Clang 3.2 but I want to experiment with some bleeding edge features found only on Clang 3.4.
I installed Clang 3.4 by following the instructions here.
Does anyone have a clue as to why Clang is complaining about stdlib.h? I am using Lubuntu 13.04.
Output from Clang 3.2 -
clang -v -g -fsanitize=address -fno-omit-frame-pointer -fms-extensions -O0 -o obj/Test.o -Iinc/ -c Test.c
Ubuntu clang version 3.2-1~exp9ubuntu1 (tags/RELEASE_32/final) (based on LLVM 3.2)
Target: i386-pc-linux-gnu
Thread model: posix
"/usr/bin/clang" -cc1 -triple i386-pc-linux-gnu -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name Test.c -mrelocation-model static -mdisable-fp-elim -fmath-errno -masm-verbose -mconstructor-aliases -fuse-init-array -target-cpu pentium4 -target-linker-version 2.23.2 -momit-leaf-frame-pointer -v -g -coverage-file /home/adminuser/project/obj/Test.o -resource-dir /usr/bin/../lib/clang/3.2 -I inc/ -fmodule-cache-path /var/tmp/clang-module-cache -internal-isystem /usr/local/include -internal-isystem /usr/bin/../lib/clang/3.2/include -internal-isystem /usr/include/clang/3.2/include/ -internal-externc-isystem /usr/include/i386-linux-gnu -internal-externc-isystem /usr/include/i686-linux-gnu -internal-externc-isystem /usr/include -O0 -fdebug-compilation-dir /home/adminuser/project -ferror-limit 19 -fmessage-length 0 -fsanitize=address -mstackrealign -fms-extensions -fmsc-version=1300 -fobjc-runtime=gcc -fdiagnostics-show-option -o obj/Test.o -x c Test.c
clang -cc1 version 3.2 based upon LLVM 3.2svn default target i386-pc-linux-gnu
ignoring nonexistent directory "/usr/bin/../lib/clang/3.2/include"
ignoring nonexistent directory "/usr/include/i686-linux-gnu"
#include "..." search starts here:
#include <...> search starts here:
inc
/usr/local/include
/usr/include/clang/3.2/include
/usr/include/i386-linux-gnu
/usr/include
Output from Clang 3.4 -
clang -v -g -fsanitize=address -fno-omit-frame-pointer -fms-extensions -O0 -o obj/Test.o -Iinc/ Test.c
Ubuntu clang version 3.4-1~exp1 (trunk) (based on LLVM 3.4)
Target: i386-pc-linux-gnu
Thread model: posix
Found candidate GCC installation: /usr/bin/../lib/gcc/i686-linux-gnu/4.7
Found candidate GCC installation: /usr/bin/../lib/gcc/i686-linux-gnu/4.7.3
Found candidate GCC installation: /usr/bin/../lib/gcc/i686-linux-gnu/4.8
Found candidate GCC installation: /usr/bin/../lib/gcc/i686-linux-gnu/4.8.1
Found candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/4.7
Found candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/4.7.3
Found candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/4.8
Found candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/4.8.1
Selected GCC installation: /usr/lib/gcc/i686-linux-gnu/4.8
"/usr/bin/clang" -cc1 -triple i386-pc-linux-gnu -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name Test.c -mrelocation-model static -mdisable-fp-elim -fmath-errno -masm-verbose -mconstructor-aliases -fuse-init-array -target-cpu pentium4 -target-linker-version 2.23.2 -v -g -coverage-file /home/adminuser/project/obj/Test.o -resource-dir /usr/bin/../lib/clang/3.4 -I inc/ -internal-isystem /usr/local/include -internal-isystem /usr/bin/../lib/clang/3.4/include -internal-isystem /usr/include/clang/3.4/include/ -internal-externc-isystem /usr/include/i386-linux-gnu -internal-externc-isystem /usr/include/i686-linux-gnu -internal-externc-isystem /usr/include -O0 -fdebug-compilation-dir /home/adminuser/BuildFW/test/GlassCubeAssembler_C -ferror-limit 19 -fmessage-length 0 -fsanitize=address,init-order -mstackrealign -fms-extensions -fmsc-version=1300 -fobjc-runtime=gcc -fobjc-default-synthesize-properties -fdiagnostics-show-option -vectorize-slp -o obj/Test.o -x c Test.c
clang -cc1 version 3.4 based upon LLVM 3.4 default target i386-pc-linux-gnu
ignoring nonexistent directory "/usr/bin/../lib/clang/3.4/include"
ignoring nonexistent directory "/usr/include/i686-linux-gnu"
#include "..." search starts here:
#include <...> search starts here:
inc
/usr/local/include
/usr/include/clang/3.4/include
/usr/include/i386-linux-gnu
/usr/include
End of search list.
In file included from Test.c:4:
/usr/include/stdlib.h:69:23: error: expected member name or ';' after declaration specifiers
union wait *__uptr;
~~~~~ ^
1 error generated.

The issue is that you're using -fms-extensions, which enables MS extensions, including a __uptr keyword. Inside stdlib.h, __uptr is used as an identifier:
union wait *__uptr;
... which is incompatible with the MS extension.
Alp Toker has added a workaround for this in clang trunk in upstream r195710, and the fix will be in the Clang 3.4 release.

This was http://llvm.org/PR17824 and was fixed by Alp Toker in Clang r195710: http://llvm.org/viewvc/llvm-project?rev=195710&view=rev

Related

macOS linking issue: build for macOS but dylib file built for DriverKit

I am trying to compile a simple helloworld.cpp file using clang on macOS Big Sur.
clang++ -std=c++17 -O3 hello.cpp -v
Apple clang version 12.0.0 (clang-1200.0.32.29)
Target: x86_64-apple-darwin20.3.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1"
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang" -cc1 -triple x86_64-apple-macosx11.0.0 -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -Werror=implicit-function-declaration -emit-obj -disable-free -disable-llvm-verifier -discard-value-names -main-file-name hello.cpp -mrelocation-model pic -pic-level 2 -mthread-model posix -mframe-pointer=all -fno-strict-return -masm-verbose -munwind-tables -target-sdk-version=11.1 -target-cpu penryn -dwarf-column-info -debugger-tuning=lldb -target-linker-version 609.8 -v -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/12.0.0 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -I/usr/local/include -stdlib=libc++ -internal-isystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1 -internal-isystem /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/local/include -internal-isystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/12.0.0/include -internal-externc-isystem /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include -internal-externc-isystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include -O3 -Wno-reorder-init-list -Wno-implicit-int-float-conversion -Wno-c99-designator -Wno-final-dtor-non-final-class -Wno-extra-semi-stmt -Wno-misleading-indentation -Wno-quoted-include-in-framework-header -Wno-implicit-fallthrough -Wno-enum-enum-conversion -Wno-enum-float-conversion -std=c++17 -fdeprecated-macro -fdebug-compilation-dir /Users/krumielf/Desktop/Thesis/clang_4life -ferror-limit 19 -fmessage-length 157 -stack-protector 1 -fstack-check -mdarwin-stkchk-strong-link -fblocks -fencode-extended-block-signature -fregister-global-dtors-with-atexit -fgnuc-version=4.2.1 -fobjc-runtime=macosx-11.0.0 -fcxx-exceptions -fexceptions -fmax-type-align=16 -fdiagnostics-show-option -fcolor-diagnostics -vectorize-loops -vectorize-slp -o /var/folders/_4/zsjcpnz14xx6_vt850fvg4bw0000gn/T/hello-37932a.o -x c++ hello.cpp
clang -cc1 version 12.0.0 (clang-1200.0.32.29) default target x86_64-apple-darwin20.3.0
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/local/include"
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/Library/Frameworks"
#include "..." search starts here:
#include <...> search starts here:
/usr/local/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/12.0.0/include
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks (framework directory)
End of search list.
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -lto_library /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/libLTO.dylib -dynamic -arch x86_64 -platform_version macos 11.0.0 11.1 -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -o a.out -L/usr/local/lib /var/folders/_4/zsjcpnz14xx6_vt850fvg4bw0000gn/T/hello-37932a.o -lc++ -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/12.0.0/lib/darwin/libclang_rt.osx.a
ld: warning: building for macOS, but linking in dylib file (/usr/local/lib/libSystem.dylib) built for DriverKit
ld: cannot link directly with dylib/framework, your binary is not an allowed client of /usr/local/lib/libSystem.dylib for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I have already installed and updated the xcode command line tools. I am not sure on what else to look into and any suggestion will be greatly appreciated.

How to prevent OSX gcc to always search /usr/local/include?

On my OSX it turns out /usr/local/include is always included as '-I', which caused a lot of headache for me:
hidden$ g++ -v -x c++ -isystem . -c /dev/null -o /dev/null
Apple clang version 11.0.0 (clang-1100.0.33.16)
Target: x86_64-apple-darwin19.3.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.15.0 -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name null -mrelocation-model pic -pic-level 2 -mthread-model posix -mdisable-fp-elim -fno-strict-return -masm-verbose -munwind-tables -target-sdk-version=10.15 -target-cpu penryn -dwarf-column-info -debugger-tuning=lldb -ggnu-pubnames -target-linker-version 530 -v -coverage-notes-file /dev/null.gcno -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/11.0.0 -isystem . -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -I/usr/local/include -stdlib=libc++ -internal-isystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1 -Wno-framework-include-private-from-public -Wno-atimport-in-framework-header -Wno-extra-semi-stmt -Wno-quoted-include-in-framework-header -fdeprecated-macro -fdebug-compilation-dir /tmp -ferror-limit 19 -fmessage-length 208 -stack-protector 1 -fstack-check -mdarwin-stkchk-strong-link -fblocks -fencode-extended-block-signature -fregister-global-dtors-with-atexit -fobjc-runtime=macosx-10.15.0 -fcxx-exceptions -fexceptions -fmax-type-align=16 -fdiagnostics-show-option -fcolor-diagnostics -o /dev/null -x c++ /dev/null
clang -cc1 version 11.0.0 (clang-1100.0.33.16) default target x86_64-apple-darwin19.3.0
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1"
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/local/include"
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/Library/Frameworks"
#include "..." search starts here:
#include <...> search starts here:
/usr/local/include
.
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/11.0.0/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks (framework directory)
End of search list.
The exact headache is not important here, but for anyone who are curious, my protobuf installed at /usr/local/include by Homebrew is used by bazel build, while I want to use my own project's version of protobuf. Bazel adds -isystem for the include paths, which is shown above that is shadowed by -I/usr/local/include.
I tried sudo xcode-select -s /Library/Developer/CommandLineTools but doesn't help.
Anyone knows where the -I/usr/local/include is ever introduced and how I can get rid of it (but not other standard include directories)? Is it some configuration that I can tweak, or is it hard-coded in compiler's binary?
It's provided by Clang itself, by default.
You can supply the -nostdinc/--no-standard-includes to suppress this, but it also suppresses a bunch of other include directories.
A slightly more targeted way is by passing -Xclang -nostdsysteminc, but it still suppresses things you probably want. You can, of course, replace those manually on the command line.

Running Clang++ compiled file raises "dyld: mach-o, but built for simulator (not macOS)" after installing React-Native

After I installed React-Native.
Whenever build a file through "clang", I can't execute the file.
Always I got this message when try to execute it.
dyld: mach-o, but built for simulator (not macOS)
And I found that my default compile option of clang is changed for "ios-simulator".
How to change this default option?
$ clang++ --std=c++17 --verbose test_move.cpp
Apple LLVM version 10.0.1 (clang-1001.0.46.4)
Target: x86_64-apple-darwin18.5.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang" -cc1 -triple x86_64-apple-ios9.0.0-simulator -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -Werror=implicit-function-declaration -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name test_move.cpp -mrelocation-model pic -pic-level 2 -mthread-model posix -mdisable-fp-elim -fno-strict-return -masm-verbose -munwind-tables -faligned-alloc-unavailable -target-sdk-version=12.2 -target-cpu core2 -dwarf-column-info -debugger-tuning=lldb -target-linker-version 450.3 -v -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/10.0.1 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator12.2.sdk -stdlib=libc++ -Wno-atomic-implicit-seq-cst -Wno-framework-include-private-from-public -Wno-atimport-in-framework-header -Wno-quoted-include-in-framework-header -Wno-error --std=c++17 -fdeprecated-macro -fdebug-compilation-dir /Users/user/workspace/examples -ferror-limit 19 -fmessage-length 149 -stack-protector 1 -fblocks -fencode-extended-block-signature -fregister-global-dtors-with-atexit -fobjc-runtime=ios-9.0.0 -fcxx-exceptions -fexceptions -fmax-type-align=16 -fdiagnostics-show-option -fcolor-diagnostics -o /var/folders/9k/c6qvlvt10wg3g5vyzf009lxw0000gn/T/test_move-3416bc.o -x c++ test_move.cpp
clang -cc1 version 10.0.1 (clang-1001.0.46.4) default target x86_64-apple-darwin18.5.0
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator12.2.sdk/usr/include/c++/v1"
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator12.2.sdk/usr/local/include"
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator12.2.sdk/Library/Frameworks"
include "..." search starts here:
include <...> search starts here:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/10.0.1/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator12.2.sdk/usr/include
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator12.2.sdk/System/Library/Frameworks (framework directory)
End of search list.
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -lto_library /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/libLTO.dylib -no_deduplicate -dynamic -arch x86_64 -ios_simulator_version_min 9.0.0 -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator12.2.sdk -o a.out /var/folders/9k/c6qvlvt10wg3g5vyzf009lxw0000gn/T/test_move-3416bc.o -lc++ -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/10.0.1/lib/darwin/libclang_rt.ios.a
$
$
$ ./a.out
dyld: mach-o, but built for simulator (not macOS)
Abort trap: 6

Use Homebrew's clang from the command line

I used brew install llvm to install LLVM 8 and I want to use the command line tools. I have set the path to the clang executable (in /usr/local/opt/llvm/bin) as suggested by Homebrew and also set LDFLAGS and CPPFLAGS as they suggest. When I compile a simple program I get
x.c:1:10: fatal error: 'stdio.h' file not found
#include <stdio.h>
^~~~~~~~~
1 error generated.
Reading the the docs for xcrun and xcode-select I feel there should be a simple way to select my LLVM toolchain but I can't figure it out. I have tried changing the TOOLCHAINS and SDKROOT with no success.
Output of clang -v -c x.c for Homebrew's llvm-8
clang version 8.0.0 (tags/RELEASE_800/final)
Target: x86_64-apple-darwin18.5.0
Thread model: posix
InstalledDir: /usr/local/opt/llvm/bin
"/usr/local/Cellar/llvm/8.0.0/bin/clang-8" -cc1 -triple x86_64-apple-macosx10.14.0 -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name x.c -mrelocation-model pic -pic-level 2 -mthread-model posix -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu penryn -dwarf-column-info -debugger-tuning=lldb -ggnu-pubnames -target-linker-version 409.12 -v -coverage-notes-file /tmp/x.gcno -resource-dir /usr/local/Cellar/llvm/8.0.0/lib/clang/8.0.0 -fdebug-compilation-dir /tmp -ferror-limit 19 -fmessage-length 80 -stack-protector 1 -fblocks -fencode-extended-block-signature -fregister-global-dtors-with-atexit -fobjc-runtime=macosx-10.14.0 -fmax-type-align=16 -fdiagnostics-show-option -fcolor-diagnostics -o x.o -x c x.c
clang -cc1 version 8.0.0 based upon LLVM 8.0.0 default target x86_64-apple-darwin18.5.0
ignoring nonexistent directory "/usr/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/local/include
/usr/local/Cellar/llvm/8.0.0/lib/clang/8.0.0/include
/System/Library/Frameworks (framework directory)
/Library/Frameworks (framework directory)
End of search list.
x.c:1:10: fatal error: 'stdio.h' file not found
#include <stdio.h>
^~~~~~~~~
1 error generated.
Output of system clang:
Apple LLVM version 10.0.1 (clang-1001.0.46.3)
Target: x86_64-apple-darwin18.5.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.14.0 -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name x.c -mrelocation-model pic -pic-level 2 -mthread-model posix -mdisable-fp-elim -fno-strict-return -masm-verbose -munwind-tables -target-sdk-version=10.14 -target-cpu penryn -dwarf-column-info -debugger-tuning=lldb -target-linker-version 450.3 -v -coverage-notes-file /tmp/x.gcno -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/10.0.1 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk -I/usr/local/include -Wno-atomic-implicit-seq-cst -Wno-framework-include-private-from-public -Wno-atimport-in-framework-header -Wno-quoted-include-in-framework-header -fdebug-compilation-dir /tmp -ferror-limit 19 -fmessage-length 80 -stack-protector 1 -fblocks -fencode-extended-block-signature -fregister-global-dtors-with-atexit -fobjc-runtime=macosx-10.14.0 -fmax-type-align=16 -fdiagnostics-show-option -fcolor-diagnostics -o x.o -x c x.c
clang -cc1 version 10.0.1 (clang-1001.0.46.3) default target x86_64-apple-darwin18.5.0
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/local/include"
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/Library/Frameworks"
#include "..." search starts here:
#include <...> search starts here:
/usr/local/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/10.0.1/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/System/Library/Frameworks (framework directory)
End of search list.
You should either install missing headers/libraries as noted in this question.
Alternatively, you should point clang to another 'sysroot', e.g.:
clang -isysroot `xcrun --show-sdk-path` hello.c

Why can’t Clang find the library I specified on the command line?

I’m writing some unit tests for a dynamic library on OS X. The dynamic library is in ../MathDll. The library itself is built via the following Makefile:
all: math.dylib
math.dylib: MathDll.cpp MathDll.h
clang -dynamiclib MathDll.cpp -o math.dylib
clean:
rm math.dylib
And running file math.dylib gives
math.dylib: Mach-O 64-bit dynamically linked shared library x86_64
I have a simple program, test1.cpp, that will call one of the functions from this library. I tried to compile it with
clang -I../MathDll -L../MathDll -lmath test1.cpp
but this gives me
ld: library not found for -lmath
clang: error: linker command failed with exit code 1 (use -v to see invocation)
When I invoke Clang with -v, the output is
Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin13.0.0
Thread model: posix
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.9.0 -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name test1.cpp -mrelocation-model pic -pic-level 2 -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 224.1 -v -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/5.0 -I ../MathDll -stdlib=libc++ -fdeprecated-macro -fdebug-compilation-dir "/Users/bdesham/Projects/New GUI/math_library/osx_test" -ferror-limit 19 -fmessage-length 118 -stack-protector 1 -mstackrealign -fblocks -fobjc-runtime=macosx-10.9.0 -fobjc-dispatch-method=mixed -fobjc-default-synthesize-properties -fencode-extended-block-signature -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -o /var/folders/gc/4w1rdgzx3zz2dbxf0m_yd67c0000gn/T/test1-sL5rwc.o -x c++ test1.cpp
clang -cc1 version 5.0 based upon LLVM 3.3svn default target x86_64-apple-darwin13.0.0
ignoring nonexistent directory "/usr/include/c++/v1"
#include "..." search starts here:
#include <...> search starts here:
../MathDll
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1
/usr/local/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/5.0/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
/usr/include
/System/Library/Frameworks (framework directory)
/Library/Frameworks (framework directory)
End of search list.
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -dynamic -arch x86_64 -macosx_version_min 10.9.0 -o a.out -L../MathDll -lmath /var/folders/gc/4w1rdgzx3zz2dbxf0m_yd67c0000gn/T/test1-sL5rwc.o -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/5.0/lib/darwin/libclang_rt.osx.a
ld: library not found for -lmath
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I must be doing something silly here. What do I need to do to make the linker find my library?
Your library needs to be called libmath.dylib, not just math.dylib.

Resources