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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With