符号引用错误
Symbol referencing errors
当我用g++ thread.cpp -o thread -lpthread编译时,我得到了这个错误,我似乎找不到引用错误:
Undefined first referenced
symbol in file
sem_destroy /var/tmp//ccfHWR7G.o
sem_init /var/tmp//ccfHWR7G.o
sem_post /var/tmp//ccfHWR7G.o
sem_wait /var/tmp//ccfHWR7G.o
ld: fatal: symbol referencing errors. No output written to thread
collect2: ld returned 1 exit status
我的头文件只包含一些全局变量和函数名:
#ifndef THREAD_H_
#define THREAD_H_
#define NUM_THREADS 6
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <semaphore.h>
#include <fstream>
#include <unistd.h>
using namespace std;
sem_t SEM;
fstream of;
pthread_t thread[NUM_THREADS];
pthread_attr_t attr;
int rc;
long t;
void *status;
void *Busy_Work(void *t);
void Open_Initialize_File();
void Initialize_Set_Attribute();
void Create_Thread();
void Free_Attricutes_Wait();
#endif /* THREAD_H_ */
这是我的主文件和检查与IDE没有任何基本的语法,我错过了:
#include "thread.h"
int main (int argc, char *argv[]) {
Open_Initialize_File();
Initialize_Set_Attribute();
Create_Thread();
Free_Attricutes_Wait();
sem_destroy(&SEM);
cout << "Main: program completed" << endl << "Exiting" << endl;
pthread_exit(NULL);
}
void Open_Initialize_File() {
of.open("SHARED.txt");
of << pthread_self() << "r" << endl;
of.close();
}
void Initialize_Set_Attribute() {
sem_init(&SEM, 0, 1);
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_JOINABLE);
}
void Create_Thread() {
for (int t = 0; t < NUM_THREADS; t++) {
rc = pthread_create(&thread[t], &attr, Busy_Work, (void *)(t+1));
if (rc) {
cout << "ERROR: return code from pthread_create() is " << rc << endl;
exit(-1);
}
}
}
void *Busy_Work(void *t) {
int i;
long tid = (long)t;
for (i=0; i<10; i++)
{
sem_wait(&SEM);
cout << "Thread" << tid << "Thread id< " << pthread_self() << " >running..." << endl;
of.open("SHARED.txt",std::fstream::app);//opening file in append mode
of << pthread_self() << "r" << endl;
of.close();
sem_post(&SEM);
if(tid%2==0)
sleep(2);
else
sleep(3);
}
pthread_exit((void*) t);
}
void Free_Attricutes_Wait() {
pthread_attr_destroy(&attr);
for (t = 0; t < NUM_THREADS; t++) {
rc = pthread_join(thread[t], &status);
if (rc) {
cout << "ERROR: return code from pthread_join() is" << rc << endl;
exit(-1);
}
cout << "Thread " << t << " Thread id <" << pthread_self() << ">exited" << endl;
}
}
链接器缺少一些符号,这可能表明还需要链接一些额外的外部库。在unix平台上,解决这个问题的一个好方法是查看手册页:
男人sem_init
这通常会告诉你需要做什么。在本例中,它将指定您需要使用-lrt
相关文章:
- 如何修复此错误:未定义对"距离(浮点数,浮点数,浮点数,浮点数,浮点数)"的引用
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- 对C宏的未定义引用,但在定义它时会出现重新定义错误
- C++引用错误
- 已定义函数时出现 G++ "未定义的引用"错误
- 错误:使用通用引用的声明冲突
- 链接 cmake 时出现未定义的引用错误
- 错误:未定义对'oboe::AudioStreamBuilder::openStream(oboe::AudioStream**)'的引用
- 链接器错误:未定义对"Reference_Genome::seq[abi:cxx11]"的引用
- C++ 中的编译错误:未定义对"主"的引用 collect2:错误:ld 返回 1 个退出状态
- 错误:无法解析对重载函数的引用;你的意思是调用它吗?
- 在 gtest 中初始化堆栈上的引用变量的隔离错误
- 运行时错误:引用绑定到类型为"int"的空指针
- 隐式重新解释引用时强制转换,没有警告/错误
- 只有级联分类器会发出未定义的引用错误
- 错误:未定义对cv::cudacodec::createVideoReader的引用
- 模板流运算符重载错误:引用初始化无效,与basic_istream和basic_ifstream之间的差异有关
- 对于我的 ComplexNumber 中的某些方法,我得到了一个奇怪的未定义错误引用.cpp,不过我对模板很陌生
- 从对象调用成员对象,错误:引用非常量值的初始值必须是左值
- Qt并发错误:引用非静态成员