Shared library compiled by Sun studio can't be used by G++

Discussion in 'C++' started by HelloLinux, Jun 26, 2008.

  1. HelloLinux

    HelloLinux Guest

    The following is how I encouted my problems:

    1) Compile print.cpp into libprint.so by Sun C++ 5.9.The command is:
    CC -G -KPIC -o libprint.so print.cpp
    It succeeds.

    2)Compile debug.cpp into libdebug.so by g++ 3.4.6, debug.cpp calls a
    function defined in libprint.so. The command is:
    g++ -shared -fPIC -o libdebug.so debug.cpp -lprint -L.
    It succeeds too.

    3)Compile main.cpp into a.out. main.cpp use a function defined in
    libdebug.so. The command is:
    g++ -fPIC main.cpp -ldebug -L.
    It failed.The error msg is like below.

    Undefined first referenced
    symbol in file
    print(char const*) ./libdebug.so
    ld: fatal: Symbol referencing errors. No output written to a.out
    collect2: ld returned 1 exit status

    What I can't understand is that why step 2 succeed but then step 3
    failed.
    Do I miss some option when invoking g++ in step 3?

    Any help will be appreciated, Thank in advance!

    The code are here.

    //print.cpp
    #include "stdio.h"
    #include "print.h"
    void print(const char *msg)
    {
    printf("print: %s\n",msg);
    }


    //debug.cpp
    #include <iostream>
    #include "print.h"
    using namespace std;
    void debug_oo(const char *msg)
    {
    cout << "debug_oo: " << msg << endl;
    print("called from debug_oo()");
    }


    //main.cpp
    #include "debug.h"
    int main(char* agrc, char** argv)
    {
    debug_oo("wangding");
    return 0;
    }
     
    HelloLinux, Jun 26, 2008
    #1
    1. Advertising

  2. HelloLinux

    HelloLinux Guest

    And my hardware is sparc, And OS is Solaris 10.
     
    HelloLinux, Jun 26, 2008
    #2
    1. Advertising

  3. HelloLinux

    Ian Collins Guest

    HelloLinux wrote:
    > The following is how I encouted my problems:
    >
    > 1) Compile print.cpp into libprint.so by Sun C++ 5.9.The command is:
    > CC -G -KPIC -o libprint.so print.cpp
    > It succeeds.
    >
    > 2)Compile debug.cpp into libdebug.so by g++ 3.4.6, debug.cpp calls a
    > function defined in libprint.so. The command is:
    > g++ -shared -fPIC -o libdebug.so debug.cpp -lprint -L.
    > It succeeds too.
    >

    You can't mix libraries compiled with one C++ compiler with code
    compiled with another. Well OK you can, provided the functions exported
    by the library have C linkage (declared as extern "C").

    --
    Ian Collins.
     
    Ian Collins, Jun 26, 2008
    #3
  4. HelloLinux

    HelloLinux Guest

    On Jun 26, 3:53 pm, Ian Collins <> wrote:
    > HelloLinux wrote:
    > > The following is how I encouted my problems:

    >
    > > 1) Compile print.cpp into libprint.so by Sun C++ 5.9.The command is:
    > > CC -G -KPIC -o libprint.so print.cpp
    > > It succeeds.

    >
    > > 2)Compile debug.cpp into libdebug.so by g++ 3.4.6, debug.cpp calls a
    > > function defined in libprint.so. The command is:
    > > g++ -shared -fPIC -o libdebug.so debug.cpp -lprint -L.
    > > It succeeds too.

    >
    > You can't mix libraries compiled with one C++ compiler with code
    > compiled with another.  Well OK you can, provided the functions exported
    > by the library have C linkage (declared as extern "C").
    >
    > --
    > Ian Collins.


    Thanks a lots, I will try to find a way out to compile all my libs
    using g++.
     
    HelloLinux, Jun 26, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    2,885
  2. John Dumais
    Replies:
    1
    Views:
    295
    tom_usenet
    Jul 9, 2003
  3. Replies:
    1
    Views:
    1,284
    santosh
    Jul 15, 2008
  4. Casey Hawthorne
    Replies:
    1
    Views:
    721
    Arne Vajhøj
    Mar 18, 2009
  5. Matt Adamson
    Replies:
    2
    Views:
    119
    Steve C. Orr [MCSD, MVP, CSM, ASP Insider]
    Mar 9, 2007
Loading...

Share This Page