VOGONS


Ansi C or C++?

Topic actions

First post, by Zirias

User metadata
Rank Newbie
Rank
Newbie

Hi,
I just wanted to do some little modifications and was quite surprised to find only plain old C code in the .cpp files. Is there a particular reason for using a C++ compiler (along with libstdc++ et al)?

edit: I just discovered openglide INDEED compiles fine using gcc (and NOT g++), so there's definitely no c++ code anywhere. This helps me build a i386 version on my amd64 host right now, because my debian environment doesn't provide ia32 libstdc++ import library ATM.

So, why is all the code in .cpp files? Is this for building in microsoft visual studio, that doesn't provide a plain C compiler but only C++? If so, wouldn't setting CXX="gcc" on any GNU environment by default be a good idea?

Reply 1 of 13, by MattRocks

User metadata
Rank Oldbie
Rank
Oldbie

Anecdotal evidence only, but when I worked with C and C++ in large organisations the developers did strange things because they were more constrained by policies and working conditions.

Some employers had offline development environments, banned stack exchange, and decisions were made in closed rooms with small scrum teams operating to aggressive time frames.

It would be common to hear disputes over which compiler was better for debugging, produced more optimised code, etc. The decision would be made in the ceremony under vote if necessary.

Similar things happened in more public projects too. I remember being banned from one forum because my opening post was on translating some PHP into Perl.

I later discovered their lead engineer had left the organisation and launch a rival PHP version of an existing Perl product, the old injured organisation was tense, and I asked the wrong question at the wrong time.

So it’s quite believable that OpenGlide adopted C on g++ to pamper a momentary ego that nobody wants to talk about.

Hope that helps? 😀

Desktop timeline [ MOS 7501 → 68030 → x86(P5/MMX) → x86(K6-2) → x86(K7*) → PPC(G3*) → x86-64(K8) → x86-64(Xeon) → x86-64(i5) → x86-64(i7) ] * lost

Reply 2 of 13, by jmarsh

User metadata
Rank Oldbie
Rank
Oldbie

OP hasn't been active since 2017 and posted this question 7 years before that. I doubt they've been waiting for someone to guess an answer.

Reply 3 of 13, by Errius

User metadata
Rank l33t
Rank
l33t

C is valid C++ so why wouldn't you use a C++ compiler

"This all reminds me when i took the windows vista sticker thingy off my old laptop, and on my washing machine as a joke. A few days later said washing machine stopped working. I still think this cannot be a coincidence."

Reply 4 of 13, by MagefromAntares

User metadata
Rank Newbie
Rank
Newbie
Errius wrote on 2026-05-09, 07:45:

C is valid C++ so why wouldn't you use a C++ compiler

While this is mostly true, yes most C is valid C++, but there are slight differences, for example C lets you convert void pointers to other kind of pointers without explicit conversion:

Valid C but invalid C++ code:

char* somedata = malloc(size);

This will fail on C++ compilers because they don't convert void pointers automatically to other types, and some kind of cast needs to be used to convert the result.

"A process cannot be understood by stopping it. Understanding must move with the flow of the process, must join it and flow with it." - Dune

Reply 5 of 13, by wbahnassi

User metadata
Rank Oldbie
Rank
Oldbie

Pure C code in .cpp extension is not right, and there are no reasons related to Visual Studio that push people to such confusing file naming. The Microsoft compiler can be forced to compile C and error out on C++ regardless of file extension, but IIRC it does that automatically with files of .c extension.. so just name the files properly to first let people know which language to use, and second have the toolchain do the right thing.

Now the choice of whether to allow some C++ in C is something else that is really up to the taste of the project owner. Each have their way...

Turbo XT 12MHz, 8-bit VGA, Dual 360K drives
Intel 386 DX-33, Speedstar 24X, SB 1.5, 1x CD
Intel 486 DX2-66, CL5428 VLB, SBPro 2, 2x CD
Intel Pentium 90, Matrox Millenium 2, SB16, 4x CD
HP Z400, Xeon 3.46GHz, YMF-744, Voodoo3, RTX2080Ti

Reply 6 of 13, by Errius

User metadata
Rank l33t
Rank
l33t

Does this compile in your C++ compilers

wmain( argc, argv )
int argc;
wchar_t **argv;
{
wprintf( L"Hello World\n");
}

"This all reminds me when i took the windows vista sticker thingy off my old laptop, and on my washing machine as a joke. A few days later said washing machine stopped working. I still think this cannot be a coincidence."

Reply 7 of 13, by jakethompson1

User metadata
Rank l33t
Rank
l33t

For a long time Microsoft's philosophy was that C++ replaced C. The C compiler that was activated in Visual Studio when compiling a .c file was frozen in time with C89/C90 features.

The C99 article on Wikipedia has a few instructive references. Here is one from 2012 showing that Microsoft philosophy: https://www.infoq.com/news/2012/05/vs_c99_support/

Since then, they had a change of heart, and the compiler for .C files in Visual Studio has been substantially upgraded in VS 2013 and later.

But because of this legacy, Windows developers often write "C++" that is little more than C with a few C++ features that were later backported to C99. Namely, begin able to declare fresh variables in a for statement, and being able to declare a variable anywhere in a function rather than only at the top of a block.

The UNIX-Haters Handbook has a humorous take on the situation:

Fortunately, most good programmers know that they can avoid C++ by writing largely in C, steering clear of most of the ridiculous fea- tures that they’ll probably never understand anyway. Usually, this means writing their own non-object-oriented tools to get just the features they need. Of course, this means their code will be idiosyncratic, incompatible, and impossible to understand or reuse. But a thin veneer of C++ here and there is just enough to fool managers into approving their projects.

People who actually like C++ will tell you that these criticisms are outdated and don't apply to "modern C++," that is, using a C++11 compliant compiler and purging any third party libraries that are non-modern C++.

Reply 8 of 13, by jakethompson1

User metadata
Rank l33t
Rank
l33t
Errius wrote on 2026-05-09, 07:45:

C is valid C++ so why wouldn't you use a C++ compiler

An even simpler example than the one MagefromAntares gave is that C++ has more reserved words than C, e.g. int class; is valid C but not C++. That might seem silly, but it might come up in C code that parses or otherwise deals with C++ or other object oriented languages. Java's reflection and JNI features run into the same problem, which they handle by deliberately misspelling class as clazz when naming a variable.

Reply 9 of 13, by MagefromAntares

User metadata
Rank Newbie
Rank
Newbie
Errius wrote on 2026-05-09, 20:56:
Does this compile in your C++ compilers […]
Show full quote

Does this compile in your C++ compilers

wmain( argc, argv )
int argc;
wchar_t **argv;
{
wprintf( L"Hello World\n");
}

This doesn't even compile even on K&R compatible C compilers, you are missing the

#include <wchar.h>

at the beginning 😁

Nice Cobra MK III avatar from Elite though 😀

"A process cannot be understood by stopping it. Understanding must move with the flow of the process, must join it and flow with it." - Dune

Reply 10 of 13, by mr.cat

User metadata
Rank Member
Rank
Member

Hmm. I'm confused as to what codebase OP was looking at.

Because the code in OpenGlide (the original OpenGLide_008_src.zip from sourceforge) sure looks like C++ to me.
Granted it's in old style, so quite C-like, but there are things such as "new" and "class" in there.
And ofc the most obvious thing, the C++ style comments (gcc will gladly accept these by default though).

EDIT: Thank you. TIL 😁
vvvvvvvvvvvvvvvvvvvv

Last edited by mr.cat on 2026-05-10, 16:15. Edited 1 time in total.

Reply 11 of 13, by MagefromAntares

User metadata
Rank Newbie
Rank
Newbie
mr.cat wrote on Yesterday, 11:26:
Hmm. I'm confused as to what codebase OP was looking at. […]
Show full quote

Hmm. I'm confused as to what codebase OP was looking at.

Because the code in OpenGlide (the original OpenGLide_008_src.zip from sourceforge) sure looks like C++ to me.
Granted it's in old style, so quite C-like, but there are things such as "new" and "class" in there.
And ofc the most obvious thing, the C++ style comments (gcc will gladly accept these by default though).

I have checked the source at https://github.com/fcbarros/openglide# as the project says https://sourceforge.net/projects/openglide/ that is now the official repository.

C supports C++ style comments since C99, so that is not weird.
But the new and the class keywords are not officially supported by any of the current C standards.
But the build system might have realized that gcc was being run on a C++ file, and added the extra command line parameter

gcc -x c++

this makes the gcc compiler defer to g++ when it encounters C++ code.

"A process cannot be understood by stopping it. Understanding must move with the flow of the process, must join it and flow with it." - Dune

Reply 12 of 13, by SiBurning

User metadata
Rank Newbie
Rank
Newbie

sizeof('c'); // might be different
char *s = "duck"; // illegal in C++
s[0] = 'f'; // undefined behavior in C

uninitialized global variables are shared in C but are illegal in C++. (Why?) This linkage issue is probably the most notorious difference, and lord help you if you link both types of files into a single binary. This is a serious problem in some APIs (::cough:: ODBC ::cough::). When I interview people, I always ask about callbacks in ODBC related to bits, endian, sizeof, and casts, and the stupid clash between typedefs and # definitions in C vs C++, and as I mentioned above, you can easily have different API definitions in the same binary. And, no. Not one person was ever able to explain more than 1/4 of it. (hint: Did I mention AIX? Or how about short vs long pointers?)

Reply 13 of 13, by MattRocks

User metadata
Rank Oldbie
Rank
Oldbie
SiBurning wrote on Today, 01:00:
sizeof('c'); // might be different char *s = "duck"; // illegal in C++ s[0] = 'f'; // undefined behavior in C […]
Show full quote

sizeof('c'); // might be different
char *s = "duck"; // illegal in C++
s[0] = 'f'; // undefined behavior in C

uninitialized global variables are shared in C but are illegal in C++. (Why?) This linkage issue is probably the most notorious difference, and lord help you if you link both types of files into a single binary. This is a serious problem in some APIs (::cough:: ODBC ::cough::). When I interview people, I always ask about callbacks in ODBC related to bits, endian, sizeof, and casts, and the stupid clash between typedefs and # definitions in C vs C++, and as I mentioned above, you can easily have different API definitions in the same binary. And, no. Not one person was ever able to explain more than 1/4 of it. (hint: Did I mention AIX? Or how about short vs long pointers?)

You're giving me nightmares and I haven't slept yet.

Desktop timeline [ MOS 7501 → 68030 → x86(P5/MMX) → x86(K6-2) → x86(K7*) → PPC(G3*) → x86-64(K8) → x86-64(Xeon) → x86-64(i5) → x86-64(i7) ] * lost