C 单个文件的未定义引用

C 单个文件的未定义引用,c,linker,pthreads,undefined-reference,C,Linker,Pthreads,Undefined Reference,我在StackOverflow上的其他文章中看到,未定义的引用错误意味着缺少定义,通常要修复它,必须在编译时链接文件。但我只编译了一个文件。我在函数pthread\u detach和pthread\u create中遇到此错误 /tmp/ccAET7bU.o: In function `sample_thread1': foo.c:(.text+0x2a): undefined reference to `pthread_detach' /tmp/ccAET7bU.o: In function

我在StackOverflow上的其他文章中看到,未定义的引用错误意味着缺少定义,通常要修复它,必须在编译时链接文件。但我只编译了一个文件。我在函数
pthread\u detach
pthread\u create
中遇到此错误

/tmp/ccAET7bU.o: In function `sample_thread1':
foo.c:(.text+0x2a): undefined reference to `pthread_detach'

/tmp/ccAET7bU.o: In function `main':
foo.c:(.text+0x85): undefined reference to `pthread_create'

collect2: error: ld returned 1 exit status
守则:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <memory.h>
#include <signal.h>
#include <string.h>
#include <linux/unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/syscall.h>
#include <netinet/in.h>
#include <pthread.h>

pid_t gettid(void)
{
    return syscall(__NR_gettid);
}

void *sample_thread1(void *x_void_ptr)
{
    FILE *fp;
    int counter;

    pthread_detach(pthread_self());

    if((fp = fopen("thread_data.txt", "w")) == NULL)
    {
        printf("ERROR : Thread cannot open data file.\n");
        return;
    }   
    counter = 1;
    while(1);
    {
        fprintf(fp, "INFO : Thread1 id %d output message %10d\n",
                gettid(), counter);
        fflush(fp);
        counter++;
        sleep(1);
    }
    pthread_exit(NULL);
}

int main(int argc, char *argv[])
{
    int counter;
    pthread_t sample_thread_t1;

    if(pthread_create(&sample_thread_t1, NULL, sample_thread1, NULL))
    {
        fprintf(stderr, "Error creating thread\n");
        exit(-1);
    }
    system("clear");

    counter = 1;
    while(1)
    {
        printf("INFO : Main Process Counter = %d\n", counter);
        counter++;
        sleep(1);
    }
    return(0);
} 
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
pid\u t getId(无效)
{
返回系统调用(_NR_getId);
}
void*sample\u thread1(void*x\u void\u ptr)
{
文件*fp;
整数计数器;
pthread_detach(pthread_self());
if((fp=fopen(“thread_data.txt”,“w”))==NULL)
{
printf(“错误:线程无法打开数据文件。\n”);
返回;
}   
计数器=1;
而(1),;
{
fprintf(fp,“信息:线程1 id%d输出消息%10d\n”,
gettid(),计数器);
fflush(fp);
计数器++;
睡眠(1);
}
pthread_exit(NULL);
}
int main(int argc,char*argv[])
{
整数计数器;
pthread_t sample_thread_t1;
if(pthread_create(&sample_thread_t1,NULL,sample_thread1,NULL))
{
fprintf(stderr,“创建线程时出错\n”);
出口(-1);
}
系统(“清除”);
计数器=1;
而(1)
{
printf(“信息:主进程计数器=%d\n”,计数器);
计数器++;
睡眠(1);
}
返回(0);
} 

您必须使用GCC或clang的
-lpthread
-pthread
选项将其与
pthread
库链接

示例:

gcc your_file.c -o program -lpthread
# or gcc your_file.c -o program -pthread
# the same for clang

我认为您需要将-lpthread传递给gcc,让它链接到pthread库。未定义的引用只是意味着链接器找不到您在代码中调用的函数,您正在为此运行哪个操作系统?对于Linux,请阅读:顺便说一句:我觉得标题中的键入很尴尬…
-lpthread
-pthread
不一定相同。您应该查阅必须使用的实现文档。例如,对于Linux,手册页()明确声明使用
-pthread
@alk,有三次或三次以上,我在越狱的iPhone上遇到了
-lpthread
问题,并且使用了
-pthread
,效果非常好。这就是为什么我建议在顶部尝试两个选项。
-pthread
总是意味着链接PThreads库,这就是
-lpthread
所做的,而
-pthread
可能做得更多,比如设置编译时“开关”。