Taztingo
Taztingo

Reputation: 1945

Makefile won't compile correctly

I can't figure out how to get my Makefile to compile a group of cpp files using the -lpthread. The problem is occurring with g++ *.cpp -c. My functions (Which are in external files), can't find pthread_exit. What is the correct way to do this?

#
#Makefile for producerConsumer
#

RM = rm -f
#SRC = producer.cpp consumer.cpp
#OBJ = $(SRC:.cpp=.o)
TESTNAME = test
TESTSRC = main.cpp
#

retest: re test

test: 
    g++ *.cpp -c
    g++ *.o -o $(TESTNAME) -lpthread

clean:
    -$(RM) *.o
    -$(RM) *~
    -$(RM) \#*
    -$(RM) *.core

fclean: clean
    -$(RM) $(TESTNAME)
    -$(RM) *.o

re: fclean

Upvotes: 1

Views: 278

Answers (1)

Sander De Dycker
Sander De Dycker

Reputation: 16243

It looks like you're using Linux, so use -pthread instead of -lpthread, and make sure to add it to both the compilation and linking commands. The -pthread option will make sure pthread support is activated while (pre-)compiling your code, and will ensure that the correct libraries are linked in properly.

Additionally, make sure you've included <pthread.h> in any compilation unit that uses pthread functions, so they all look for the correct symbols.

Here's a sample makefile :

CXX = g++
LD = g++
CXXFLAGS = -Wall -pthread
LDFLAGS = -pthread

SOURCES = main.cpp
OBJECTS = $(SOURCES:.cpp=.o)
BINARY = test

all : $(SOURCES) $(BINARY)

$(BINARY) : $(OBJECTS) 
    $(LD) $(LDFLAGS) $^ -o $@

.cpp.o :
    $(CXX) $(CXXFLAGS) -c $< -o $@

.PHONY : clean
clean :
    rm -f *.o $(BINARY)

Upvotes: 3

Related Questions