Linking C++ object code with C linker

Linking C++ object code with C linker

Post by Roger Mitchel » Wed, 19 Feb 1997 04:00:00



Curious if anyone can help me.  I have written a C++ program with
multiple source files which uses functions from a C library.  This
library was not written by me but I have added two function calls to my
C++ code from within the C library code.  I then compile and create the
library using 'cc' and the 'ar' command.  To compile the C++ program
I use 'g++' to create object code and then link to the library using
'cc'.  Originally I overcame the C to C++ linking problem, by using the
name created by g++ within the object code.  For example, the
'recover_process' function became 'recover_process__Fv'.  This was put
in the C library source and everything linked fine.

Now, however, I am running into more linking problems.

What is the appropriate way to make calls to C++ functions from within a
C program?  Thanks for the help.

Roger Mitchell

 
 
 

Linking C++ object code with C linker

Post by Jonathan Mag » Wed, 19 Feb 1997 04:00:00




Quote:>Curious if anyone can help me.  I have written a C++ program with
>multiple source files which uses functions from a C library.  This
>library was not written by me but I have added two function calls to my
>C++ code from within the C library code.  I then compile and create the
>library using 'cc' and the 'ar' command.  To compile the C++ program
>I use 'g++' to create object code and then link to the library using
>'cc'.  Originally I overcame the C to C++ linking problem, by using the
>name created by g++ within the object code.  For example, the
>'recover_process' function became 'recover_process__Fv'.  This was put
>in the C library source and everything linked fine.

This is because the C++ compiler mangles the names of symbols to
support overloading and other wonderful C++ features. You  can turn
this feature off though, for any particular declaration by preceding
it with:

extern "C"

So for example:
 extern "C" int recover_process(void);

cheers,
jem.

--

Co-author of UNIX Web Server Book (2nd ed), available January, '96 from Ventana
<URL:http://www.vmedia.com/cat/press/store/wsb>

 
 
 

Linking C++ object code with C linker

Post by Olav Woelfelschneid » Thu, 20 Feb 1997 04:00:00


RM> What is the appropriate way to make calls to C++ functions from within a
RM> C program?  Thanks for the help.

You will have to put c stubs in your c++ files. Like:

static void MyClass:foobar(void);

void MyClass:foobar(void) { /* much magic */ }

void FoobarWrapper(void) {
  MyClass::foobar();

Quote:}

Then call the FoobarWrapper() from outer space.

Hmm... there also was something with this `extern "C" {' keyword...

--

                       PGP fingerprint = 065F66B32AAD7D2DB719673C95A79DAF
     But let your communication be Yea, yea; nay, nay:    -- Matthew 5:37
     for whatsoever is more than these cometh of evil.       on computers

 
 
 

Linking C++ object code with C linker

Post by Greg Come » Sat, 22 Feb 1997 04:00:00



Quote:>Curious if anyone can help me.  I have written a C++ program with
>multiple source files which uses functions from a C library.  This
>library was not written by me but I have added two function calls to my
>C++ code from within the C library code.  I then compile and create the
>library using 'cc' and the 'ar' command.  To compile the C++ program
>I use 'g++' to create object code and then link to the library using
>'cc'.  Originally I overcame the C to C++ linking problem, by using the
>name created by g++ within the object code.  For example, the
>'recover_process' function became 'recover_process__Fv'.  This was put
>in the C library source and everything linked fine.

>Now, however, I am running into more linking problems.

>What is the appropriate way to make calls to C++ functions from within a
>C program?  Thanks for the help.

In general, consider a `linkage specification'.  In this case,
you would spell the specification: extern "C".  This gives you a
fighting chance at your C++ code linking with your compatible C code.
You would specify this on the function prototype:

    extern "C" T recover_process();

You can also do block linkage specifications:

    extern "C"  {
        T recover_process();
    }

Note that I am assuming you are talking about non-member functions here.
If you are actually trying to link and call member functions, things
will be harder and even more non-portable.

- Greg
--
       Comeau Computing, 91-34 120th Street, Richmond Hill, NY, 11418-3214
               Producers of Comeau C++ 4.0 front-end pre-release
****WEB: http://www.comeaucomputing.com / Voice:718-945-0009 / Fax:718-441-2310

 
 
 

1. linking C++ code to a shared object on AIX

Hi all,
        can anyone tell me if there is a way to link a shared object such that you

        call the linker for each object file requires as opposed to one call to
the linker
        and passing the link a complete list of object files.
        For example, in psuedo code I would like to do the following:

        for earch (list of object files to link)
                call linker with an object file

        as opposed to what is typcially done which is

        ld (list of object files).

        If there is a way to do this could you please provide an example.

        Many thanks.  Regards, John Andrusek

2. man configuration

3. is getaddrinfo memory leak?

4. Linking C and C++ code object files with 'ld'?

5. SC for SCO problem

6. ld - Linker : how to link object with -misalign flag ?

7. No menu with minicom ?

8. Linking C code with C++ code

9. dlsym with C++ object code

10. Problem linking a C++ shared object and C executable

11. Bug: Dynamic Linking and global C++ objects

12. Dynamic linking of C++ shared objects