c - undefined reference (readline,pthread_create,pthread_detach),makefile不包含库

标签 c makefile compiler-errors pls

这可能是一个简单的答案,但是我正在尝试为简单的用户级文件系统编译代码。我在Windows Ubuntu子系统上运行代码。

我已经更新并安装了所有lpthread和lreadline库,并且在编译时仍会得到 undefined reference 。

gcc -Wall -g  -lreadline -lcurses -lpthread userfs.c  parse.c crash.c -o userfs
/tmp/ccNrZDqQ.o: In function `main':
/home/kupinah/userfs/userfs.c:75: undefined reference to `readline'
/tmp/ccwcrZEh.o: In function `init_crasher':
/home/kupinah/userfs/crash.c:10: undefined reference to `pthread_create'
/home/kupinah/userfs/crash.c:14: undefined reference to `pthread_detach'
collect2: error: ld returned 1 exit status
Makefile:12: recipe for target 'userfs' failed
make: *** [userfs] Error 1

这是每个代码的位置和 header 。

userfs.c:
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <fcntl.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <assert.h>
#include <string.h>
#include "parse.h"
#include "userfs.h"
#include "crash.h"
...
...
...

    while(1) {
        cmd_line = readline(buildPrompt());
        if (cmd_line == NULL) {
            fprintf(stderr, "Unable to read command\n");
            continue;
        }

...
...



生成文件
CC = gcc
COMPILER_WARNINGS = -Wall
GDB_FLAGS = -g 
GCOV_FLAGS = -fprofile-arcs -ftest-coverage
GPROF_FLAGS = -pg -a
LD_LIBS = -lreadline -lcurses -lpthread
CFLAGS = $(COMPILER_WARNINGS) $(GDB_FLAGS) $(LD_LIBS)

all: userfs

userfs: userfs.c parse.c crash.c
    $(CC) $(CFLAGS) userfs.c  parse.c crash.c -o userfs

clean:
    /bin/rm -f userfs *.o *~


帮助请。

最佳答案

@MadScientist帮助我解决了这一问题。

解决此问题的原因是对make文件进行了一些简单的编辑。

CC = gcc
COMPILER_WARNINGS = -Wall
GDB_FLAGS = -g
GCOV_FLAGS = -fprofile-arcs -ftest-coverage
GPROF_FLAGS = -pg -a
LD_LIBS = -lpthread -lreadline -lcurses
CFLAGS = $(COMPILER_WARNINGS) $(GDB_FLAGS)

all: userfs

userfs: userfs.c parse.c crash.c
        $(CC) $(CFLAGS) userfs.c  parse.c crash.c -o userfs $(LD_LIBS)

clean:
        /bin/rm -f userfs *.o *~

变化:
LD_LIBS = -lpthread -lreadline -lcurses ----重新排序
CFLAGS = $(COMPILER_WARNINGS) $(GDB_FLAGS) ----删除了$(LD_LIBS)
userfs: userfs.c parse.c crash.c $(CC) $(CFLAGS) userfs.c parse.c crash.c -o userfs $(LD_LIBS) ----添加了$(LD_LIBS)

关于c - undefined reference (readline,pthread_create,pthread_detach),makefile不包含库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61258113/

相关文章:

c - 如何计算字符串中的字符数(如果没有占用所有空间)

C Linux - fgets 的输入导致无限循环

c - 受限3体重力模拟c

c - 在 Makefile 中使用 vpath 和通配符

opencv - 在 Ubuntu/Linux 上用 C++ 编译 OpenCV 代码的 Makefile

c++ - 隐式类型转换 - 编译器错误

c - 不知道如何修复错误

android - 什么是 Android JNI 应用程序上下文中的模块?

c++ - 如何获得对操作数的引用?

c# - 类方法输出字符串消息,由于错误引用从 void 到字符串的转换不适用,因此无法进行单元测试