1

On MacOS 14.2, typing either 'clang --version' or 'gcc --version' (or even 'cpp --version') returns the same:

Apple clang version 15.0.0 (clang-1500.1.0.2.5)
Target: arm64-apple-darwin23.2.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

What is the difference between cpp, gcc, clang? 'ls -la /usr/bin' tells me at least none of them are symlinks.

3
  • Removed the link to the training , it was simply the inspiration for the question (see example starting slide 28 of PDF). I believe the reasons are to demonstrate the generality of compilers, examine the pre-processing step, as well as isolate the use of macros for beginning students. Commented Dec 22, 2023 at 21:20
  • the motivation to this question was a simpler question resolved on SO, which may add a little context to this very broad prompt. i thought i had found a difference in the behaviors of the listed binaries, but i was using gcc/clang incorrectly. i think the answers found here could still be useful to future users. Commented Dec 24, 2023 at 2:11
  • stackoverflow.com/questions/59905104/… might help with the other question. gcc -nostartfiles -Wl,-e,_mymain main.c compiles and links (and can be successfully executed). Commented Dec 24, 2023 at 16:25

1 Answer 1

4

With regard to, "What is the difference between cpp and these other binaries (gcc and clang)? 'ls -la /usr/bin' clearly demonstrates none of them are symlinks."

/usr/bin/clang, /usr/bin/gcc and /usr/bin/cpp are hardlinks to the same binary. An ls -il of the three will show the same inode number, an equivalent link-count (e.g. 76) and a 256shasum hash for each with the same value.

$ ls -il /usr/bin/clang /usr/bin/gcc /usr/bin/cpp    
1152921500312436371 -rwxr-xr-x  76 root  wheel  167136 Dec  2 02:00
  /usr/bin/clang
1152921500312436371 -rwxr-xr-x  76 root  wheel  167136 Dec  2 02:00
  /usr/bin/cpp
1152921500312436371 -rwxr-xr-x  76 root  wheel  167136 Dec  2 02:00
 /usr/bin/gcc

$ shasum -a 256 /usr/bin/clang /usr/bin/gcc /usr/bin/cpp    
ca08407ee7e1a179d096fe27b8edef2a24cb33402f6338654e1b25589df9a4ba
  /usr/bin/clang
ca08407ee7e1a179d096fe27b8edef2a24cb33402f6338654e1b25589df9a4ba
  /usr/bin/gcc
ca08407ee7e1a179d096fe27b8edef2a24cb33402f6338654e1b25589df9a4ba
  /usr/bin/cpp

Thus, when executing any of these, the name chosen for execution affords the option of allowing different behavior for the same file.

18
  • Thanks! I had no idea about hardlinks. Is it documented anywhere what the different execution behaviors are for these particular ones (clang, gcc, cpp)? A totally tangential question this raises for me is how to generally inspect the execution properties of a hardlink? Commented Dec 22, 2023 at 21:16
  • 1
    The program is able to determine which was called and then choose different behavior based on invocation. The program could be called foo and invoked as foo -clang and foo -gcc instead of being hard links and perform the same operation @shea Commented Dec 22, 2023 at 22:27
  • @shea There's no way to inspect it. It's just part of the code of the program. It checks what name it was run with, and adjusts its behavior accordingly. The actual software is clang, but when it sees that its name is gcc it implements parameter processing compatible with GCC. Commented Dec 23, 2023 at 0:03
  • 3
    @seamus Having one binary hardlinked under different names (and having slightly different behavior depending on the value of $0) is not uncommon (historically, it helped to save disk space used by the OS). more/less are often hardlinked, also rm/unlink or the various versions of grep (ls -il /usr/bin/{bz,}{e,f,}grep). Run ls -il /usr/bin | sort | fgrep -v 'r-x 1' for the whole list. Commented Dec 24, 2023 at 9:37
  • 1
    In nohillsides comment more/less and rm/unlink both links behave the same whilst the grep ones show one executable behaving in different ways. Commented Dec 24, 2023 at 10:13

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.