Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating makefile with pthreads

I want to create a makefile that supports posix semaphores. That is what I've got so far:

CFLAGS=-g -ansi -pedantic -Wall -Werror -D_XOPEN_SOURCE=600
LDFLAGS=-pthread 
CC=gcc
OBJECTS=MsgQueueMain.o MsgQueue.o Queue.o MyMalloc.o
TARGET=MsgQueueMain

all: $(TARGET)

$(TARGET): $(OBJECTS)
    $(CC) $(OBJECTS) -o $@

include depends

depends:
    $(CC) -MM $(OBJECTS:.o=.c) > depends

clean:
    rm ./$(TARGET) *.o

For some reason, I'm getting "undefined reference" for all calls to semaphore.h api functions.

like image 978
user2130932 Avatar asked Sep 19 '25 21:09

user2130932


1 Answers

You need to link with the rt or pthread library. From man sem_destroy reference page:

Link with -lrt or -pthread.

Add to the end of the compiler command as order is important (unsure if order is important for -pthread as this defines some macros and adds -lpthread).

As commented by Vlad Lazarenko the LDFLAGS is not part of your TARGET. Change to:

$(CC) $(OBJECTS) -o $@ $(LDFLAGS)

like image 136
hmjd Avatar answered Sep 22 '25 12:09

hmjd