可以在Ubuntu中进行编译,但不能在Mac OS X中进行编译

Can compile in Ubuntu but not in Mac OS X

本文关键字:编译 Mac OS 但不能 Ubuntu      更新时间:2023-10-16

i在Ubuntu 12.04上构建了动态库(.so file)。我们称之为test.so。我有一个test.cpp文件,该文件调用一些库功能。我首先将test.cpp编译到test.o中:

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

成功。然后,我将test.o编译到test.so中:

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

也成功了。

我做了类似的事情,但是在Mac OS X上。

我首先获得了test.o

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

然后

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

这失败了,因为我没有提供test.cpp中使用的库。我修改了它:

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

然后起作用。

请注意,对于第一种情况,我没有为库和test.cpp中使用的特定库提供这样的途径。有人知道为什么我在第一种情况下不需要,而在第二种情况下需要?

带有默认选项的链接器在Linux和OSX上的行为不相同。要获得OSX链接以更像您对Linux的期望,请使用以下链接标志。

-Wl,-undefined,dynamic_lookup