在 C++ 中加载共享库会导致分段错误
Load shared lib in c++ causes segmentation fault
我正在学习 c++,并正在尝试在 linux (.so) 上加载共享库。
当我运行下面的代码时,我遇到了分段错误。
当我尝试使用 valgrind 运行控制台应用程序时,我得到以下结果:
valgrind ./TestLoadSo --leak-check=full -v
==26828== Memcheck, a memory error detector
==26828== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==26828== Using Valgrind-3.12.0 and LibVEX; rerun with -h for copyright info
==26828== Command: ./TestLoadSo --leak-check=full -v
==26828==
!!!Hello World!!!
==26828== Jump to the invalid address stated on the next line
==26828== at 0x0: ???
==26828== by 0x53E63F0: (below main) (libc-start.c:291)
==26828== Address 0x0 is not stack'd, malloc'd or (recently) free'd
==26828==
==26828==
==26828== Process terminating with default action of signal 11 (SIGSEGV)
==26828== Bad permissions for mapped region at address 0x0
==26828== at 0x0: ???
==26828== by 0x53E63F0: (below main) (libc-start.c:291)
==26828==
==26828== HEAP SUMMARY:
==26828== in use at exit: 3,126 bytes in 9 blocks
==26828== total heap usage: 13 allocs, 4 frees, 76,998 bytes allocated
==26828==
==26828== LEAK SUMMARY:
==26828== definitely lost: 0 bytes in 0 blocks
==26828== indirectly lost: 0 bytes in 0 blocks
==26828== possibly lost: 0 bytes in 0 blocks
==26828== still reachable: 3,126 bytes in 9 blocks
==26828== suppressed: 0 bytes in 0 blocks
==26828== Rerun with --leak-check=full to see details of leaked memory
==26828==
==26828== For counts of detected and suppressed errors, rerun with: -v
==26828== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
[1] 26828 segmentation fault (core dumped) valgrind ./TestLoadSo --leak-check=full -v
C++ 主类
extern "C" typedef char* (*helloWorld_t)();
int main() {
void* handle = dlopen("./libMyLib.dll.so", RTLD_LAZY);
if (!handle) {
cerr << "Cannot open library: " << dlerror() << 'n';
return 1;
}
helloWorld_t hello = (helloWorld_t)dlsym( handle, "helloWorld" );
const char * tmp = hello();
printf("n%s",tmp);
return 0;
}
外部函数为:
extern "C++" char* helloWorld() {
char str[25];
strcpy(str, "HelloWorld");
}
如果我使用extern "C"
则会出现编译错误:
error: conflicting declaration of ‘char* helloWorld()’ with ‘C’ linkage
extern "C" char* helloWorld() {
我真的不清楚我哪里出错了。
函数不能同时具有 C 和C++链接,并且函数指针类型必须与其目标函数的链接匹配。
您不能通过其朴素的名称dlsym
extern "C++"
函数。您必须在这两种情况下都使用extern "C"
(推荐),或者始终使用extern "C++"
并将dlsym(handle, "helloWorld")
中的字符串替换为函数的损坏名称(不推荐)。
始终检查dlsym
的结果,如果它返回空指针,则报告错误(使用dlerror()
就像您为dlopen
所做的那样)。
不要使用字符数组或指针来表示字符串。字符串有一种类型,称为std::string
。
最后但并非最不重要的一点是,始终使用-Wall -Werror
进行编译,以便捕获诸如实际上不返回值的非void
函数之类的内容。
这里有很多问题:
extern "C++" char* helloWorld() {
char str[25];
strcpy(str, "HelloWorld");
}
它应该使用"C"
联动。它应该返回一些东西。它将字符串复制到局部变量,因此值在返回时会丢失。所以可能
extern "C" char* helloWorld() {
static char str[25]; // will keep its value accross calls, not thread safe
return strcpy(str, "HelloWorld"); // return pointer to start of str
}
请注意,多个调用都返回相同的静态缓冲区。如果需要副本,则需要让调用方提供缓冲区,或返回分配有malloc
的缓冲区。
相关文章:
- 在某些循环内使用vector.push_back时出现分段错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- 当我的阵列太大时出现分段错误
- 分段错误当我试图运行程序时出错
- 在c++中初始化矩阵时出现分段错误(核心转储)
- 尝试使用集合函数时出现分段错误
- 我无法缩小此分段错误的原因
- g++的分段错误(在NaN上使用to_string两次时)
- 我是如何在这段代码中出现分段错误的
- 创建结构的数组时遇到分段错误
- 在c++中键入向量中的所有值后,得到分段错误(核心转储)
- 在 c++ 中实现 Trie 时出现分段错误
- 为什么 fstream 在打开带有格式的文件时会导致分段错误?
- 为什么我遇到分段错误?
- 动态类的分段错误(家庭作业问题)
- 分段错误 - 读取初始化指针的数组
- 如何摆脱C ++中的分段错误错误?
- 使用 CTYPE 时出现分段错误
- 为什么代码给出分段错误?