- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
如果我使用 -static-libstdc++
构建一个 C++ 应用程序,它加载一个共享库(通过 dlopen),该库也是使用 -static-libstdc++
构建的,那么该应用程序dlopen 期间出现段错误。
但是——这只发生在某些设置中:
RTLD_DEEPBIND
)-static-libstdc++
,它会起作用。RTLD_LAZY | RTLD_DEEPBIND
) 传递给 dlopen,它就会工作。所以我怀疑问题与应用程序和 .so 之间的符号混淆/重复有关。RTLD_LAZY | RTLD_DEEPBIND
) 加载 .so,然后关闭它并仅使用 RTLD_LAZY
重新加载,它也有效。extern "C"
{
int ExportedFunction1()
{
std::cout << "\n---\n" << __FUNCTION__ << "\n---\n" << std::endl;
return(0);
}
}
#include <iostream>
#include <dlfcn.h>
int main(int argc, char * argv[])
{
void * ph(NULL);
if(argc == 2 && argv[1][0] == '1')
{
std::cout << "Calling dlopen with flags RTLD_LAZY | RTLD_DEEPBIND..." << std::flush;
ph = dlopen("./libfunctions.so", RTLD_LAZY | RTLD_DEEPBIND);
std::cout << "done. Result: " << ph << std::endl;
if(ph)
dlclose(ph);
}
std::cout << "Calling dlopen with flags RTLD_LAZY..." << std::flush;
ph = dlopen("./libfunctions.so", RTLD_LAZY);
std::cout << "done. Result: " << ph << std::endl;
if(ph)
dlclose(ph);
return 0;
}
$ g++ -m32 -g -fPIC -c functions.cpp -o functions.o
$ g++ -m32 -g -fPIC -shared -Wl,-soname,libfunctions.so -static-libgcc -static-libstdc++ functions.o -o libfunctions.so
$ g++ -m32 -g -fPIC -static-libgcc -static-libstdc++ main.cpp -l dl -o main
$ ./main
Calling dlopen with flags RTLD_LAZY...Segmentation fault (core dumped)
$ gdb -c ./core ./main
GNU gdb (GDB) 7.9.1
Copyright (C) 2015 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-linux".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./main...done.
[New LWP 19846]
warning: Could not load shared library symbols for linux-gate.so.1.
Do you need "set solib-search-path" or "set sysroot"?
Core was generated by `./main'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0 __atomic_add_single (__val=1, __mem=0x0)
at /home/test/dev/3rdParty/gcc/build/i686-pc-linux-gnu/libstdc++-v3/include/ext/atomicity.h:74
74 { *__mem += __val; }
(gdb) bt
#0 __atomic_add_single (__val=1, __mem=0x0)
at /home/test/dev/3rdParty/gcc/build/i686-pc-linux-gnu/libstdc++-v3/include/ext/atomicity.h:74
#1 __atomic_add_dispatch (__val=1, __mem=0x0)
at /home/test/dev/3rdParty/gcc/build/i686-pc-linux-gnu/libstdc++-v3/include/ext/atomicity.h:98
#2 _M_add_reference (this=0x0)
at /home/test/dev/3rdParty/gcc/build/i686-pc-linux-gnu/libstdc++-v3/include/bits/locale_classes.h:510
#3 std::locale::locale (this=0xb74f7ffc <__gnu_internal::buf_cout_sync+28>)
at /home/test/dev/3rdParty/gcc/gcc-4.9.3/libstdc++-v3/src/c++98/locale_init.cc:223
#4 0xb746f559 in basic_streambuf (this=<optimized out>)
at /home/test/dev/3rdParty/gcc/build/i686-pc-linux-gnu/libstdc++-v3/include/streambuf:466
#5 stdio_sync_filebuf (__f=0xb76a2a20 <_IO_2_1_stdout_>, this=<optimized out>)
at /home/test/dev/3rdParty/gcc/build/i686-pc-linux-gnu/libstdc++-v3/include/ext/stdio_sync_filebuf.h:77
#6 std::ios_base::Init::Init (this=0xb74f7a01 <std::__ioinit>)
at /home/test/dev/3rdParty/gcc/gcc-4.9.3/libstdc++-v3/src/c++98/ios_init.cc:85
#7 0xb7469419 in __static_initialization_and_destruction_0 (__initialize_p=1, __priority=65535)
at /home/test/dev/3rdParty/build_toolchain/include/c++/4.9.3/iostream:74
#8 0xb7469456 in _GLOBAL__sub_I_functions.cpp(void) () at functions.cpp:12
#9 0xb772c25e in ?? () from /lib/ld-linux.so.2
#10 0xb772c35a in ?? () from /lib/ld-linux.so.2
#11 0xb7730622 in ?? () from /lib/ld-linux.so.2
#12 0xb772c117 in ?? () from /lib/ld-linux.so.2
#13 0xb772fdf4 in ?? () from /lib/ld-linux.so.2
#14 0xb76edcae in ?? () from /lib/libdl.so.2
#15 0xb772c117 in ?? () from /lib/ld-linux.so.2
#16 0xb76ee3b6 in ?? () from /lib/libdl.so.2
#17 0xb76edd61 in dlopen () from /lib/libdl.so.2
#18 0x0804e102 in main (argc=1, argv=0xbfadc254) at main.cpp:18
令我感到奇怪的是,这似乎只在某些版本的 GCC 中失败,而且仅适用于 32 位。我还没有尝试过 GCC 5。
我感谢您的想法/建议。
最佳答案
If -static-libstdc++ is not used when building for either the shared lib or the app, it works.
一般来说,您应该或者避免使用-static-libstdc++
,或者隐藏它的所有符号来避免这样的问题。
So I suspect the problem is related to symbol confusion/duplication between the app & .so.
正确。特别是,问题在于某些符号是重复的,而另一些则不是。我们不得不 disable STB_GNU_UNIQUE
符号的原因。
if I have the code load the .so first with (RTLD_LAZY | RTLD_DEEPBIND), and then close it and re-load with only RTLD_LAZY, it also works.
那是因为 dlclose
在您使用时实际上并没有卸载库。来自 man dlclose
:
The function dlclose() decrements the reference count on the dynamic
library handle handle. If the reference count drops to zero and no
other loaded libraries use symbols in it, then the dynamic library
is unloaded.
您应该能够通过在 dlclose
之后停止 GDB 中的程序并查看其 /proc/$PID/maps
来验证是否属于这种情况——它是您很可能会发现 libfunctions.so
仍然存在于内存中。
关于c++ - 使用-static-libstdc++ 构建应用程序和共享库时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35672680/
Java 库和 android 库有什么区别,各自有什么优点/缺点? 最佳答案 您可以在 Android 应用程序中包含标准 Java .jar 文件库。它们在 .apk 构建时被翻译成 Dalvik
所以,我现在的代码就像从 Java 层加载库(比如 liba.so),并在内部 liba.so 加载 libb.so。因此,如果我必须将所有库打包到 APK 中并将其安装在没有 root 访问权限的设
我想在我的系统中设置 LEDA 库。 我已经从以下链接下载了 LEDA 库 http://www.algorithmic-solutions.info/free/d5.php Instruct
我想用 autoconf 创建一个共享库。但是,我希望共享库具有“.so”扩展名,而不是以“lib”开头。基本上,我想制作一个加载 dlopen 的插件。 .是否有捷径可寻? 当我尝试使用 autoc
我需要在 Apps 脚本应用程序上修改 PDF。为此,我想使用 JS 库:PDF-LIB 我的代码: eval(UrlFetchApp.fetch("https://unpkg.com/pdf-lib
我正在构建一个使用以下 Boost header 的程序(我使用的是 Microsoft Visual C++ 10), #include #include #include #include
当我通过 cygwin 在 hadoop 上运行此命令时: $bin/hadoop jar hadoop-examples-*.jar grep input output 'dfs[a-z.]+' 我
我已经通过 vcpgk 成功安装了一个 C++ 库,名为:lmdb:x64-windows 我还安装了lmdb通过 Cabal 安装的 Haskell 绑定(bind)包 在尝试测试 lmdb 包时:
我该如何解决这个问题? 我刚刚将 javacv jar 文件复制到我的项目 Lib 文件夹下,但出现了这个错误! 我可以找到这个thread来自谷歌,但不幸的是,由于我国的谷歌限制政策,该页面无法打开
我有一个 Android 库项目 FooLib。 FooLib 引用 Android Context 之类的东西,但不需要任何资源文件(res/ 中的东西)所以我目前将其打包为供我的应用使用的 JAR
我正在开发一个 Android 应用程序(使用 Android Studio),它能够通过手势识别算法了解您正在进行的 Activity 。对于我使用 nickgillian ithub 帐户上可用的
关于从 .NET Framework 项目中引用 .NET Standard 类库的问题有很多类似的问题,其中 netstandard 库中的 NuGet 包依赖项不会流向 netframework
我已经从互联网上下载了 jna-4.2.2.jar,现在想将这个 jar 导入到我的项目中。但是当我试图将这个 jar 导入我的项目时,出现以下错误。 [2016-06-20 09:35:01 - F
我正在尝试通过编译在 Mac 上安装 rsync 3.2.3。但是,我想安装所有功能。为此,它需要一些库,此处 ( https://download.samba.org/pub/rsync/INSTA
进入 Web 开发有点困难。过去 5 年我一直致力于 winforms 工作。所以我正在努力从一种切换到另一种。前段时间,我使用过 JavaScript,但现在还没有大量的 JavaScript 库
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我正在寻找一个用Python编写的与logstash(ruby + java)类似的工具/库。 我的目标是: 从 syslog 中解析所有系统日志 解析应用程序特定日志(apache、django、m
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
我花了几天时间试图寻找用于 JavaPOS 实现的 .jar 库,但我找不到任何可以工作的东西。我找到了很多像这样的文档:http://jpos.1045706.n5.nabble.com/file/
这个问题在这里已经有了答案: Merge multiple .so shared libraries (2 个答案) 关闭 9 年前。 我有我在代码中使用的第三方库的源代码和对象。该库附带有关如何使
我是一名优秀的程序员,十分优秀!