这可能是一个简单的答案,但是我正在尝试为简单的用户级文件系统编译代码。我在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/

10-10 03:43