Sujet : Re: C23 thoughts and opinions
De : Keith.S.Thompson+u (at) *nospam* gmail.com (Keith Thompson)
Groupes : comp.lang.cDate : 27. May 2024, 00:30:42
Autres entêtes
Organisation : None to speak of
Message-ID : <87h6ekywv1.fsf@nosuchdomain.example.com>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
User-Agent : Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
jak <
nospam@please.ty> writes:
[...]
Sorry but no. I wrote that there are compilers who do it and when they
replied, bringing the gcc as an example, I replied that the g++ does
something similar.
>
and no, I have not confused the .c with the .C:
>
$ cat foo.c
#include <iostream>
>
int main()
{
std::cout << "hello" << std::endl;
return 0;
}
$ g++ -Wall -pedantic foo.c -o foo
$ ./foo
hello
$
Perhaps this is the source of the confusion.
The "g++" command unconditionally treats a file whose name ends in ".c"
as C++ source, regardless of the contents of that file. Yes, that's
unexpected and counterintuitive behavior.
As I recall, in the very early days of C++ it was recommended to use a
".c" suffix for C++ source files. I think g++ was designed to work with
that convention. These days almost all C++ source is in files with a
distinct suffix (".cpp", ".cc", ".C", etc.), but g++ still works with
the old convention.
If foo.c contains code that is valid C and invalid C++, "g++ foo.c" will
still attempt to compile it as C++.
A compiler driver *could* theoretically examine the source file content
to determine what language to use, but as far as I know no real-world
compiler drivers do that. In particular, neither gcc nor g++ does so.
If they did, there would be a lot of problematic corner cases, like code
that's valid C and C++ with different semantics.
-- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.comvoid Void(void) { Void(); } /* The recursive call of the void */