This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

RE: c++ code compiling


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.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]