LLVM & Clang can't compile for a supported arch

25,721

Solution 1

As this comment says this option it's not supported yet under linux, for now.

Solution 2

-march is LLVM's internal tools command line option and is not connected with clang at all. If you need to compile for other target you need to specify the target triplet. This can be done in several ways (I do not remember offhand, whether they work with 3.1, but they definitely work with 3.2):

  • Make a link from clang to your-target-triple-clang, e.g. to arm-none-linux-gnueabi-clang and compile everything via it
  • Provide -target option, e.g. clang -target arm-none-linux-gnueabi

Solution 3

To get a list of options of the clang compiler, use:

clang -cc1 -help

To specify the target, use -triple:

clang -cc1 -triple "arm-vendor-os" filename

where "vendor" and "os" should be replaced with the actual vendor and OS name. It can also be replaced with unknown.

-triple is a string of the form ARCHITECTURE-VENDOR-OS or ARCHITECTURE-VENDOR-OS-ENVIRONMENT. For example: x86_64-apple-darwin10

Solution 4

the llvm linker links for the host, which is only one of the targets, it wont link to every target in the list. it will definitely compile for any target. Basically clang goes from C/C++ to bytecode, then llc takes bytecode and makes assembly for the specific target (new experrimental option to take the bytecode straight to object file) then you need to get a cross assembler and a cross linker to take it the final mile (I use gnu binutils). Unfortunately I found that clang to bytecode is not completely generic (I had hoped and expected that it would be), it does in fact change the target independent output based on the target. The example below using the host triple instead of using -march allowed for my examples to build properly on more hosts.

ARMGNU?=arm-none-eabi
LOPS = -Wall -m32 -emit-llvm -ccc-host-triple $(ARMGNU)
OOPS = -std-compile-opts
LLCOPS = -march=thumb -mtriple=$(ARMGNU)

    clang $(LOPS) -c blinker03.c -o blinker03.clang.bc
    opt $(OOPS) blinker03.clang.bc -o blinker03.clang.thumb.opt.bc
    llc $(LLCOPS) blinker03.clang.thumb.opt.bc -o blinker03.clang.thumb.opt.s
    $(ARMGNU)-as blinker03.clang.thumb.opt.s -o blinker03.clang.thumb.opt.o
    $(ARMGNU)-ld -o blinker03.clang.thumb.opt.elf -T memmap vectors.o blinker03.clang.thumb.opt.o

I have not, but before long will experiment with using the llc straight to object (actually I tried it on a simple test but have not used it on anything larger or posted it anywhere).

Solution 5

You're confusing your flags. clang's -march= wants a processor family. You probably meant to use clang -arch arm instead.

Share:
25,721
user1849534
Author by

user1849534

Updated on July 05, 2022

Comments

  • user1849534
    user1849534 almost 2 years

    Under Ubuntu 64 bit I got

    llc --version
    LLVM (http://llvm.org/):
      LLVM version 3.1
      Optimized build with assertions.
      Built Oct 15 2012 (18:15:59).
      Default target: x86_64-pc-linux-gnu
      Host CPU: btver1
    
      Registered Targets:
        arm      - ARM
        mips     - Mips
        mips64   - Mips64 [experimental]
        mips64el - Mips64el [experimental]
        mipsel   - Mipsel
        thumb    - Thumb
        x86      - 32-bit X86: Pentium-Pro and above
        x86-64   - 64-bit X86: EM64T and AMD64
    

    I can't do this

    clang -march=arm -x c++ /tmp/cpp.cpp 
    error: unknown target CPU 'arm'
    

    I'm missing something here ? Why I can't compile for ARM ?

  • user1849534
    user1849534 over 11 years
    clang -arch arm -x c++ /tmp/cpp.cpp that leads to clang: warning: argument unused during compilation: '-arch arm', this doesn't solve my problem
  • Lily Ballard
    Lily Ballard over 11 years
    @user1849534: Curious. What version of clang are you using? My clang-421 has the -arch flag.
  • user1849534
    user1849534 over 11 years
    are you on linux ? I'm using the version of clang included in the Android NDK r8c for Linux
  • Lily Ballard
    Lily Ballard over 11 years
    @user1849534: No, I'm on OS X, so I'm using the clang that ships with Xcode. A quick google suggests that Android NDK r8c comes with Clang 3.1, whereas I'm using Clang 4.1.
  • Some programmer dude
    Some programmer dude over 11 years
    Clang 4.1 is a OSX specific version, based on the 3.1 subversion tree according to this question/answer.
  • old_timer
    old_timer over 11 years
    not shown above but by compiling each source file to ir then using llvm-link to combine them, you can optimize the entire application rather than optimize each individual file, in theory resulting in something better, though in practice gnu is still producing slightly faster code (optimizing one file at a time). Understand my benchmarks were very limited (but have shown over time gcc is getting worse and llvm is getting better for at least one open source library).
  • Anton Korobeynikov
    Anton Korobeynikov over 11 years
    -arch is darwin-only option.
  • LeoTh3o
    LeoTh3o over 10 years
    @Anton now -arch should be available also on "standard" clang. I believe I have used it in Ubuntu with clang.
  • Anton Korobeynikov
    Anton Korobeynikov over 10 years
    @LeoTh3o - it maybe Ubuntu-local stuff. The proper way is using -target
  • srking
    srking over 9 years
    Hint about using 'thumbv7' helped. +1 from me.
  • Knut
    Knut over 8 years
    clang (3.6) has no option -triple -> error: unknown argument (also not in -help)
  • Serge Rogatch
    Serge Rogatch almost 8 years
    Isn't the option indeed --target (with 2 leading hyphens)?
  • Celuk
    Celuk about 2 years
    @SergeRogatch Both are valid, but with leading 1 hypen you need a space between keywords, e.g. -target arm-none-linux-gnueabi, with leading 2 hypens you need = sign, e.g. --target=arm-none-linux-gnueabi