2

Today I accidentally added the extension '.c' on a CPP program (with namespaces, classes and whatnot inside) and passed it to g++. It compiled it without an issue, but shouldn't it have treated it as a C Program and throw an error or a warning? On other threads I read that the extension for g++ doesn't matter (some suggest using any arbitrary extension that's not taken, however I tried other extension asides from the standard .c,.C,.cpp etc) and they are not recognised.

So, what exactly happens here with the extensions? Was my cpp program compiled as a cpp program or as a c one?

6
  • how did you invoke the compiler? What exactly did you type on your command line? Commented Oct 15, 2020 at 15:42
  • What happens if you use the gcc toolchain driver instead of the g++ toolchain driver? Commented Oct 15, 2020 at 15:43
  • 1
    Does this anwer your question? stackoverflow.com/questions/172587/… Commented Oct 15, 2020 at 15:43
  • 1
    I just compiled it using g++ like so, g++ -o pr.o pr.c and it compiled fine. Also, using GCC won't work at all (meaning it won't compile). Worth noting I compiled it on WSL, not sure if it makes a difference. Commented Oct 15, 2020 at 15:47
  • The answer on the proposed dupe explains what flags on gcc are more or less equivalent to calling just g++ Commented Oct 15, 2020 at 15:49

2 Answers 2

2

It compiled it without an issue, but shouldn't it have treated it as a C Program

No, it should't've. g++ treats files with .c suffix as C++ sources by default.

Was my cpp program compiled as a cpp program?

Yes.

On other threads I read that the extension for g++ doesn't matter

This is not exactly correct. g++ does detect language based on the file suffix, but if C is detected, then C++ is used to compile the file instead by default. The default can be overridden by specifying the -x LANG option. g++ -x none will detect language in same way as gcc would.

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

2 Comments

So, regardless g++ will compile any compatible source file as a c++ program. However, it won't compile if suffix is something like .py for instance, so where does the idea that it doesn't care about suffixes come from? Referring to this: stackoverflow.com/questions/31369583/…
@pol Probably from the fact that .c is compiled as C++. If you only ever test .c and .cpp suffixes, then you might make the misleading conclusion that the suffix doesn't matter. Or, the answer may have been written from perspective that if you specify the language to the compiler then the suffix doesn't matter. Indeed, the suffix only matters in case the language is deduced.
0

Since you haven't posted your code here, I am assuming it doesn't have any special libraries included in it that are privileged only to g++ compiler. As per your query, the c++ code can be compiled by gcc just fine as it chooses the best compiler based on the file extension and the same thing can be said about g++ compiler compiling the C though it compiles both .c and .cpp codes just as c++.

Lastly, I will say that it's not good practice and can cause linker problems ( Some functions or libraries are missing errors ). Even though if you want so bad, it could work if you change the compilation line to this

gcc <file_name>.c -lstdc++

1 Comment

Actually, my current code does have iostream and namespaces inside, so it can't be compiled by gcc at all.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.