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: how to comilate cpp with object file


Benoît Allombert wrote:

> I dont understand why i could copilate with c and not with c++ file,
> when do:
:
> someone could explaie why it din t work!!!

Your object file exports myfunc in C-style. You need to use:

    extern "c" void myfunc(char*);

or similar in your C++ source, or:

    extern "C"
    {
        void myfunc(char*);
    }

which is more useful for more than one function, or:

    #ifdef __cplusplus
    extern "C"
    {
    #endif

    void myfunc(char*);

    #ifdef __cplusplus
    }
    #endif

if you want source which compiles as both C and C++.

The reason is that in C++ it is valid to have both:

    void myfunc(char*);
    void myfunc(int);

and the compiler decides which one to call based on context. These two
functions, however, must be exported with unique names. Hence the
compiler generates 'decorated' or 'mangled' names which include return
type and parameter type, etc., e.g.:

    void myfunc(char*);  =>  _Z6myfuncPc
    void myfunc(int);    =>  _Z6myfunci

and it assumes, unless declared extern "c", that it is importing
myfunc() with such a mangled name.

Hope that helps,
Rup.


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