user
user

Reputation: 100

MacOSX shared libraries: Undefined symbols for architecture x86_64

I am having some trouble to compile a 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 the 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 creates my shared library for test but fails when creating libother.so.

When using only one shared library (so printing Hello world in main directly from test) it works just fine but the problem occurs when using several shared libraries...

I am not an Apple user and never works on MacOSX before so I don't really understand how linking is being done. And the error doesn't really makes sense to me...

Thank you for helping me to understand this error!

Upvotes: 3

Views: 2147

Answers (1)

crazypeter
crazypeter

Reputation: 1449

This is because libother uses libtest but you don't link with it. Try

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

The -L tells the compiler where to search for the library, -l links with it.

Upvotes: 6

Related Questions