0

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.

kupinah
  • 3
  • 2
  • Since you don't show your makefile we can't tell you how to do it, but you must always, always, _always_ put libraries (`-l...`) at the _end_ of the link line, after all source and object files, not at the beginning. All common linkers are single-pass linkers, and so the order that libraries are listed is very important. – MadScientist Apr 16 '20 at 19:08
  • @MadScientist I have added the makefile, so you think this is an order of the libraries issue? – kupinah Apr 16 '20 at 19:18

1 Answers1

0

@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)

kupinah
  • 3
  • 2
  • Correct. You don't want `LD_LIBS` in `CFLAGS` anyway because if you ever decide to build individual object files (so you don't have to recompile everything any time anything changes) you'll want to use `CFLAGS` when you compile object files and you can't put libraries on compile lines – MadScientist Apr 16 '20 at 21:03