繁体   English   中英

无法在Mac上使用CMake链接到.so文件

[英]Can't link to .so file on Mac with CMake

我正在使用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

有关如何解决此问题的任何想法?

尽管Apple建议捆绑软件使用.bundle扩展名,但许多开发人员还是出于跨平台熟悉的考虑而将其扩展名为.so 在Linux上,共享模块(在MacOS上为捆绑包)和共享库(在MacOS上为dylib)之间没有区别。

理解为,如ld所述,您不能链接到MacOS上的MH_BUNDLE。 它要么需要是dylib才能链接它,要么需要使用dyld API加载.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;
}

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

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

暂无
暂无

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

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