V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX  ›  v2qwsdcv  ›  全部回复第 2 页 / 共 2 页
回复总数  26
1  2  
2018-12-19 18:03:36 +08:00
回复了 sky2017 创建的主题 C 关于 C++ std::thread 的疑问
动态链接库不是 C++标准,是不同操作系统的实现。
我测试了一下,在 Linux 下不能导出自定义的类型为全局变量。没有你说的阻塞的情况,应该就是没有生成这个变量导致。

楼上说的对, 反对使用全局变量。

```

#include <thread>
#include <cstdio>
extern "C"
{
class TestClass
{
public:
TestClass()
{
printf("construct TestClass\n");
m_thread = std::thread([] {});
}
~TestClass()
{
printf("destruct TestClass\n");
if (m_thread.joinable())
{
m_thread.join();
}
}

protected:
private:
std::thread m_thread;
};

extern TestClass tc;

extern int go =1002;
extern struct my m;

struct my{
int a;
int b;
};
}

//g++ --std=c++11 -fPIC -shared dynamic.cpp -o libdy.so
```

从符号表上看只有 int go 被导出了
```
nm -D libdy.so
0000000000201024 B __bss_start
w __cxa_finalize
0000000000201024 D _edata
0000000000201028 B _end
00000000000005c0 T _fini
w __gmon_start__
0000000000201020 D go
0000000000000480 T _init
w _ITM_deregisterTMCloneTable
w _ITM_registerTMCloneTable
w _Jv_RegisterClasses

```

调用的代码

```
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>

int main()
{
void *handle = dlopen("./libdy.so", RTLD_NOW);
if (!handle)
{
printf("%s\n", dlerror());
exit(-1);
}
{
void *ptr = nullptr;
ptr = dlsym(handle, "tc");
if (ptr == nullptr)
{
printf("tc is null\n");
printf("%s\n", dlerror());
}
else
{
printf("find tc\n");
}
}

{
void *gp = nullptr;
gp = dlsym(handle, "go");
if (!gp)
{
printf("tc is null\n");
printf("%s\n", dlerror());
}
else
{
printf("go is %d\n", *((int *)gp));
}
}

{
void *ptr = nullptr;
ptr = dlsym(handle, "m");
if (ptr == nullptr)
{
printf("m is null\n");
printf("%s\n", dlerror());
}
else
{
printf("find m\n");
}
}
dlclose(handle);

return 0;
}

//g++ -std=c++11 -rdynamic call_dynamic.cpp -o call_dynamic -ldl
```
2018-12-04 16:21:04 +08:00
回复了 zhiqiang 创建的主题 C C++什么情况下会出现类 static 成员析构错误?
如果不是 STL 的 bug,那么可能和我以前碰到的一个问题一样:由于源文件编码导致。你的源文件有的用 gbk 有的用 utf8. C++编译单元的编码和该编译单元的源文件相同。
2018-10-29 10:12:31 +08:00
回复了 xcai 创建的主题 Linux IBM 喜提红帽
Debian 或成最大赢家。
2018-10-08 16:53:09 +08:00
回复了 mrchi 创建的主题 程序员 入职提交材料中需要户口本复印件正常吗?
上海 部分公司需要 貌似跟社保相关
2018-08-17 09:50:20 +08:00
回复了 jokerL 创建的主题 问与答 有些事不吐不快
所以要么只生一个好,要么别生孩子。
2018-08-09 09:51:38 +08:00
回复了 wsds 创建的主题 程序员 c++不用 boost 库,怎么切割字符串?
1  2  
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2162 人在线   最高记录 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 18ms · UTC 10:32 · PVG 18:32 · LAX 03:32 · JFK 06:32
Developed with CodeLauncher
♥ Do have faith in what you're doing.