Gio
Gio

Reputation: 4229

G++ command line works with threading flag, linux makefile does not

This command line works beautifully on ubuntu (using C++ and threading):

g++ -std=c++11 prog.cpp -o prog.out -lpthread

my makefile just blows up:

all: main

main: prog.o
    g++ -o prog prog.o

prog.o: prog.cpp
    g++ -std=c++11 -c prog.cpp -lpthread

I'm not sure but it appears the -lpthread flag isn't being picked up. It's late and I've been working on the makefile for two hours, and any help would be appreciated.

make returns an error:undefined reference to 'pthread_create'

Upvotes: 3

Views: 145

Answers (1)

Etan Reisner
Etan Reisner

Reputation: 81012

You neglected to explain what "blows up" means.

But presumably it means "fails to link" and that would be because you put it on the wrong command.

-l is a linker flag but you have it in the compilation command.

You need to move it to the main target.

Also you make note of make rule #2 (from the GNU make maintainer):

Every non-.PHONY rule must update a file with the exact name of its target.

Make sure every command script touches the file “$@“–not “../$@“, or “$(notdir $@)“, but exactly $@. That way you and GNU make always agree.

You could also greatly simplify your makefile (down to essentially nothing) if you wanted to take advantage of the built-in rules.

CPPFLAGS := -std=c++11
LDLIBS := -lpthread

all: prog

That's it.

Upvotes: 3

Related Questions