ld: file not found: /Library/Developer/.../libclang_rt.ubsan_osx_dynamic.dylib - xcode

I'm trying to use the Undefined Behavior Sanitizer by building like this
gcc -fsanitize=undefined add.c -o add
also
clang -fsanitize=undefined -O add.c -o add
In both cases, I'm getting a file not found error:
ld: file not found: /Library/Developer/CommandLineTools/usr/bin/../lib/clang/8.0.0/lib/darwin/libclang_rt.ubsan_osx_dynamic.dylib
This is the output I get when running gcc -v and clang -v
Apple LLVM version 8.0.0 (clang-800.0.42.1)
Target: x86_64-apple-darwin15.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 8.0.0 (clang-800.0.42.1)
Target: x86_64-apple-darwin15.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
According to this news release, it is available in GCC and also the original homepage for it says it's been merged into LLVM. The linked to article says GCC 4.9 has it, which I assume I have (at least --the version numbering seems different but the article was written a few years ago and I have updated my system several times).
Question: how can I build an executable to use UBSan?

According to blog Friday Q&A 2015-07-03: Address Sanitizer
by Mike Ash:
Any idea why Apple clang doesn't support -fsanitize=undefined?
When I try that I get:
clang -fsanitize=undefined ub.c
...
ld: file not found: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/
bin/../lib/clang/8.0.0/lib/darwin/libclang_rt.ubsan_osx_dynamic.dylib
Spencer at 2016-12-21 01:23:59:
If you use -fsanitize=address,undefined it works. Hopefully this
helps anyone else who finds this post search for that error like I did.
Its not an ideal solution for us since they are separate components, and we have limited control over the environment and tools. We had to suspend some Address Sanitizer testing because Asan produces incorrect results with GCC inline assembly and use of ebp/rbp as a general purpose register. And here, the environment and tools are provided by Travis CI.
A bug report was filed with LLVM at Issue 33201, Apple Clang bundled with Xcode 8 does not reject -fsanitize=undefined when it should. It may be closed as off-topic since its an Apple Clang bug (we don't have an iTunes account to file the bug with Apple).

In order to use the undefined behavior sanitizer, I had to install and build LLVM with clang as well as libcxx and libcxxabi, even though my MacOS already came with a version of clang. (with the result that I now I have two versions of clang installed on my computer). From the llvm getting started page
cd where-you-want-llvm-to-live
svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm
cd where-you-want-llvm-to-live
cd llvm/tools
svn co http://llvm.org/svn/llvm-project/cfe/trunk clang
cd where-you-want-llvm-to-live
svn co http://llvm.org/svn/llvm-project/libcxx/trunk libcxx
svn co http://llvm.org/svn/llvm-project/libcxxabi/trunk libcxxabi
Now, I can use the undefined behavior sanitizer by running
/usr/local/bin/clang -fsanitize=undefined -O add.c -o add

Related

Using -march=native flag with clang on Apple M1 [duplicate]

Whenever I try to compile any C++ program with march=native on a Macbook with a M1 chip, I get the following error when using clang:
clang: error: the clang compiler does not support '-march=native'
However, it used to work on an older Macbook with an Intel CPU. Does clang not support this architecture (yet)?
clang --version gives:
Apple clang version 12.0.0 (clang-1200.0.32.29)
Target: arm64-apple-darwin20.2.0
In Apple clang version 13.0.0, -mcpu=apple-m1 is now available.
$ clang --print-supported-cpus
Apple clang version 12.0.5 (clang-1205.0.22.9)
Target: arm64-apple-darwin20.5.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
Available CPUs for this target:
a64fx
apple-a10
apple-a11
apple-a12
apple-a13
apple-a14
apple-a7
apple-a8
apple-a9
apple-latest
apple-s4
apple-s5
carmel
cortex-a34
cortex-a35
cortex-a53
cortex-a55
cortex-a57
cortex-a65
cortex-a65ae
cortex-a72
cortex-a73
cortex-a75
cortex-a76
cortex-a76ae
cortex-a77
cortex-a78
cortex-x1
cyclone
exynos-m3
exynos-m4
exynos-m5
falkor
generic
kryo
lightning
neoverse-e1
neoverse-n1
saphira
thunderx
thunderx2t99
thunderx3t110
thunderxt81
thunderxt83
thunderxt88
tsv110
vortex
-mcpu=apple-a14 might be the best one for M1.
As far as I know this is not Apple M1 specific, it occurs also with clang for various other architectures (mostly various other arm processors).
e.g. See this bug report here https://github.com/DMOJ/judge-server/issues/303
Basically every build of clang for a new architecture must choose to provide defaults for "march=native" for the target for which the compiler is built or not; and if not then you will see this error message. Even for those arm processors who do have optimisation targets you often have to use specifically "-mcpu=xxx" instead of "-march".
e.g. For the iphone you would use -mcpu=apple-a11 -mcpu=apple-a12 etc.
However no such target has been implemented yet for Apple M1
In Clang 15, -march=native now exists for Apple M1 (I personally use it).
another solutions is update clang with brew
lang: error: the clang compiler does not support '-march=native'
1° - Install llvm con brew
brew install llvm
2° - Check clang version
cd /opt/homebrew/opt/llvm
.clang --version
Result:
Homebrew clang version 15.0.6
Target: arm64-apple-darwin22.2.0
Thread model: posix
InstalledDir: /opt/homebrew/opt/llvm/bin
3° - modify path with the new version, for example add to .zshrc
export PATH="/opt/homebrew/opt/llvm/bin:$PATH"

How to use clang to create a windows binary

I have installed clang on my Windows 10 machine.
$ clang --version
clang version 6.0.1 (tags/RELEASE_601/final)
Target: x86_64-pc-windows-msvc
Thread model: posix
InstalledDir: C:\Program Files\LLVM\bin
With it, I can compile C and C++ sources and generate object files, and use lib.exe to create libraries of them.
When it's time to link it into a binary, to my surprise, clang is creating a lib/exp output, not an executable?
Why does clang think it should be creating a library, not an executable?
clang++ -oarmor XWin/main.o -lSDL2main -lpi -ldblunt -lsino -lbase -L. -L ../../src/sino -L MSWin/libs/SDL/lib ../../src/Chipmunk2D/build.release/x64/Release/chipmunk.lib MSWin/libs/OpenAL/lib/OpenAL32.lib -lSDL2 -lOpenGL32
Creating library armor.lib and object armor.exp
LINK : fatal error LNK1561: entry point must be defined
clang++.exe: error: linker command failed with exit code 1561 (use -v to see invocation)
In UNIX, clang will by default create binaries, not libraries?
Check this link -
https://metricpanda.com/rival-fortress-update-27-compiling-with-clang-on-windows
There are currently two flavors of Clang that work on Windows: vanilla LLVM Clang and Clang/C2 with Microsoft Codegen.

gcc -print-multi-os-directory error on mac

My mac version is Sierra. Xcode 8.0
gcc info
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 8.0.0 (clang-800.0.42.1)
Target: x86_64-apple-darwin16.4.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
I think gcc is not problem. My project build option is -print-multi-os-directory. but my gcc unsupported... what's the problem???
thanks
on mac your GCC is Clang as you may see in output
in clang option -print-multi-os-directory is unsupported
On current trunk (line 2079)
Was marked as unsupported here
You may try to install pure gcc via brew install gcc command
Also why do you need this option? Do you have any special checks in you project ?(this option just print path )
Since you are trying to build binutils:
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;a=blob;f=bfd/configure;h=97693870c462d5e5231bd2b512a76b7c271b9133;hb=7fa393306ed8b93019d225548474c0540b8928f7#l10348
Try to figure out why you get in condition when multi-os option is used.
This happened because your compiler passed test:

MAKE on new iMAC with CLANG, but Makefile refers to GCC

I seek some help with 2 Makefiles for 2 different software that are much older than my iMAC machine. I think the software assumes the C compiler to be GCC, while my new iMAC has CLANG as default I think...
I know there are some other threads on this general topic such as at : Compile program using a Makefile with gcc instead of clang
But I wonder if there are Makefile specific modifications for these 2 software that you can suggest I perform. I dont have any experience with modifying Makefiles or flags etc.
The 2 Makefiles and the software that they come from can be all accessed from the folder at Dropbox. Thank you!
My make attempt for mdust looks like this:
Checked http://b110-wiki.dkfz.de/confluence/display/nextrnai/Step-by-step+installation+on+MAC for details on
Installation of mdust
Mdust can be downloaded via this link.
Unpack the file. In the folder created open fastafile.c and delete the third line #include (malloc.h is not needed under Mac OS X, but leads to an error with make). Finally typemake (ignore warnings) which creates the mdust script required by NEXT-RNAi (compilation requires prior installation of Apple Developer Tools / Xcode).
tar -xvzf mdust.tar.gz
cd mdust
emacs fastafile.c (delete line include )
make
So I opened fastafile.c with vi and deleted the
line include
But now, when I try make, I think I see CLANG Vs gcc compiler issues.
DCook04-2:mdust anand$ make
gcc -O2 -Wall -I. -I- -D_REENTRANT -c fastafile.c -o fastafile.o
clang: error: '-I-' not supported, please use -iquote instead
make: *** [fastafile.o] Error 1
My make attempt for RECON may have worked, only warning, no errors per STDOUT. Longer STDOUT, so in folder with link above, and obviously named file.
More info that's hopefully relevant:
DCook04-2:src anand$ gcc -v
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin12.5.0
Thread model: posix
DCook04-2:src anand$ clang -v
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin12.5.0
Thread model: posix

Clang Code Coverage - Mac OS X - Linker Error

I could successfully get code coverage information on our C++ code base on Linux using the GCC features of GCOV and the LCOV tool.
But I am having trouble in Mac OS X.
As Apple does not have the classic GCC compiler anymore, and we fear that the LLVM-GCC compiler would one day disappear too (LLVM-GCC is not even available as an option in Xcode 5.0) - we have decided to use Clang to compile our code.
While using the Clang compiler I am passing in these flags -->
-g -fprofile-arcs -ftest-coverage to generate the Code Coverage information.
I can see the .gcno files getting generated along with the object files.
When it comes to linking - "-lgcov” linker flag which works with GCC is not supported.
The code coverage on Clang / LLVM is now supported by the “profile_rt” library.
Unfortunately it’s a bit tricky to find this library because Apple for whatever reason decided not to include it in the default library path. Instead you’ll have to manually navigate to /usr/lib/ to link against it:
And as specified am linking against libprofile_rt.a library.
But i have linker issues.
But i keep getting these linker errors
Undefined symbols for architecture x86_64:
"_llvm_gcov_init", referenced from:
___llvm_gcov_init in Iso9660Reader.o
___llvm_gcov_init in AutoExtractCreator.o
___llvm_gcov_init in TempFilePath.o
___llvm_gcov_init in TempPath.o
___llvm_gcov_init in ReadDirectory.o
___llvm_gcov_init in OpenDirectory.o
___llvm_gcov_init in SpltPath.o
...
ld: symbol(s) not found for architecture x86_64
I also tried linking against the dynamic library - libprofile_rt.dylib found in
/usr/lib folder - But i still get the same issue.
This is Clang Version running on Mountain Lion.
clang --version
Apple LLVM version 5.0 (clang-500.2.75) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin12.5.0
Thread model: posix
I also have Xcode 5.0 and Developer Tools installed.
I solved this.
I was missing the following Linker Flags
-Wall -fprofile-arcs -ftest-coverage
Other Linker Flag -fprofile-arcs fixes the issue for me.
Build Settings > Other Linker Flags > -fprofile-arcs
The above answer did not work for me on OSX Yosemite (10.10.3) with Xcode 6.3.1. It seems that Apple moved these libraries around. I was able to get it to work with the following compile options:
-lclang_rt.profile_osx
-L/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/6.1.0/lib/darwin

Resources