0

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?

3
  • 2
    It's common for the c++ command to be a hard link to g++ just for this convenience. cc and gcc are likewise linked. Commented Oct 26, 2021 at 23:59
  • So, is this something that operating system creators generally do? And GNU Project doesn't have any connection to this? Commented Oct 27, 2021 at 0:02
  • It's either a decision by the package creators (who make the package that you install), or possibly by the GCC project itself as part of building the application. The operating system itself wouldn't really know anything about this specially. Commented Oct 27, 2021 at 0:05

2 Answers 2

3

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++
$ 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.

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

Comments

2

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.

1 Comment

Another thing with having C be compile C code, is it's one letter. Not overly descripting (neither is awk, mind you) but for the sake of the poor humans that have to use this stuff, we need to get away from one-letter identifiers. If I'm slapping people around in code reviews for using 1 letter, I'd be a hypocrite if I didn't do the same over a 1 letter program name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.