【问题标题】:Symbol referencing errors符号引用错误
【发布时间】:2014-06-05 18:54:27
【问题描述】:

当我使用 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;
}
}

【问题讨论】:

  • 该错误意味着链接器找不到指示的符号。您可能需要链接到一些额外的库,例如 rt (-lrt)。你在哪个平台上工作?
  • @mwijns Unix 是我的工作
  • @mwijns 我添加了 -lrt 并编译了
  • 当它运行良好,但不创建 .txt 文件
  • 检查你打开的文件流是否ok,我觉得你还需要指定std::fstream::out。

标签: c++ pthreads posix semaphore


【解决方案1】:

链接器缺少一些符号,这可能表明还需要链接一些额外的外部库。在 unix 平台上,了解如何解决此问题的一个好方法是查看手册页:

man sem_init

这通常会告诉您需要做什么。在这种情况下,它将指定您需要与 -lrt 链接

【讨论】:

    猜你喜欢
    • 2021-11-08
    • 2015-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-21
    • 2014-11-21
    • 2020-12-27
    相关资源
    最近更新 更多