MacOSX Shared Libraries: Undefined Symbols for the x86_64 Architecture

I am having trouble compiling code using shared libraries on MacOSX. I first wrote it on Debian before trying to compile it on MacOSX.

Here is the code:

test.hxx:

#ifndef TEST_HXX
#define TEST_HXX

namespace test
{
    class CFoo
    {
        /* data */
    public:
        CFoo (){}
        virtual ~CFoo (){}
        void bar();

    }; /* CFoo */
} /* namespace test */

#endif /* TEST_HXX */

      

test.cxx:

#include <iostream>

#include "test.hxx"

void test::CFoo::bar()
{
    std::cout << "Hello world!" << std::endl;
} /* bar() */

      

other.hxx:

#ifndef OTHER_HXX
#define OTHER_HXX

namespace other
{
    class CBar
    {
    public:
        CBar (){}
        virtual ~CBar (){}
        void foo();

    }; /* CBar */
} /* namespace other */

#endif /* OTHER_HXX */

      

other.cxx:

#include <iostream>

#include "test.hxx"
#include "other.hxx"

void other::CBar::foo()
{
    test::CFoo c;
    c.bar();
} /* bar() */

      

main.cxx:

#include "other.hxx"

int main (int argc, const char *argv[])
{
    other::CBar c;
    c.foo();
    return 0;

} /* main () */

      

And a simple makefile:

LIBTEST = libtest.so
LIBOTHER = libother.so


all: $(LIBTEST) $(LIBOTHER)
    g++ -ltest -lother -I. -L. main.cxx

libtest.so: test.o
    g++ -shared  test.o -o $(LIBTEST)

libother.so: other.o
    g++ -shared other.o -o $(LIBOTHER)

test.o: test.cxx test.hxx
    g++ -fPIC -c test.cxx

other.o: other.cxx other.hxx
    g++ -fPIC -c other.cxx

clean:
    $(RM) $(LIBOTHER) $(LIBTEST) test.o other.o a.out

      

So I basically create objects test.o

and other.o

and create two shared libraries from them (one per object).

other.cxx

uses the class contained in test.cxx

to print Hello world

.

So this makefile and code works fine on my Debian, but I have a compilation error when trying to compile it on MacOSX:

g++ -fPIC -c test.cxx
g++ -shared test.o -o libtest.so
g++ -fPIC -c  other.cxx
g++ -shared other.o -o libother.so
Undefined symbols for architecture x86_64:
  "test::CFoo::bar()", referenced from:
      other::CBar::foo() in other.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [libother.so] Error 1

      

It compiles and builds my shared library for test

, but doesn't work when built libother.so

.

When using only one shared library (so printing Hello world

in main

directly from test

) it works fine, but the problem occurs when using multiple shared libraries ...

I'm not an Apple user and never work on MacOSX, so I don't understand how the binding is done. And the error doesn't really make sense to me ...

Thanks for helping me understand this error!

+3


source to share


1 answer


This is because it libother

uses libtest

, but you are not communicating with it. Try

g++ -shared other.o -o libother.so -L. -ltest

      



-L

tells the compiler where to look for the library, -L

links with it.

+5


source







All Articles