Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/137.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 什么是g++;在MacOSX上构建true.so/MH_BUNDLE共享库的标志(不是动态库)?_C++_Macos_Shared Libraries - Fatal编程技术网

C++ 什么是g++;在MacOSX上构建true.so/MH_BUNDLE共享库的标志(不是动态库)?

C++ 什么是g++;在MacOSX上构建true.so/MH_BUNDLE共享库的标志(不是动态库)?,c++,macos,shared-libraries,C++,Macos,Shared Libraries,我正在尝试在MacOSX上创建一个.so。似乎有一个介于.so和.dylib之间的类型 $ file some_real.so some_real.so: Mach-O 64-bit bundle x86_64 dynamiclib标志按预期生成动态库 $ 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

我正在尝试在MacOSX上创建一个.so。似乎有一个介于.so和.dylib之间的类型

$ file  some_real.so
some_real.so: Mach-O 64-bit bundle x86_64
dynamiclib标志按预期生成动态库

$ 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
共享标志没有给出我想要的

$ 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
本文对此进行了一些讨论,并提到了-fPIC标志。我把它添加到命令行中,它仍然会生成一个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

(原因:我需要此输出为.so/MH_BUNDLE类型,因为我正在尝试创建一些针对.so格式的内容的google测试,而链接器拒绝链接gtest.dylib和我的.so。)

如果要生成捆绑包,请在生成文件时使用
-bundle
而不是
-dynamiclib

bundle和dylibs之间最明显的区别在于,您可以在编译时链接到dylib

e、 g.
g++-o测试文件testcode.c-lmylib
将链接到
libmylib.dylib
,而如果您尝试链接捆绑包,则会得到:

ld: can't link with bundle (MH_BUNDLE) only dylibs (MH_DYLIB) file 'test.bundle' for architecture x86_64
这是最大的区别-您不能动态链接捆绑包,而是必须
dlopen
或使用。我不想使用OSX专用函数——它们已经不推荐使用,您可以从
dl*
函数中获得所需的所有功能

关于每个建筑,我将举一个例子:

对象文件
test.o
,生成动态库:

g++ -dynamiclib -o test.dylib test.o
打包:

g++ -bundle -o test.bundle test.o
在运行时链接捆绑包并获取符号:

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");
使用旧例程链接捆绑包(说真的,不要这样做):

i、 请不要使用这些功能

#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);
warning: 'NSCreateObjectFileImageFromFile' is deprecated: first deprecated in OS X 10.5