Generating object files more efficiently

Currently I compile my C code in 2 steps in order to generate .o files

clang -emit-llvm -c foo.c -o foo.bc

llc -march=XYZ foo.bc -filetype=obj

Is there a way to generate either .o or .elf files in just 1 command?


I would have guessed:

clang -c foo.c -o foo.o -march=XYZ

shared library:
clang -shared foo.c -o -march=XYZ

clang foo.c -o foo -march=XYZ


I tried the last one and it gave me this:

error: unknown target CPU ‘XYZ’
note: valid target CPU values are: nocona, core2, penryn, bonnell, atom,
silvermont, slm, goldmont, goldmont-plus, tremont, nehalem, corei7,
westmere, sandybridge, corei7-avx, ivybridge, core-avx-i, haswell,
core-avx2, broadwell, skylake, skylake-avx512, skx, cascadelake,
cannonlake, icelake-client, icelake-server, knl, knm, k8, athlon64,
athlon-fx, opteron, k8-sse3, athlon64-sse3, opteron-sse3, amdfam10,
barcelona, btver1, btver2, bdver1, bdver2, bdver3, bdver4, znver1, znver2,

I copied “-march=XYZ” from your original email,

you have to replace it with your actual target architecture or simply drop it.

It is my actual target architecture

I think you need to pass a triple for your target to clang using --target otherwise it will use your host target.

Oh, my bad.

Idk why llc seems to know that architecture but clang does not.

Doesn’t make sense to me, sorry.


clang --target=XYZ x.c -o foo.elf

error: unknown target triple ‘XYZ’, please use -triple or -arch

-triple and -arch didn’t work either

-march for clang and -march for llc do different things unfortunately. -march for clang at least on x86 is the same as -mcpu in llc. Which is an artifact of gcc compatibility.


In order for clang to support a new target you need to add support in lib/Basic/Targets.cpp in the AllocateTarget function and add new files to the lib/Basic/Targets/ directory.


Thanks, Craig,

FYI, I also had to update clang/lib/Basic/CMakeLists.txt

How can we pass compiler options to clang? I’m trying to pass this option -mcpu=mycpu but it’s not taking it.
When I print the Opts->CPU string in TargetInfo::CreateTargetInfo(), I get an empty string.

clang --target=XYZ -mcpu=mycpu x.c -o foo.elf