CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2002
    Location
    La Plata, Buenos Aires
    Posts
    615

    header definitions question (extern "C")



    i´m compiling a c++ program which uses some C libraries (pc.h, bios.h) in GCC 3.2 and the linker fails with "multiple redefinition" error in many of my program functions (seems a conflict between .cpp and .h files)

    what is #ifdef c_plus_plus and extern "c"?

    can be avoided the problem with this definitions?

    thank you.

    also, give some recommendation on general .h and .cpp coding conventions such as using, for example,

    #ifndef __PROG_H__
    #include __PROG_H__
    #endif

  2. #2
    Join Date
    Mar 2002
    Location
    AhuhA
    Posts
    204

    Smile Hi

    I think you should put up some code or you should be more specific about your problem






    Regards,

  3. #3
    Join Date
    Dec 2002
    Location
    La Plata, Buenos Aires
    Posts
    615

    i`ve fixed the problem

    Ive fixed the problem with the linker: I excluded from the DJGPP IDE the linking for ".h" files.

    But im still wanting an explanation of this commonly seen definitions in .h files:

    Code:
    #ifdef _cplusplus
    extern"c" {
    
    ...
    
    #endif...
    thank you in advance.

  4. #4
    Join Date
    Oct 2002
    Location
    Tx, US
    Posts
    208
    I couldn't get it what problem u have & what did u fix ?

    -------------------------------------------------------------
    #ifdef _cplusplus
    extern"c" {

    ...

    #endif...
    -------------------------------------------------
    C++ compiler decorates the function name (changes name) which is called name mangling
    The above code generally use to prevent the name mangling of the C++ compiler. so if ur compiling with C++ compiler it stil going to act as C declartion


    Vinod

  5. #5
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    The reason C++ does "name mangling" is because C++ provides
    function overloading, whereas C does not. Function overloading
    is the process by which you give multiple functions the same
    name, but they would vary by their arguments.

    As an example:
    Code:
    void print(int i);
    void print(char c);
    void print(float f);
    // etc....
    Thus, you could do the following:
    Code:
    int i = 0;
    float f = 0.0;
    char c = 'c';
    
    print(i);
    print(f);
    print(c);
    Each function IS different, so what should the compiler do? It
    "mangles" each function so that it basically creates something to
    the effect of:
    Code:
    print_int
    print_float
    print_char
    Each compiler's method of name mangling will be different.

    --Paul

  6. #6
    Join Date
    Feb 2003
    Location
    Los Angeles, CA
    Posts
    2
    Each compiler's method of name mangling will be different.

    --Paul
    Which is true. Which is the reason C++ libraries compiled from different compilers, or even different version of the same compilers, are NOT compatible with each other. This is also the reason when you are providing third party tools or SDK library, you want to strictly stick to a C interface API, not a C++ interface.

    The C++ standard failed to provide a standard for the name mangling while at the same time C++ requires name mangling to be mandatory, making C++ an incomplete language.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured