2

I'm creating new threads in a function, and I've included pthread.h. But it's not working, I keep receiving the following error upon compiling:

undefined reference to `pthread_create'

The flags I'm using to compile are the following:

CFLAGS=-std=gnu99 -pthread -g -Wall -Wextra -Werror -Wmissing-declarations -Wmissing-prototypes -Werror-implicit-function-declaration -Wreturn-type -Wparentheses -Wunused -Wold-style-definition -Wundef -Wshadow -Wstrict-prototypes -Wswitch-default -Wunreachable-code

The compiler is gcc

Makefile:

CC=gcc
CFLAGS=-std=gnu99 -pthread -g -Wall -Wextra -Werror -Wmissing-declarations -Wmissing-prototypes -Werror-implicit-function-declaration -Wreturn-type -Wparentheses -Wunused -Wold-style-definition -Wundef -Wshadow -Wstrict-prototypes -Wswitch-default -Wunreachable-code

all: finder

finder: stack.o list.o finder.o
    $(CC) -o mfind stack.o list.o mfind.o

stack.o: stack.c stack.h
    $(CC) -c stack.c $(CFLAGS)

list.o: list.c list.h
    $(CC) -c list.c $(CFLAGS)

finder.o: finder.c finder.h
    $(CC) -c finder.c $(CFLAGS)

clean:
    rm -f *.o finder
Cows42
  • 307
  • 3
  • 12
  • `CFLAGS` is normally the wrong variable to put linked libraries into, many build systems put the `CFLAGS` on the command line *before* the files to be linked. Your build system might offer a `LIBS` variable where you can put `-pthread`. Not enough information to tell for sure. –  Oct 13 '17 at 14:40
  • And what *exact* command are you running when you get this error? – Chris Turner Oct 13 '17 at 14:40
  • minimal code available? – CS Pei Oct 13 '17 at 14:42
  • @FelixPalmen I use a makefile, therefore CFLAGS to just define the flags I'm using. – Cows42 Oct 13 '17 at 14:43
  • I'm merely just t rying to compile my program with a makefile. I'm only creating new threads. @ChrisTurner – Cows42 Oct 13 '17 at 14:43
  • @CSPei This is what I'm calling to create new threads: pthread_create(&newT->tid, NULL, beginSearch, (void *)newT); – Cows42 Oct 13 '17 at 14:44
  • @Cows42 yes, but that doesn't answer my question does it – Chris Turner Oct 13 '17 at 14:44
  • 1
    @Cows42 then show the (relevant parts of the) Makefile. I bet there's something like `gcc -o$@ $(CFLAGS) $<` for linking. `-pthread` must appear **after** the input files. –  Oct 13 '17 at 14:44
  • Makefile or no, we need to see the exact compile command being run or it's rather difficult to debug. I would guess the linking stage is failing for not being passed the -lpthread flag, but we'd need the actual failing compile command to be sure. – Christian Gibbons Oct 13 '17 at 14:45
  • Edit your question and post the complete Makefile – klutt Oct 13 '17 at 14:45
  • check the sample code [here] (https://linux.die.net/man/3/pthread_create) which can be compiled by 'gcc -pthread a.c' – CS Pei Oct 13 '17 at 14:46
  • Alright, I've edited main post with the makefile @FelixPalmen – Cows42 Oct 13 '17 at 14:46
  • 1
    Where in `$(CC) -o mfind stack.o list.o mfind.o` is `-pthread` or a variable that contains `-pthread`? – Chris Turner Oct 13 '17 at 14:47
  • @ChrisTurner ahhhhh... didn't see that $(CFLAGS) was missing on that row.. :/ Thanks! – Cows42 Oct 13 '17 at 14:49
  • @Cows42 no, you don't put `$(CFLAGS)` there. See my answer... `$(CFLAGS)` is by convention used for flags needed during *compilation*, `$(LDFLAGS)` for those used during linking (you don't need any here) and `$(LIBS)` for libraries that get linked. –  Oct 13 '17 at 14:50
  • @FelixPalmen oh ok I understand, thanks :) – Cows42 Oct 13 '17 at 14:51
  • @FelixPalmen there's no reason not to include `$(CFLAGS)` as well – Chris Turner Oct 13 '17 at 14:51
  • @ChrisTurner it serves no purpose and you get better readable output without heaps of redundant flags. Sooner or later, you *will* have the situation that you need different flags for different stages. –  Oct 13 '17 at 14:52
  • almost a duplicate: https://stackoverflow.com/questions/1662909/undefined-reference-to-pthread-create-in-linux?rq=1 – Jens Gustedt Oct 13 '17 at 15:20
  • Isn't it that way CFLAGS is only for those whose builds the package but not for package maintainer (means for people who generate autotools output files and distribute them along with package in source-code form)? For package maintainer AM_CFLAGS and target-specific CFLAGS seem to be intended. Package maintainer builds package for own testing. Real building takes place on user station. –  Jul 06 '18 at 08:44

1 Answers1

5

-pthread is needed at the linking stage, not when compiling the individual translation units. A typical approach would look like this:

CC=gcc
CFLAGS=-std=gnu99 -g -Wall -Wextra -Werror -Wmissing-declarations -Wmissing-prototypes -Werror-implicit-function-declaration -Wreturn-type -Wparentheses -Wunused -Wold-style-definition -Wundef -Wshadow -Wstrict-prototypes -Wswitch-default -Wunreachable-code
LIBS=-pthread

all: finder

finder: stack.o list.o finder.o
    $(CC) -o mfind stack.o list.o mfind.o $(LIBS)

stack.o: stack.c stack.h
    $(CC) -c stack.c $(CFLAGS)

list.o: list.c list.h
    $(CC) -c list.c $(CFLAGS)

finder.o: finder.c finder.h
    $(CC) -c finder.c $(CFLAGS)

clean:
    rm -f *.o finder