public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* RE: c++ code compiling
@ 2001-10-04 18:26 qiaowen.hua
  2001-10-05  0:53 ` Rupert Wood
  0 siblings, 1 reply; 5+ messages in thread
From: qiaowen.hua @ 2001-10-04 18:26 UTC (permalink / raw)
  To: eljay, frank.schafer; +Cc: gcc-help

Dear Eljay and Frank,
    In fact, i don't wanna give out a clear declaration of the function 
be called. I know if i code it with "C", this error is just a 
warning and i can successfully link the programm as long as i provide 
the obj file with this function's implementation when
linking. So, do you know if g++ compiler has some compilation options 
can be used to bypass this "error"?

Best regards,
Hua Qiaowen

-----Original Message-----
From: eljay [SMTP:eljay@adobe.com]
Sent: Thursday, October 04, 2001 8:21 PM
To: Qiaowen Hua
Subject:  Re: c++ code compiling


Hi Hua,

Have you tried giving an explicit declaration (prototype)?

Example:
extern int foo(int i);

Sincerely,
--Eljay

^ permalink raw reply	[flat|nested] 5+ messages in thread

* RE: c++ code compiling
  2001-10-04 18:26 c++ code compiling qiaowen.hua
@ 2001-10-05  0:53 ` Rupert Wood
  2001-10-05  3:20   ` Frank Schafer
  0 siblings, 1 reply; 5+ messages in thread
From: Rupert Wood @ 2001-10-05  0:53 UTC (permalink / raw)
  To: qiaowen.hua; +Cc: gcc-help, eljay, frank.schafer

Hua Qiaowen wrote:

> In fact, i don't wanna give out a clear declaration of the function
> be called.

But that's wrong - for at least two reasons:

1. One of the philosophies of C++ (IIRC) is that as much compile-time
   checking is performed as possible. If you don't give the compiler
   complete type information, it can't perform full compile time
   type checking. Even if the compiler would accept it, it would be
   considered bad style.

2. C++ generates 'mangled' or 'decorated' function names. In the
   object file,

       int f(void) { return 1; }

   will be symbol _Z1fv not _f (on my system at least).

   This is because C++ supports type overloading; it's valid to have
   two functions:

       int   f(int);
       char* f(char*);

   in the same program, and the compiler will sort out which one to
   call. These overloaded functions need to have different symbol
   names. The _Z and the v in the above example represent (I think)
   return type and argument type respectively, and extra decorators
   can be added for class names, namespaces and other modifiers
   (const, etc.) If you don't declare a function, there's no way the
   compiler can know what mangled name to generate a call to.

> I know if i code it with "C", this error is just a warning and i can
> successfully link the programm as long as i provide the obj file
> with this function's implementation when linking. So, do you know if
> g++ compiler has some compilation options can be used to bypass this
> "error"?

There is no flag to turn this off. If you'd like to see for yourself (or
change this) then look in gcc/cp/lex.c: searching for 'first use this
function' will take you to the right place.

If you want to link against an C-style undecorated symbol then you have
to declare a function:

    extern "C" int f(void);

or similar. You can declare a block of these:

    extern "C" {
        int f(void);
        char* g(int);
    };

etc. but you need the "C" to specify non-mangled names.

I have a hunch that you don't want to provide a delcaration because you
don't know what it is - that you have someone else's object or library
that you'd like to link to and play with but you don't know the exact
function arguments and return types. You may be better off reverse
engineering the function you'd like to call rather than repeatedly
trying and guessing, although the above 'extern "C"' form will link
against regular C functions if you really want.

If you just want to omit a declaration to be lazy then tough luck: C++
won't allow this. (Or at least g++ won't - I'm not going to check the
standard.)

Rup.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: c++ code compiling
  2001-10-05  0:53 ` Rupert Wood
@ 2001-10-05  3:20   ` Frank Schafer
  0 siblings, 0 replies; 5+ messages in thread
From: Frank Schafer @ 2001-10-05  3:20 UTC (permalink / raw)
  To: Rupert Wood; +Cc: qiaowen.hua, gcc-help, eljay

Rupert Wood wrote:

> Hua Qiaowen wrote:
>
> > In fact, i don't wanna give out a clear declaration of the function
> > be called.
>
> But that's wrong - for at least two reasons:
>
> 1. One of the philosophies of C++ (IIRC) is that as much compile-time
>    checking is performed as possible. If you don't give the compiler
>    complete type information, it can't perform full compile time
>    type checking. Even if the compiler would accept it, it would be
>    considered bad style.
>
> 2. C++ generates 'mangled' or 'decorated' function names. In the
>    object file,
>
>        int f(void) { return 1; }
>
>    will be symbol _Z1fv not _f (on my system at least).
>
>    This is because C++ supports type overloading; it's valid to have
>    two functions:
>
>        int   f(int);
>        char* f(char*);
>
>    in the same program, and the compiler will sort out which one to
>    call. These overloaded functions need to have different symbol
>    names. The _Z and the v in the above example represent (I think)
>    return type and argument type respectively, and extra decorators
>    can be added for class names, namespaces and other modifiers
>    (const, etc.) If you don't declare a function, there's no way the
>    compiler can know what mangled name to generate a call to.
>
> > I know if i code it with "C", this error is just a warning and i can
> > successfully link the programm as long as i provide the obj file
> > with this function's implementation when linking. So, do you know if
> > g++ compiler has some compilation options can be used to bypass this
> > "error"?
>
> There is no flag to turn this off. If you'd like to see for yourself (or
> change this) then look in gcc/cp/lex.c: searching for 'first use this
> function' will take you to the right place.
>
> If you want to link against an C-style undecorated symbol then you have
> to declare a function:
>
>     extern "C" int f(void);
>
> or similar. You can declare a block of these:
>
>     extern "C" {
>         int f(void);
>         char* g(int);
>     };
>
> etc. but you need the "C" to specify non-mangled names.
>
> I have a hunch that you don't want to provide a delcaration because you
> don't know what it is - that you have someone else's object or library
> that you'd like to link to and play with but you don't know the exact
> function arguments and return types. You may be better off reverse
> engineering the function you'd like to call rather than repeatedly
> trying and guessing, although the above 'extern "C"' form will link
> against regular C functions if you really want.
>
> If you just want to omit a declaration to be lazy then tough luck: C++
> won't allow this. (Or at least g++ won't - I'm not going to check the
> standard.)
>
> Rup.

Hi Rup,

thanks for this excellent explanation of C++. I was too lazy, to write this
all down in MY reply.

You're right, it's WRONG what Qiao wants to do, but it's not only wrong, it
even doesn't make sense. ( I hope Qiao reads this reply too. I've already
deleted his mail. )
Why the hick should I link a function if I don't know its interface?
The interface declaration is done by including the header files for the
library in question. If the headers aren't available, You won't be able to
write a progam based on the lib.

Frank

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: c++ code compiling
  2001-10-04  0:54 qiaowen.hua
@ 2001-10-04  1:12 ` Frank Schafer
  0 siblings, 0 replies; 5+ messages in thread
From: Frank Schafer @ 2001-10-04  1:12 UTC (permalink / raw)
  To: qiaowen.hua; +Cc: gcc-help

qiaowen.hua@astec.alcatel.com.cn wrote:

> Dear all,
>     If in c++ code, i call function which is implicit declared, the
> compiling will stop with error "implicit declaration of function
> `function name`", how to suppress this error? I only desire to pass it
> in linking phase.
>
> Best regards,
> Hua Qiaowen

Hi there,

if I remember right ( from my study of C ), You have to declare the
function as ``extern'' to use it.

Frank

^ permalink raw reply	[flat|nested] 5+ messages in thread

* c++ code compiling
@ 2001-10-04  0:54 qiaowen.hua
  2001-10-04  1:12 ` Frank Schafer
  0 siblings, 1 reply; 5+ messages in thread
From: qiaowen.hua @ 2001-10-04  0:54 UTC (permalink / raw)
  To: gcc-help

Dear all,
    If in c++ code, i call function which is implicit declared, the 
compiling will stop with error "implicit declaration of function 
`function name`", how to suppress this error? I only desire to pass it 
in linking phase.

Best regards,
Hua Qiaowen

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2001-10-05  3:20 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-10-04 18:26 c++ code compiling qiaowen.hua
2001-10-05  0:53 ` Rupert Wood
2001-10-05  3:20   ` Frank Schafer
  -- strict thread matches above, loose matches on Subject: below --
2001-10-04  0:54 qiaowen.hua
2001-10-04  1:12 ` Frank Schafer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).