Home > Enterprise >  Compiling a .cpp file with c command instead of g (Linux)
Compiling a .cpp file with c command instead of g (Linux)

Time:10-27

Let's say we have a simple c file named hello.cpp which prints "Hello World!".

We generally create the executable using g hello.cpp. When I tried executing the command c hello.cpp it creates the executable successfully. Shouldn't it throw an error saying there is no c command available? and suggest us to use g ?

I tried running man c on the terminal, this brings up the GNU C Project page. So, does the terminal replace our c hello.cpp with g hello.cpp internally? It shouldn't do that right?

Additional Info:
Similarly, if I have a hello.c program that prints "Hello World!". When I execute c hello.c on the command line, I get the error:

$ c hello.c
c: command not found

This is expected since we have to use gcc hello.c. Why am not getting a similar error for the c hello.cpp?

CodePudding user response:

On my Ubuntu system, I see this:

$ ls -l /usr/bin/c  
lrwxrwxrwx 1 root root 21 May  6  2019 /usr/bin/c   -> /etc/alternatives/c  
ssd@c3-0:~$ ls -l /etc/alternatives/c  
$ ls -l /etc/alternatives/c  
lrwxrwxrwx 1 root root 12 May  6  2019 /etc/alternatives/c   -> /usr/bin/g  

So c is actually an alias (symbolic link) to g

The alternatives system allows changing what compiler c is an alias to:

$ update-alternatives --display c  
c   - auto mode
  link best version is /usr/bin/g  
  link currently points to /usr/bin/g  
  link c   is /usr/bin/c  
  slave c  .1.gz is /usr/share/man/man1/c  .1.gz
/usr/bin/clang   - priority 10
/usr/bin/g   - priority 20
  slave c  .1.gz: /usr/share/man/man1/g  .1.gz

So c could actually be either g or clang

For a C compiler, you don't want the c command, but cc, which again, could be either gcc or clang.

My CentOS system isn't quite so obvious with the symlinks, but g --version and c --version give the same output and both say they are g . However, cc is a direct symlink to gcc there.

CodePudding user response:

Shouldn't it throw an error saying there is no c command available? and suggest us to use g ?

Weeelll, there are no regulations or standards that restrict or require c command to do anything, so there is no "should" or "shouldn't". However, it would be strongly expected that c is a working C compatible compiler, that supports similar or same flags as cc does.

does the terminal replace our c hello.cpp with g hello.cpp internally?

A terminal is the device that displays things. Terminal does not replace it, it has no effect on it.

Most probably your system designer, but maybe administrator or publisher or distributor or package designer (or anyone in the chain), configured your system to provide a command named c . Usually, that command is a symbolic link to a working C compiler, usually g on Linux systems. On my system, /usr/bin/c program is just installed with package named gcc, but some systems allow it to be configurable.

It shouldn't do that right?

As stated above, terminal shouldn't replace commands, it has no effect on it.

This is expected since

It is expected since there is no command named c. There are endless other unknown commands.

cc is the good old name for a C compiler. c99 is the standardized name of C99 compatible compiler.

Why am not getting a similar error for the c hello.cpp?

Because a command named c exists on your system.

  • Related