Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > extern "C"

Reply
Thread Tools

extern "C"

 
 
Sherm Pendley
Guest
Posts: n/a
 
      09-05-2008
Uwe Schmitt <(E-Mail Removed)> writes:

> On 4 Sep., 21:10, Ian Collins <(E-Mail Removed)> wrote:
>> Uwe Schmitt wrote:
>>
>> > Which compilers do you use and how do you use them ?

>>
>> > If you use the gnu compiliers, you should use

>>
>> > * * *g++ -c cpplib.cpp

>>
>> > and then

>>
>> > * * *gcc cprogram.c cpplib.o

>>
>> > Greetings, Uwe

>>
>> That's very unlikely to work. *The main program must be compiled and
>> linked as C++.

>
> But all names cpplib.o are plain, because we used 'extern "C"' in
> 'function()'s declaration. So the linker will see no C++ mangled
> symbolnames during linking.
> Did I miss something ?


The public symbols exported by cpplib.o and used in cprogram aren't
the problem. The problem is that cpplib.o probably needs to link to
libstdc++. That happens by default when you use g++, but not with
gcc.

sherm--

--
My blog: http://shermspace.blogspot.com
Cocoa programming in Perl: http://camelbones.sourceforge.net
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      09-05-2008
On Sep 5, 7:29 pm, Uwe Schmitt <(E-Mail Removed)> wrote:
> On 4 Sep., 21:10, Ian Collins <(E-Mail Removed)> wrote:
> > Uwe Schmitt wrote:


> > > Which compilers do you use and how do you use them ?
> > > If you use the gnu compiliers, you should use


> > > g++ -c cpplib.cpp


> > > and then


> > > gcc cprogram.c cpplib.o


> > That's very unlikely to work. The main program must be compiled and
> > linked as C++.


> But all names cpplib.o are plain, because we used 'extern "C"' in
> 'function()'s declaration. So the linker will see no C++ mangled
> symbolnames during linking.


So?

> Did I miss something ? Why should I use C++ compiler for
> compiling and linking a C program with some object file which
> looks like a C-object file ?


The C++ standard is quite clear: main() must be compiled as C++.
Depending on the compiler, it might work if this isn't the case,
or it might not, or somethings might work, and others not. In
particular, with some compilers, there's a very good chance that
static variables won't be initialized correctly if main isn't
compiled as C++.

And mangling really has not got much to do with the problem.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      09-05-2008
On Sep 5, 7:35 pm, Sherm Pendley <(E-Mail Removed)> wrote:
> Uwe Schmitt <(E-Mail Removed)> writes:
> > On 4 Sep., 21:10, Ian Collins <(E-Mail Removed)> wrote:
> >> Uwe Schmitt wrote:
> >> > Which compilers do you use and how do you use them ?


> >> > If you use the gnu compiliers, you should use


> >> > g++ -c cpplib.cpp


> >> > and then


> >> > gcc cprogram.c cpplib.o


> >> That's very unlikely to work. The main program must be
> >> compiled and linked as C++.


> > But all names cpplib.o are plain, because we used 'extern
> > "C"' in 'function()'s declaration. So the linker will see no
> > C++ mangled symbolnames during linking. Did I miss
> > something ?


> The public symbols exported by cpplib.o and used in cprogram
> aren't the problem. The problem is that cpplib.o probably
> needs to link to libstdc++. That happens by default when you
> use g++, but not with gcc.


The problem is also that static variables have to be
initialized; some compilers do this by generating special code
in main. Of course, if main wasn't compiled with a C++
compiler, it won't have that special code.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
Jerry Coffin
Guest
Posts: n/a
 
      09-06-2008
In article <48c0dbfe$0$17346$(E-Mail Removed)>,
http://www.velocityreviews.com/forums/(E-Mail Removed) says...

[ ... ]

> Most c++ compiler support C99 unless you disable it in the command line.


Really? All the C++ compilers I have handy seem to reject the following
perfectly legal bit of C99 code:

union {
char birthday[9];
int age;
float weight;
} people = { .age = 14 };

Even Comeau (which will accept it as C99 code) rejects it as C++ code
(quite rightly, I might add, since it's ill formed C++ code, and I don't
believe C++ 0x will allow it either).

Every other compiler I have handy (Microsoft, Digital Mars, gnu) rejects
it as either C or C++. gnu has a '-std=c99' flag, but appears to
implement little enough of C99 that it doesn't accept this even with
that flag.

--
Later,
Jerry.

The universe is a figment of its own imagination.
 
Reply With Quote
 
Uwe Schmitt
Guest
Posts: n/a
 
      09-06-2008
On 5 Sep., 20:25, James Kanze <(E-Mail Removed)> wrote:
> On Sep 5, 7:35 pm, Sherm Pendley <(E-Mail Removed)> wrote:
>
>
>
> > Uwe Schmitt <(E-Mail Removed)> writes:
> > > On 4 Sep., 21:10, Ian Collins <(E-Mail Removed)> wrote:
> > >> Uwe Schmitt wrote:
> > >> > Which compilers do you use and how do you use them ?
> > >> > If you use the gnu compiliers, you should use
> > >> > * * *g++ -c cpplib.cpp
> > >> > and then
> > >> > * * *gcc cprogram.c cpplib.o
> > >> That's very unlikely to work. *The main program must be
> > >> compiled and linked as C++.
> > > But all names cpplib.o are plain, because we used 'extern
> > > "C"' in 'function()'s declaration. So the linker will see no
> > > C++ mangled symbolnames during linking. *Did I miss
> > > something ?

> > The public symbols exported by cpplib.o and used in cprogram
> > aren't the problem. The problem is that cpplib.o probably
> > needs to link to libstdc++. That happens by default when you
> > use g++, but not with gcc.

>
> The problem is also that static variables have to be
> initialized; some compilers do this by generating special code
> in main. *Of course, if main wasn't compiled with a C++
> compiler, it won't have that special code.
>
> --
> James Kanze (GABI Software) * * * * * * email:(E-Mail Removed)
> hing nConseils en informatique orientée objet/
> * * * * * * * * * *Beratung in objektorientierter Datenverarbeitung
> 9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


thanks, I learned something new

Greetings, Uwe
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      09-06-2008
Jerry Coffin <(E-Mail Removed)> writes:

> In article <48c0dbfe$0$17346$(E-Mail Removed)>,
> (E-Mail Removed) says...
>
> [ ... ]
>
>> Most c++ compiler support C99 unless you disable it in the command line.

>
> Really? All the C++ compilers I have handy seem to reject the following
> perfectly legal bit of C99 code:
>
> union {
> char birthday[9];
> int age;
> float weight;
> } people = { .age = 14 };
>
> Even Comeau (which will accept it as C99 code) rejects it as C++ code
> (quite rightly, I might add, since it's ill formed C++ code, and I don't
> believe C++ 0x will allow it either).
>
> Every other compiler I have handy (Microsoft, Digital Mars, gnu) rejects
> it as either C or C++. gnu has a '-std=c99' flag, but appears to
> implement little enough of C99 that it doesn't accept this even with
> that flag.


Recent versions of gcc accept it.

--
Ben.
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      09-06-2008
Ben Bacarisse wrote:
> Jerry Coffin <(E-Mail Removed)> writes:
>
>> In article <48c0dbfe$0$17346$(E-Mail Removed)>,
>> (E-Mail Removed) says...
>>
>> [ ... ]
>>
>>> Most c++ compiler support C99 unless you disable it in the command line.

>> Really? All the C++ compilers I have handy seem to reject the following
>> perfectly legal bit of C99 code:
>>
>> union {
>> char birthday[9];
>> int age;
>> float weight;
>> } people = { .age = 14 };
>>
>> Even Comeau (which will accept it as C99 code) rejects it as C++ code
>> (quite rightly, I might add, since it's ill formed C++ code, and I don't
>> believe C++ 0x will allow it either).
>>
>> Every other compiler I have handy (Microsoft, Digital Mars, gnu) rejects
>> it as either C or C++. gnu has a '-std=c99' flag, but appears to
>> implement little enough of C99 that it doesn't accept this even with
>> that flag.

>
> Recent versions of gcc accept it.
>

gcc also accepts VLAs, but I doubt any other C++ compiler would.

--
Ian Collins.
 
Reply With Quote
 
Jerry Coffin
Guest
Posts: n/a
 
      09-07-2008
In article <(E-Mail Removed)>, (E-Mail Removed) says...

[ ... ]

> Recent versions of gcc accept it.


I'm glad to hear that. What I was using isn't exactly current, but it's
not terribly old either -- it's good to hear that they're apparently
making a real attempt at C99 compliance (finally...)

--
Later,
Jerry.

The universe is a figment of its own imagination.
 
Reply With Quote
 
Michael DOUBEZ
Guest
Posts: n/a
 
      09-08-2008
Jerry Coffin a écrit :
> In article <48c0dbfe$0$17346$(E-Mail Removed)>,
> (E-Mail Removed) says...
>
> [ ... ]
>
>> Most c++ compiler support C99 unless you disable it in the command line.

>
> Really? All the C++ compilers I have handy seem to reject the following
> perfectly legal bit of C99 code:
>
> union {
> char birthday[9];
> int age;
> float weight;
> } people = { .age = 14 };
>


True. I have overly generalized.
I was thinking of VLAs which tends to creep in so often. This code is
however accepted by my IAR c++ compiler.

More compliant c++ compiler will hopefully reject this code. But I guess
one could tweak them into accepting it.

--
Michael
 
Reply With Quote
 
Michael DOUBEZ
Guest
Posts: n/a
 
      09-08-2008
Jerry Coffin a écrit :
> In article <48c0dbfe$0$17346$(E-Mail Removed)>,
> (E-Mail Removed) says...
>
> [ ... ]
>
>> Most c++ compiler support C99 unless you disable it in the command line.

>
> Really? All the C++ compilers I have handy seem to reject the following
> perfectly legal bit of C99 code:
>
> union {
> char birthday[9];
> int age;
> float weight;
> } people = { .age = 14 };
>


True. I have overly generalized.
I was thinking of VLAs which tends to creep in so often. This code is
however accepted by my IAR c++ compiler.

More compliant c++ compiler will hopefully reject this code. But I guess
one could tweak them into accepting it.

--
Michael
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference of extern short *x and extern short x[]? Andre C Programming 5 07-17-2012 07:38 PM
extern const char * vs. extern const char []http://tinyurl.com/47e3k Thomas Matthews C++ 5 08-02-2004 10:36 AM
Re: extern function declaration josef angermeier C++ 0 07-17-2003 08:31 AM
Re: extern function declaration Andrey Tarasevich C++ 0 07-16-2003 06:51 PM
char s[] = "asdf" and extern char *s in separate files Newsgroup - Ann C++ 1 07-05-2003 06:34 AM



Advertisments