I’m confused about the clang --target, -mcpu, -march
Can someone give a clear explanation on what is the difference between them?
I can try
Originally I thought need to specify -mcpu (which I assume means CPU) and -march but then I can’t figure out how --target fits into the picture. Sometimes it tells me that -march or -mcpu options is not used. I would really appreciate any help on this.
Let’s say you’re on an x86_64-linux-gnu machine (basically any linux machine) and you want to compile some code for a haswell specific binary, you’d use:
clang -march=haswell foo.c
Which tells clang to use the current host as the “OS” for your compile and to tell the backend to generate haswell specific code.
Let’s say you instead want to compile for arm-linux-gnu (just your basic arm linux machine), you’d use:
clang -target arm-linux-gnu foo.c
I’m glossing over this a bit because it’s not relevant to what you asked, but the other options there are going to be things like a sysroot where you can get the headers and libraries for an arm-linux-gnu machine, because they’re likely not installed on your x86_64-linux-gnu machine.
Now, let’s say you want to target a specific arm processor as part of your cross compile, you’d do this:
clang -target arm-linux-gnu foo.c -mcpu=armv7
or you could do this:
clang -target armv7-linux-gnu foo.c
this is partially because of legacy reasons, and partially because arm is weird here. The -mcpu and -march options are very similar in clang. In general, they’re setting the processor you’re compiling your code to run on. (Arm is even weirder here so I’m glossing over a lot of details).
The reasons behind this are largely historical and option compatibility with gcc. If you’ve done cross compilation with gcc all that the --target option does in clang is select at runtime the same thing that you’d use --target for on the configure line.