我对Mac非常陌生,并试图编译一些链接到libdl.so的代码。
我使用CMake来配置我的项目,在我的CMakeList中有:
IF(UNIX)
FIND_LIBRARY(LIBDL_LIB NAMES libdl.so
PATHS /usr/lib/ usr/lib64/)
TARGET_LINK_LIBRARIES(PLUGINS_FRAMEWORK ${LIBDL_LIB})
ENDIF(UNIX)这在Ubuntu上很好,但是MacOSX10.9上没有libdl.so。
Mac上的libdl.so在哪里?
如果它不在那里,我怎么得到它?
谢谢!
发布于 2013-11-24 00:42:21
在Mac上获取dlopen()等不需要特殊的库,共享对象以.dylib或Mac上的.bundle结尾。
这段代码编译时没有额外的库:
#include <dlfcn.h>
#include <stdio.h>
int main(int argc, char **argv)
{
char *name = "libc.dylib";
if (argc > 1)
name = argv[1];
void *dlh = dlopen(name, 0);
if (dlh == 0)
printf("Failed to dlopen() %s\n", name);
else
printf("Got handle %p from dlopen() for %s\n", dlh, name);
dlclose(dlh);
return 0;
}它运行并产生:
Got handle 0x7fff624e9378 from dlopen() for libc.dylib汇编:
gcc -O3 -g -std=c11 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition -Werror dl.c -o dl我在MacOSX10.9小牛上使用了GCC 4.8.2,但是对于任何版本仍然支持的Mac,答案都是一样的。
请注意,Mac上的ldd等效为otool -L,它生成:
$ otool -L dl
dl:
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1197.1.1)
/usr/gcc/v4.8.2/lib/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)
$我不知道什么是幕后魔术或戏法意味着开放libc.dylib实际上是有效的。然而:
$ ./dl libSystem.B.dylib
Got handle 0x7fff6e398378 from dlopen() for libSystem.B.dylib
$ ./dl
Got handle 0x7fff690d2378 from dlopen() for libc.dylib
$两者的地址是相同的,因此可能有一些映射在幕后进行。
发布于 2019-03-19 09:38:28
有独立于平台的方式来添加链接器标志,因此必须使用dlopen。Cmake提供CMAKE_DL_LIBS
包含dlopen和dlclose的库的名称,通常是大多数UNIX机器上的-ldl。
您可以使用它来代替手工搜索-ldl,并考虑到平台的差异:
target_link_libraries(PLUGINS_FRAMEWORK PUBLIC ${CMAKE_DL_LIBS}) https://stackoverflow.com/questions/20169660
复制相似问题