using command-line clang, OS X 10.9.1, Xcode 5.0.2 - macos

I'd like to use the -fsanitize=address features of clang.
I'm on:
OS X 10.9.1 with Xcode 5.0.2
Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)
I'm running clang from the command line, and I'm getting the error:
clang: error: unsupported argument 'address' to option 'fsanitize='
I'm told this works, but searching around hasn't shown me the way; do I need to bring in a different version of clang?
Thanks!

The out-of-box version of clang on OS X does not implement support of Address Sanitizer. You will have to build your own version of clang from sources.

You need a different build of clang than the one apple gave you. The simplest way to get one (or more) is to install Homebrew http://brew.sh
and then
brew install llvm --with-asan
You could also try a prebuilt clang from http://llvm.org/releases/download.htm.
It's by no means impossible to build clang 'by hand', but last I checked it did take multiple checkouts to get the source tree. The other options should have you asaning in minutes.

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"

GDB debugging x86 Assembler code on OS X Mavericks

I've been trying to find an answer to this on stackoverflow for about an hour now, seems that a lot of similar problems are around but none really fitting to mine.
Information about what tools I'm using can be found further down!
I am writing my own compiler for a subset of the java language and thus creating assembler code. Now I'm at a point where I need to debug said assembler code to locate a bug. The problem is when I compile my assembler code to a binary with gcc -m32 -g myAssembler.s I get the following warning:
warning: no debug symbols in executable (-arch i386)
(This warning also occurs using -ggdb, -ggdb2, -ggdb3, -g2, -g3instead of -g
Since there are no debug symbols I can't use gdb to debug my code. I don't know much about linking and how it's done or who does it (especially on a Mac), so precise/noob-friendly answers would be very welcome.
Tools I'm using:
The assembly created from my compiler is x86, 32-bit GAS Syntax.
I am using OS X Mavericks with GNU gdb (GDB) 7.6 downloaded via MacPorts (They changed its name to ggdb. The ggdb --version command shows also a line saying :
This GDB was configured as "x86_64-apple-darwin13.0.0".
(Not quite sure if that's important.)
Running gcc --version returns:
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin13.0.0
Thread model: posix
This is probably connected with Apple dropping support for GDB in Xcode 5 and you are using the version of gcc that comes with Xcode 5. You probably need to change compiler or debugger (to lldb)… you might look at llvm-gcc (which Xcode 5 also drops support for). Try www.llvm.org for a download. But that might be more trouble than using lldb (if your problem is indeed connected to Xcode 5's changes). Good luck! – CRD
I just downloaded gcc-4.2 from homebrew and that seems to work for lldb (which is still supported by Xcode 5 it seems). – Octoshape

Installing Clang on Mac

This is the first time I am working on Mac and I wanted to install the latest version of Clang, so I installed Xcode 5 and Xcode command line tools.
But the Clang version I got from the Xcode (in the terminal, I typed clang --version). It gives me following output:
clang++ --version
Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin12.5.0
Thread model: posix
I am confused what Clang version it is. I read that the latest Clang is version 3.5. Is the one above similar? (I need complete support for C++11). What is the difference?
Apple uses its own version numbers for Clang and LLVM which correspond with the Xcode version they are bundled with.
The actual Clang version is in indicated by the (based on LLVM X.Xsvn) part. So you have version 3.3, which is the most recent official release as you can see from the LLVM website. That version implements all the major C++11 features as you can see from the C++ support status page.

Why can't I check my version of GCC compiler on OS X

I typed this in my terminal:
llvm-gcc --version
But what I have is this :
Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin13.0.0
Thread model: posix`
Am I not able to interpret this block of statements? or did I forget to enable something on my system?
Nothing is wrong. The situation is just a little confusing.
Apple no longer distributes GCC with Xcode. They now use Clang as the default (and only!) compiler on Mac OS X. cc, gcc, and clang (as well as the C++ variants, c++, g++, and clang++) are now all linked to run clang under current versions of Xcode.
The version of clang you're running is, per the --version output, 500.2.79.
If you want to install GCC as an alternative to clang, you can do so using a variety of methods, including one using Homebrew:
https://github.com/mxcl/homebrew/wiki/Custom-GCC-and-cross-compilers

How can I update clang to 3.3 on Mac OS X 10.6

I'm running Mac OS X 10.6 and want to install TextMate 2, which is for Mac OS X 10.7+.
But all what it needs is a newer version of clang (LLVM), which is included in Lion and Mountain Lion. I read before here I can do that with MacPorts. So I used port install clang and MacPorts updated to clang-2.9, then clang-3.2 and finally clang-3.3. I thought, its updated now and I checked the version: clang --version. And it's not updated:
Apple clang version 2.0 (tags/Apple/clang-139) (based on LLVM 2.9svn)
Target: x86_64-apple-darwin10
Thread model: posix
So I copied the new clang file in this path (/opt/local/libexec/llvm-3.3/bin) to/usr/bin`. But now there's a fail :( :
dyld: Library not loaded: #executable_path/../lib/libLLVM-3.3svn.dylib
Referenced from: /usr/bin/clang
Reason: image not found Trace/BPT trap
The libLLVM-3.3svn.dylib is at /opt/local/libexec/llvm-3.3/lib. What can I do now, that
it runs clang-3.3? Sorry for my not perfect english ;) Thank you!
If you run clang from /usr/bin, it will be looking for the LLVM library in /usr/bin/../lib/libLLVM-3.3svn.dylib, i.e., /usr/lib/libLLVM-3.3svn.dylib (at least that's what the dynamic loader is telling you). You could try to copy the lib file into /usr/lib.
Alternatively, you can just download a more recent official build of Clang here:
http://llvm.org/releases/download.html
As far as I know, these builds are self-contained and do not need any dynamic libraries. However, it could be necessary to copy Apple's ARC libraries into a certain directory. If you run into trouble, please ask again.
Another approach is to build Clang and LLVM from source. You can find the repository URLs and instructions here:
http://clang.llvm.org/get_started.html
This should also build Clang with the LLVM libraries statically linked.
3.3, by the way, has not been officially released, so I would recommend using 3.2 unless you need any specific new features.

Resources