1

I want to compile a program which is using threads. I use the #include <pthread.h> library, however when I compile it using my make file I get the following errors:


file.c:(.text+0x378): undefined referece to `pthread_create`
file.c:(.text+0x3ad): undefined referece to `pthread_join`

The program run fine with the normal gcc compilator using -lpthread flag The source of the make file that I am using:

CC=gcc
COMPILE.c=$(CC) $(CFLAGS) $(CPPFLAGS) -c
LINK.c=$(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) -lpthread


CFLAGS  = -Wall -O -I$(dir $(LIBLAB))

TEMPFILES = core core.* *.o temp.* *.out typescript*

all:
    @echo "Please compile directly the intended programs by name"

clean:
    rm -f ${TEMPFILES}

I have set the -lpthread flag in the make file but is not working, what changes should I make in the make file?

1 Answers1

1

Your replacement for LINK.c is wrong. You can't add libraries to this because it means they appear on the link line first.

If you'd shown us the output of make, in addition to the makefile, probably we would have seen it more quickly. Libraries like -lpthread must come at the end of the link line, after any objects that might have needed to use the library.

As I mentioned in comments, you should remove the LINK.c setting and add:

CFLAGS = -pthread

and that should be good enough. If you do need to add libraries, you should use LDLIBS like this:

LDLIBS = -lpthread

not replace the LINK.c variable.

MadScientist
  • 92,819
  • 9
  • 109
  • 136