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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • HelloLinux

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

    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("wangd ing");
    return 0;
    }
  • HelloLinux

    #2
    Re: Shared library compiled by Sun studio can't be used by G++

    And my hardware is sparc, And OS is Solaris 10.

    Comment

    • Ian Collins

      #3
      Re: Shared library compiled by Sun studio can't be used by G++

      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.

      Comment

      • HelloLinux

        #4
        Re: Shared library compiled by Sun studio can't be used by G++

        On Jun 26, 3:53 pm, Ian Collins <ian-n...@hotmail.co mwrote:
        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++.

        Comment

        Working...