- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我收到 undefined reference pthread API ,我不知道如何解决它们?
场景如下:
libA.a -- 这是第 3 方库 [它包含许多依赖于 pthread 的 API]
libB.a -- 这是我自己的库。我使用了一些第三方库 [libA.a] 的 API 并创建了我自己的库。[我自己没有在 libB.a 中使用任何 pthread API]
我将 libA.a + libB.a + (A + B) 的 header 提供给我的客户端 exe。 -- 说 MAIN.exe
MAIN.cpp -- 将使用我的图书馆提供的 API。
当我尝试运行 MAIN.exe 时,出现 undefined reference 错误。
源码如下:
libA.a:只包含A.h和A.cpp
A 类
{
民众:
无效显示();
void printNumber();
};
#include "iostream"
#include "A.h"
#include<stdlib.h>
#include "pthread.h"
using namespace std;
void* printNum(void*)
{
sleep(1);
for(int i = 1; i<= 10; i++)
{
cout<<"i: "<<i<<endl;
}
pthread_exit(NULL);
return NULL;
}
void A::dispA()
{
cout<<"A::disp()"<<endl;
}
void A::printNumber()
{
pthread_t t1;
pthread_create(&t1, NULL, &printNum, NULL);
pthread_exit(NULL);
}
cd /practise/A
g++ -c A.cpp
ar -cvq libA.a *.o
libB.a:只包含B.h和B.cpp
class B
{
public:
void funB();
void dispB();
};
#include "B.h"
#include "iostream"
#include "A.h"
using namespace std;
void B::funB()
{
cout<<"B::funB()"<<endl;
}
void B::dispB()
{
A a;
a.dispA();
a.printNumber();
}
创建 libB.a 的命令:
cd /practise/B
g++ -c B.cpp -I../A
ar -cvq libB.a *.o
#include "iostream"
#include "B.h"
using namespace std;
int main()
{
B b;
b.dispB();
b.funB();
return 0;
}
创建main.exe的命令:
cd /practise/MAIN
g++ -o noThread MAIN.cpp -I../A -I../B -L../A -L../B -lB -lA
我得到的错误:../A/libA.a(A.o):在函数 A::printNumber()' 中:
pthread_create'collect2: ld 返回 1 个退出状态
A.cpp:(.text+0x8c): undefined reference
注意:我知道,如果我尝试使用 -lrt 标志,它不会给出任何错误。但问题是我的客户端 [MAIN.cpp] 不能使用 -lrt 标志或 -lpthread 或任何与线程相关的库。因此,他建议我提供单线程库。
那么,如何提供单线程库????
libA.a 是第三方的,我不能更改它的代码。libB.a 是我自己的库 [而且我必须使用 libA.a 的 API]
是否有任何特定的标志可用于使 main.cpp 正常运行??
另一个疑问:
为什么 Main.cpp 给我错误,即使客户端只调用线程独立函数:
int main()
{
B b;
//b.dispB(); <== commented thread dependent function
b.funB(); <== this doesn't depend on pthread. but still main.cpp is failing. Don't know WHY !!
return 0;
}
最佳答案
如果您确定没有从您的库使用的代码路径调用实际的 pthread
代码,那么您可以尝试制作 ptherad
调用的虚拟版本,如下所示:
DummyPThreads.c(注意 c 不是 c++)
int pthread_create(pthread_t*, const pthread_attr_t*, void* (*)(void *), void*)
{
return 0;
}
void pthread_exit(void*)
{
}
// etc...
编译:
gcc -c -o DummyPThreads.o DummyPThreads.c
添加到您的应用程序:
g++ -o noThread MAIN.cpp -I../A -I../B DummyPThreads.o -L../A -L../B -lB -lA
关于c++ - 如何在构建单线程库时删除 pthread undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36004333/
我是一名优秀的程序员,十分优秀!