xwang
xwang

Reputation: 127

Can compile in Ubuntu but not in Mac OS X

I built a dynamic library (.so file) on Ubuntu 12.04. Let's call it test.so. I had a test.cpp file, which calls some library functions. I first compiled test.cpp into test.o by:

g++ test.cpp -o -c test.o

It succeeded. Then I compiled test.o into test.so by:

g++ -shared test.o -o test.so

Also succeeded.

I did the similar thing but on Mac OS X.

I first got test.o by:

g++ test.cpp -o -c test.o

Then

g++ -dynamiclib test.o -o test.dylib

This failed, because I didn't provide the libraries that are used in test.cpp. I modified it:

g++ -dynamiclib test.o -o test.dylib -L/path/to/libraries -lLibraryName

Then it works.

Notice that for the first case, I didn't provide such a path to the libraries and the specific library used in test.cpp. Does someone know why I don't need to in the first case but need to in the second case?

Upvotes: 2

Views: 1294

Answers (1)

Kenneth E. Bellock
Kenneth E. Bellock

Reputation: 1056

The linker, with default options, does not behave the same on Linux and OSX. To get OSX linking to behave more like what you expect on linux, use the following link flag.

-Wl,-undefined,dynamic_lookup

Upvotes: 1

Related Questions