简体   繁体   English

在mac osx(不是dylib)上构建真正的.so / MH_BUNDLE共享库的g ++标志是什么?

[英]What are the g++ flags to build a true .so/MH_BUNDLE shared library on mac osx (not a dylib)?

I'm trying to create a .so on mac osx. 我正在尝试在mac osx上创建一个.so。 There seems to be a distinction between .so and .dylib types. 似乎.so和.dylib类型之间存在区别

$ file  some_real.so
some_real.so: Mach-O 64-bit bundle x86_64

dynamiclib flag produces a dylib as expected dynamiclib标志按预期生成dylib

$ g++ -dynamiclib -o libgtest-1.7.0.dylib  [my .o files]
$ file libgtest-1.7.0.dylib
libgtest-1.7.0.dylib: Mach-O 64-bit dynamically linked shared library x86_64
#### ^^^ as expected

shared flag is not giving what I want 共享标志没有给出我想要的东西

$ g++ -shared -o libgtest-1.7.0.so  [my .o files]
$ file libgtest-1.7.0.so
libgtest-1.7.0.dylib: Mach-O 64-bit dynamically linked shared library x86_64
#### ^^^ not as expected; wanted bundle x86_64

This stackoverflow answer talks about it a bit, and mentions the -fPIC flag. 这个stackoverflow回答谈了一下,并提到了-fPIC标志。 I added that to the commandline and it still produces a dynlib 我将它添加到命令行,它仍然生成一个dynlib

$ g++ -shared -fPIC -o libgtest-1.7.0.so  [my .o files]
$ file libgtest-1.7.0.so
libgtest-1.7.0.dylib: Mach-O 64-bit dynamically linked shared library x86_64
#### ^^^ not as expected; wanted bundle x86_64

(Why: I need this output to be of the .so/MH_BUNDLE type because I'm trying to create some google tests against something that is already in the .so format and the linker is refusing to link the gtest .dylib and my .so. ) (为什么:我需要此输出为.so / MH_BUNDLE类型,因为我正在尝试针对已经采用.so格式的内容创建一些谷歌测试,并且链接器拒绝链接gtest .dylib和我的。所以。)

If you want to build a bundle use -bundle instead of -dynamiclib when you're making the file. 如果你想建立一个捆绑使用-bundle而不是-dynamiclib当你正在做的文件。

The most obvious difference between bundles and dylibs is that you can link to a dylib at compile time. bundle和dylib之间最明显的区别是你可以在编译时链接到dylib。

eg g++ -o testfile testcode.c -lmylib will link to libmylib.dylib , while if you attempt to link a bundle you get: 例如g++ -o testfile testcode.c -lmylib将链接到libmylib.dylib ,而如果你尝试链接一个bundle,你得到:

ld: can't link with bundle (MH_BUNDLE) only dylibs (MH_DYLIB) file 'test.bundle' for architecture x86_64

This is the biggest difference - you can't link a bundle dynamically, but instead have to dlopen or use the Object File Image Functions . 这是最大的区别 - 您无法动态链接包,而是必须dlopen或使用对象文件图像功能 I'd steer away from the OS X only functions - they are deprecated, and you can get all the functionality you need from the dl* functions instead. 我不再使用OS X的功能 - 它们已被弃用,您可以从dl*函数中获得所需的所有功能。

As to building each, I'll give an example: 至于构建每个,我会举一个例子:

object file test.o , making a dylib: 对象文件test.o ,制作一个dylib:

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

making a bundle: 捆绑:

g++ -bundle -o test.bundle test.o

linking a bundle at run-time & getting a symbol: 在运行时链接一个包并获取一个符号:

void *v = dlopen("test.bundle", RTLD_LOCAL);
// declare func_ptr as a pointer to a fn taking void, returning an int
int (*func_ptr)(void);
func_ptr = (int (*)(void))dlsym(v, "symbol");

linking a bundle using old routines (seriously, don't do this): 使用例程链接一个bundle(严重的是,不要这样做):

#include <mach-o/dyld.h>

int rc;
NSObjectFileImage img;
NSModule handle;
NSSymbol sym;

rc = NSCreateObjectFileImageFromFile("test.bundle", &img);
if (rc != NSObjectFileImageSuccess) {
  fprintf(stderr, "Could not load libanswer.bundle.\n");
  exit(-1);
}

/* Get a handle for the bundle. */
handle = NSLinkModule(img, "test.bundle", FALSE);

/* Look up the get_answer function. */
sym = NSLookupSymbolInModule(handle, "_get_answer");
if (sym == NULL)
{
  fprintf(stderr, "Could not find symbol: _get_answer.\n");
  exit(-2);
}

int (*func_ptr)(void);
func_ptr = NSAddressOfSymbol(sym);

if you're compiling with clang, then you'll get a bunch of warnings like: 如果你正在使用clang编译,那么你会得到一堆警告:

warning: 'NSCreateObjectFileImageFromFile' is deprecated: first deprecated in OS X 10.5

ie please don't use these functions. 即请不要使用这些功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM