From mboxrd@z Thu Jan 1 00:00:00 1970 From: Frank Schafer To: Rupert Wood Cc: qiaowen.hua@astec.alcatel.com.cn, gcc-help@gcc.gnu.org, eljay@adobe.com Subject: Re: c++ code compiling Date: Fri, 05 Oct 2001 03:20:00 -0000 Message-id: <3BBD8CD6.88406297@setuza.cz> References: <616BE6A276E3714788D2AC35C40CD18D03A921@whale.softwire.co.uk> X-SW-Source: 2001-10/msg00046.html 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