a06e
a06e

Reputation: 20774

Is Clang as (or more) portable than gcc for C++?

Suppose I have a C++ project, and I compile it with gcc and with clang. You can assume that the gcc compiled version runs in another linux machine. Will this imply (in normal circumstances) that the clang version will also run on the other linux machine?

Upvotes: 3

Views: 1921

Answers (4)

Thomas
Thomas

Reputation: 3225

Clang binraries are as portable as gcc binaries are, as long as you are linking to the same libraries and you aren't passing flags like -march=native to the compiler.

Clang has one huge advantage over gcc, it can deal with alsmost all libstdc++ versions, while gcc is bound to its bundled version and often can't parse any older versions.

So the following often happens in production environments:

  • Install an LTS distro (Ubuntu 12.04 for example)
  • Keep gcc, glibc and libstdc++ untouched
  • Install a recent clang version for C++11, etc
  • Build the release binaries with clang

So (in my specific example) those binaries will work on all distros with libstdc++ >= 4.6 and glibc >= 2.15.

This may be an interesting read for you.

Upvotes: 2

Serge Ballesta
Serge Ballesta

Reputation: 149085

If the program is a simple Hello world, it should work on the other machine when compiled through Clang.

But when the program is a real program with a lot a lines and compilation units, and calls to many external libs everything is possible depending on the program itself and the compilation options :

  • hardware requirements (memory) being different (mainly depends on compilation options)
  • use of different (versions of) libraries between gcc and clang
  • UB giving expected results in one and not in the other
  • different usages for implementation defined rules
  • use of gcc extensions not accepted by clang

For all of the above except 2 first, it should run on other machines it it runs on one

Upvotes: 2

Dakkaron
Dakkaron

Reputation: 6276

The answer is, well, depends.

The first hard requirement is the same CPU architecture. 64 Bit is not enough of a qualifier. If you compile of x64 you won't have much success running it on 64-Bit ARM.

The next big one is libraries. If you use any libraries in the program, the target system needs to have those libraries. This includes the kernel headers. So if you compile for e.g. a current kernel version, using the most cutting-edge features, then you will have no joy running that program on a very old version of Linux.

The last one is hardware dependencies. If you create a program that e.g. requires 4 GB of RAM and then try to run it on a small embedded device with 256 MB RAM, that won't work either.

To fit better to your changed question: From my experience there shouldn't be much of a difference in portability between Clang and gcc. Also googling didn't turn up anything, so it should basically work. But better always test stuff like that before you publish some binary in production.

Upvotes: 2

clambake
clambake

Reputation: 822

linux programs depend on their build environment. If your glibc version or kernel is different there will be lots of possibilities that the executable will not be able to run. You could use the interpreter language of llvm though, it compiles into bytecode which can be interpreted on various operating systems.

Upvotes: 1

Related Questions