3

I am trying to learn C and just started the book "Learn C the Hard Way." In the second lesson the author mentions Makefiles and states that when using the command "make example.c", 'make' will automatically call cc to build the .c file. Out of curiosity, I started to investigate cc since I was only familiar with gcc. According to user Jonathan Leffler on SO's post "Difference between CC, gcc and g++?", cc usually links to gcc in Linux.

So here are my questions:

1) How does cc link to gcc in Linux? It seems like a simple question but I can't tell if it would require a basic answer or an advanced OS answer. (It seems that cc isn't an environmental variable as one user claimed on SO after checking with "env >> env_vars.txt" on Red Hat Linux and then reading the contents of the text file env_vars.txt.)

2) How do we know the internal details of what 'make' will do when it encounters a .c file (or any other file)? For example, in the case of .c files, how exactly can we know or confirm that 'make' will call cc as opposed to directly calling gcc with a .c file (or not)? (Does 'make' reference a default Makefile? If so, how can we read the contents of this default Makefile?)

1 Answer 1

8

I can't answer for all Linuxes, but hopefully this will give you enough to determine the answers for your own system.

  1. How does cc link to gcc

We can use

$ type cc cc is /usr/bin/cc 

So what is /usr/bin/cc?

$ ls -l /usr/bin/cc lrwxrwxrwx 1 root root 20 Jun 29 2016 /usr/bin/cc -> /etc/alternatives/cc 

so it's a symbolic link - but one that's managed by the alternatives system. We can query that:

$ update-alternatives --query cc Name: cc Link: /usr/bin/cc Slaves: cc.1.gz /usr/share/man/man1/cc.1.gz Status: auto Best: /usr/bin/gcc Value: /usr/bin/gcc Alternative: /usr/bin/clang Priority: 10 Slaves: Alternative: /usr/bin/gcc Priority: 20 Slaves: cc.1.gz /usr/share/man/man1/gcc.1.gz 

So cc is indeed linked to /usr/bin/gcc.

Note that /usr/bin/gcc may itself be a symbolic link - to s specific version of gcc:

$ ls -l /usr/bin/gcc lrwxrwxrwx 1 root root 5 Feb 11 2016 /usr/bin/gcc -> gcc-5 
  1. How do we know the internal details of what 'make' will do when it encounters a .c file

Given a file hello.c then we can check what make will do using the -n option:

$ make -n hello cc hello.c -o hello 

Beyond that, you can refer to the documentation for your system's version of make - for example GNU make: Catalogue of Rules

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much for your reply. I spent hours on the Linux 'man'/manual pages and with Google searches to no avail and appreciate your explanation and information.
A process can read the command that invoked it. This means it can distinguish if it was called as cc or gcc and do different things depending on how you started it. You may not be able to replace cc with gcc. Check the man page. At the moment, man ccgives you the man page to gcc, so they are currently interchangeable.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.