我一直在尝试用 C++ 开发一个可以在应用程序中运行时加载的动态库。我终于成功了,但它有点难看。我有一个函数,它将指向 C++ 类的指针作为参数,如下所示:
bool registerGrindPlugin( Grind::PluginManager* mgr );
Run Code Online (Sandbox Code Playgroud)
但当然它被导出为:
_Z19registerGrindPluginPN5Grind13PluginManagerE
Run Code Online (Sandbox Code Playgroud)
我尝试了一个带有简单函数的 .c 文件,它可以很好地导出为“registerGrindPlugin”,但当然我不能以这种方式传递 C++ 类作为参数。
所以...我的问题是,有没有办法对导出的符号进行解开或别名,这样我就不必在 dlsym 调用中使用像 Z19registerGrindPluginPN5Grind13PluginManagerE 这样的怪物?
我确实看到了一些关于 -alias_list 作为链接器选项的内容,但我还没有完全弄清楚如何在 Xcode 中使用它。如果这是解决方案,有人可以提供有关如何使用它的更多详细信息吗?
将可执行文件与 dylib 链接时,是install_name_tool使 dylib 路径相对于可执行文件的唯一方法,还是在 clang 的链接步骤中可以这样做?
鉴于以下项目结构:
- Project Root
| compile.sh
- lib_src
| myprint.cpp
| myprint.h
- main_src
| main.cpp
Run Code Online (Sandbox Code Playgroud)
使用这些文件:https://gist.github.com/JohannesMP/8fa140b60b8ffeb2cae0
运行compile.sh(为了简单起见,在这里使用而不是 make 文件)会产生以下文件:
- Project Root
| main (a unix executable linked to myprint.dylib)
| myprint.dylib (a dynamic library that main uses)
Run Code Online (Sandbox Code Playgroud)
./main在项目中使用while 'd运行程序cd工作正常,但尝试从其他任何地方运行它(例如通过简单地双击它)将导致以下错误:
dyld: Library not loaded: myprint.dylib
Referenced from: /Users/Jo/Sandbox/libtest/main
Reason: image not found
Trace/BPT trap: 5
Run Code Online (Sandbox Code Playgroud)
当使用检查 main 时,otool我可以看到这是因为 myprint.dylib 的路径没有根据可执行文件定义: …
通过堆栈跟踪,unsafeMutableAddressor崩溃似乎发生在AppConstant.kShowOverlay从控制器访问时viewDidLoad()
我的定义AppConstant.swift如下:
struct AppConstant {\n // MARK: Properties\n static let kShowOverlay = false\n}\nRun Code Online (Sandbox Code Playgroud)\n\n以下是 crashlytics 的堆栈跟踪:
\n\nCrashed: com.apple.main-thread\n EXC_BAD_ACCESS KERN_INVALID_ADDRESS 0x0000000000000048\n\n 0\xc2\xa0 libswiftCore.dylib\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa00x51bfc5c _TTWuRq_Ss9Indexable_GVSs5Sliceq__Ss14CollectionTypeSsFS1_10suffixFromuRq_S1__fq_Fqq_S_5Indexqq_S1_11SubSequence\n 1\xc2\xa0 MyAppModule\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa00x56fbad4 AppConstants.kShowOverlay.unsafeMutableAddressor (AppConstants.swift)\n 2\xc2\xa0 libdispatch.dylib\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa00x34ba9f13 dispatch_once_f + 62\n 3\xc2\xa0 MyApp\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa00x23e38e8 globalinit_33_D6508E60499748694DF7352631AEB836_func18 (MyViewController.swift:73)\n 4\xc2\xa0 libdispatch.dylib\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa00x34ba92cf _dispatch_client_callout + 22\n 5\xc2\xa0 libdispatch.dylib\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa00x34ba9f13 dispatch_once_f + 62\n 6\xc2\xa0 MyApp\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0 \xc2\xa0\xc2\xa0\xc2\xa00x2383e88 MyViewController.setupCollectionView(MyViewController) -> () -> () (MyViewController.swift:530)\n 7\xc2\xa0 MyApp\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa00x29f0a3c MyViewController.viewDidLoad(MyViewController) -> () -> () (MyViewController.swift:59)\nRun Code Online (Sandbox Code Playgroud)\n\n我无法重现此崩溃,只能从 crashlytics 获取堆栈跟踪。
\n\n …我正在从启用位码的 iOS 源代码构建一个动态框架(使用cmake和xcodebuild)。我使用lipo和install_name_tool制作一个胖二进制文件并 update LC_ID_DYLIB,以便正确加载二进制文件。当我归档应用程序时,框架已正确签名并与应用程序一起打包。这是以下的输出file:
MyFramework: Mach-O universal binary with 3 architectures: [arm_v7: Mach-O dynamically linked shared library arm_v7] [arm_v7s] [arm64]
MyFramework (for architecture armv7): Mach-O dynamically linked shared library arm_v7
MyFramework (for architecture armv7s): Mach-O dynamically linked shared library arm_v7s
MyFramework (for architecture arm64): Mach-O 64-bit dynamically linked shared library arm64
Run Code Online (Sandbox Code Playgroud)
查看otool -l输出LC_ID_DYLIB显示:
Load command 4
cmd LC_ID_DYLIB
cmdsize 64
name @rpath/MyFramework.framework/MyFramework (offset 24) …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Clion 和 Cmake 构建 OSX 捆绑包,目前库链接正确,因为我没有预编译错误,但是当我构建并尝试运行程序时,出现以下错误
dyld:未加载库:libxl.dylib
参考自:
/路径/到/可执行文件/
原因:找不到图片
我搜索过这个问题,似乎找不到明确的解决方案,这可能是由于我缺乏 CMake 和 C++ 的经验。
如果有人有解决方案或可以指出我应该阅读的内容以进一步理解这一点,我将不胜感激。
请参阅下面的 CMakeLists.txt
cmake_minimum_required(版本3.7)
项目(项目名称)
设置(CMAKE_CXX_STANDARD 14)
设置(SOURCE_FILES main.cpp)
find_package(需要Qt5Core)
find_package(需要 Qt5Widgets )
find_package(需要 Qt5Gui )
设置(PROJECT_LINK_LIBS libxl.dylib)
link_directories(${CMAKE_SOURCE_DIR}/LibXL/lib)
include_directories(${CMAKE_SOURCE_DIR}/LibXL/include_cpp)
#Mac 捆绑包(在 Mac 上构建)
add_executable(project_target_mac MACOSX_BUNDLE main.cpp)
qt5_use_modules(project_target_mac核心小部件GUI)
target_link_libraries(project_target_mac Qt5::Widgets)
target_link_libraries(project_target_mac ${PROJECT_LINK_LIBS} )
#set_target_properties(project_target_mac PROPERTIES INSTALL_RPATH "${CMAKE_SOURCE_DIR}/LibXL/lib")
add_executable(project_target ${SOURCE_FILES})
qt5_use_modules(project_target核心小部件GUI)
target_link_libraries(project_target Qt5::Widgets)
target_link_libraries(project_target ${PROJECT_LINK_LIBS
} )
我刚刚在 OSX 上安装了新的更新,因为它不断弹出。当然,它破坏了一些东西。我无法再访问我的 R。
我花了很长时间才设置好我的环境。我已将所有 R 软件包与我在 conda 中使用的 R 一起安装。
我怎样才能解决这个问题?为什么当我进行任何更新或轻微调整时 dyld 库总是会崩溃?确实打乱了工作流程……
Joshs-MBP:~ mu$ which R
/Users/mu/anaconda/bin/R
Joshs-MBP:~ mu$ R
dyld: Library not loaded: @rpath/libreadline.6.2.dylib
Referenced from: /Users/mu/anaconda/lib/R/lib/libR.dylib
Reason: image not found
Abort trap: 6
```
Run Code Online (Sandbox Code Playgroud)
我正在运行 OSX Sierra 10.12.6,当它开始更新 Xcode 时,它立即崩溃了(我以为它只是更新我的 iTunes)。
Joshs-MBP:~ mu$ conda info
Current conda install:
platform : osx-64
conda version : 4.3.29
conda is private : False
conda-env version : 4.3.29
conda-build version : not installed
python version : 3.6.3.final.0
requests version …Run Code Online (Sandbox Code Playgroud) python setup.py bdist_wheel在使用或等效命令构建 Cython 模块后,我需要运行某些命令。这可能吗?
具体情况如下:\n我正在为 osx 开发 Cython 程序。它链接到.dylib我的主目录中的某些文件,并且要构建程序,您必须运行
export DYLD_LIBRARY_PATH=/path/to/parent/dir\nRun Code Online (Sandbox Code Playgroud)\n\n(这很麻烦)或包含一个指令来setup.py运行
install_name_tool -change @executable_path/my.dylib /path/to/my.dylib\nRun Code Online (Sandbox Code Playgroud)\n\n这基本上的作用是告诉二进制文件在哪里寻找dylib. 这两种选择都不是理想的,但后者似乎更便携。现在我刚刚将这些说明添加到 的底部setup.py,但这仍然不可移植:假设我按如下方式打包并上传我的包:
python setup.py sdist # I think this collects source files into a .tar\npython setup.py bdist_wheel # I think this builds a binary. This is where I assume the post-compilation stuff should happen.\ntwine upload dist/* # This puts stuff up on PyPi.\nRun Code Online (Sandbox Code Playgroud)\n\n现在,我已经阅读了文档和一些教程,但我承认我仍然不能 100% 确定这些命令的作用。但我知道如果随后在另一个项目中我运行 …
我正在开发一个软件包,需要在所有用户帐户都可以访问的某个地方安装一个 dylib。我实际上不确定把它放在哪里最好。
通常我认为/usr/local/lib是正确的,但偶尔我们会遇到用户对该文件夹做奇怪的事情,包括更改其权限,以便只有一个用户可以访问其中的文件,从而破坏了该系统上其他用户帐户的软件。从技术上讲,macOS 不允许这种事情——/usr/local旨在成为用户可以随意使用的地方。
另一种解决方案可能是/Library/Frameworks或/Library/Application Support/<my app name>。这些文件夹肯定会更安全,因为用户不应该随意使用它们,也不应该修改他们的权限。然而,这两个地方都没有让我觉得动态库是正确的。/Library/Frameworks最接近,我想我遇到了其他将 dylib 放在那里的应用程序,但它显然是用于框架的。
那么放置它的正确位置在哪里?
我尝试在cgo中导入dylib动态库,但失败。这是我的代码。
\n\npackage main\n//#cgo CFLAGS: -I./yun2txt/include\n//#cgo LDFLAGS: -L./yun2txt/lib -ltotxt\n//\n//#include <library.h>\nimport "C"\n\nfunc main() {\n C.hello()\n}\n\nRun Code Online (Sandbox Code Playgroud)\n\n也有错误。
\n\ndyld: Library not loaded: @rpath/libtotxt.dylib\n Referenced from: /private/var/folders/2n/k70s3c1j08q2z_6kw06n13nh0000gn/T/___go_build_cgo\n Reason: image not found\n\nRun Code Online (Sandbox Code Playgroud)\n\n我该如何解决这个问题\xef\xbc\x9f
\n我正在将 C/C++ 应用程序库(由共享库组成)从 Linux 移植到 Mac OS,当尝试在 Mac 构建中查看生成的 dylib 中的动态符号表时,我得到以下信息:
$ nm -g -D -C --defined-only libMyLib.dylib
/Library/Developer/CommandLineTools/usr/bin/nm: error: libMyLib.dylib: File format has no dynamic symbol table.
Run Code Online (Sandbox Code Playgroud)
我在某些系统库上得到了相同的结果。那么Mac dylibs没有动态符号表是正常的吗?或者我在链接过程中可能做错了什么?