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 10, 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 10, 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 10, 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 10, by MagefromAntares

User metadata
Rank Newbie
Rank
Newbie
Errius wrote on Yesterday, 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.

Reply 5 of 10, 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 10, 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 10, 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 10, by jakethompson1

User metadata
Rank l33t
Rank
l33t
Errius wrote on Yesterday, 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 10, by MagefromAntares

User metadata
Rank Newbie
Rank
Newbie
Errius wrote on Yesterday, 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 😀

Reply 10 of 10, 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).