cmakefilecompiler-errorspls

Undefined reference to (readline, pthread_create, pthread_detach), makefile not including libraries


This may be a simple answer but I am trying to compile code for a simple User level file system. I am running my code on a windows Ubuntu subsystem.

I have all the lpthread and lreadline libraries updated and installed and I still get the undefined reference when compiling.

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

Here is the code locations and headers included for each.

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;
        }

...
...


makefile

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 *~

Help pls.


Solution

  • @MadScientist Helped me out on this one.

    What fixed this issue was some simple edits to the make file.

    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 *~
    

    Changes:

    LD_LIBS = -lpthread -lreadline -lcurses---- Reordering

    CFLAGS = $(COMPILER_WARNINGS) $(GDB_FLAGS)---- Removed $(LD_LIBS)

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