首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法用.so链接到Mac上的CMake文件

无法用.so链接到Mac上的CMake文件
EN

Stack Overflow用户
提问于 2019-01-02 08:48:45
回答 2查看 1.6K关注 0票数 0

我正在使用Swig开发一个PHP 7扩展,并试图链接到libphp7.so。从我的CMakeLists.txt文件:

代码语言:javascript
复制
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)

但我发现了一个错误:

代码语言:javascript
复制
[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

我试图链接到的文件:

代码语言:javascript
复制
$ 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

对如何解决这个问题有什么想法吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-01-03 06:42:08

我从这个链接中找到了如何/做什么:构建库时的Clang和未定义符号

所以不需要在编译时链接到libphp7.so,运行时可以正常工作。可以通过设置CXX_FLAG来启用此功能(有关详细信息,请参阅链接)。

票数 0
EN

Stack Overflow用户

发布于 2019-01-02 20:47:01

虽然苹果建议给捆绑包扩展.bundle,但为了跨平台的熟悉,许多开发人员给它们提供了.so扩展。在Linux上,没有区分共享模块(MacOS上的包)和共享库(MacOS上的dylib )。

理解这一点,正如ld所述,您不能链接到MacOS上的MH_BUNDLE。它需要是一个dylib来链接它,或者您需要使用dyld加载.so

这个链接给出了如何在MacOS上动态加载包的示例:

代码语言:javascript
复制
#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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54003455

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档