1

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?

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
Pol
  • 185
  • 9
  • how did you invoke the compiler? What exactly did you type on your command line? – 463035818_is_not_an_ai Oct 15 '20 at 15:42
  • What happens if you use the `gcc` toolchain driver instead of the `g++` toolchain driver? – Eljay Oct 15 '20 at 15:43
  • 1
    Does this anwer your question? https://stackoverflow.com/questions/172587/what-is-the-difference-between-g-and-gcc – 463035818_is_not_an_ai Oct 15 '20 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. – Pol Oct 15 '20 at 15:47
  • The answer on the proposed dupe explains what flags on gcc are more or less equivalent to calling just g++ – 463035818_is_not_an_ai Oct 15 '20 at 15:49
  • So, from what I can understand after all it will compile any compatible file as a .cpp program. This thread however says that g++ would accept any file extension not already 'taken: https://stackoverflow.com/questions/31369583/does-it-matter-which-file-extension-i-use-for-my-c-programs . So I even tried something like '.frog' as an extension, which g++ doesn't recognise. Why is that? – Pol Oct 15 '20 at 16:56

2 Answers2

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.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • 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: https://stackoverflow.com/questions/31369583/does-it-matter-which-file-extension-i-use-for-my-c-programs – Pol Oct 15 '20 at 16:53
  • 1
    @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. – eerorika Oct 16 '20 at 10:25
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++
Sumit Jaiswal
  • 216
  • 1
  • 4
  • 17
  • Actually, my current code does have iostream and namespaces inside, so it can't be compiled by gcc at all. – Pol Oct 15 '20 at 16:50