我正在使用Swig开发一个PHP 7扩展,并试图链接到libphp7.so。从我的CMakeLists.txt文件:
find_library(php7_lib php7 PATHS "/usr/local/Cellar/php/7.3.0/lib/httpd/modules" NO_DEFAULT_PATH)
target_link_libraries(navdb_php7_client_api ${php7_lib} dl)但我发现了一个错误:
[100%] Linking CXX shared module .../lib/libnavdb_php7_client_api.so
...
ld: can't link with bundle (MH_BUNDLE) only dylibs (MH_DYLIB) file '/usr/local/Cellar/php/7.3.0/lib/httpd/modules/libphp7.so' for architecture x86_64我试图链接到的文件:
$ file /usr/local/Cellar/php/7.3.0/lib/httpd/modules/libphp7.so
/usr/local/Cellar/php/7.3.0/lib/httpd/modules/libphp7.so: Mach-O 64-bit bundle x86_64对如何解决这个问题有什么想法吗?
发布于 2019-01-03 06:42:08
我从这个链接中找到了如何/做什么:构建库时的Clang和未定义符号
所以不需要在编译时链接到libphp7.so,运行时可以正常工作。可以通过设置CXX_FLAG来启用此功能(有关详细信息,请参阅链接)。
发布于 2019-01-02 20:47:01
虽然苹果建议给捆绑包扩展.bundle,但为了跨平台的熟悉,许多开发人员给它们提供了.so扩展。在Linux上,没有区分共享模块(MacOS上的包)和共享库(MacOS上的dylib )。
理解这一点,正如ld所述,您不能链接到MacOS上的MH_BUNDLE。它需要是一个dylib来链接它,或者您需要使用dyld加载.so。
这个链接给出了如何在MacOS上动态加载包的示例:
#include <stdio.h>
#import <mach-o/dyld.h>
int main( )
{
int the_answer;
int rc; // Success or failure result value
NSObjectFileImage img; // Represents the bundle's object file
NSModule handle; // Handle to the loaded bundle
NSSymbol sym; // Represents a symbol in the bundle
int (*get_answer) (void); // Function pointer for get_answer
/* Get an object file for the bundle. */
rc = NSCreateObjectFileImageFromFile("libanswer.bundle", &img);
if (rc != NSObjectFileImageSuccess) {
fprintf(stderr, "Could not load libanswer.bundle.\n");
exit(-1);
}
/* Get a handle for the bundle. */
handle = NSLinkModule(img, "libanswer.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);
}
/* Get the address of the function. */
get_answer = NSAddressOfSymbol(sym);
/* Invoke the function and display the answer. */
the_answer = get_answer( );
printf("The answer is... %d\n", the_answer);
fprintf(stderr, "%d??!!\n", the_answer);
return 0;
}https://stackoverflow.com/questions/54003455
复制相似问题