3

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'

3
  • 1
    Move -lpthread into the recipe for main. Commented Nov 20, 2015 at 1:57
  • 1
    Do not edit your question to include the answer. It invalidates having asked the question in the first place. Commented Nov 20, 2015 at 1:59
  • Sorry that didn't work. I realized I had left it out and hand edited it back in. Commented Nov 20, 2015 at 1:59

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Nice. You know your makefile's.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.