0

I can't compile my project. When I run make command I get the following error.

If I try compile shareMemoryWriter.cpp alone with command g++ -std=c++11 shareMemoryWriter.cpp main2.cpp -o main -lpthread -lrt the compile succeed

operating system: linux mint Makefile:

    CC = g++

CFLAGS=-Wall -g -std=c++11 -lpthread -lrt 

LDFLAGS= -std=c++11 -lpthread -lrt

SOURCES=main.cpp \
daq.cpp \
srs.cpp \
fec.cpp \
chip.cpp \
detector.cpp \
chamber.cpp \
chamberSpecs.cpp \
multilayer.cpp \
layer.cpp \
readout.cpp \
connector.cpp \
connectorSpecs.cpp \
createEvents.cpp \
event.cpp \
shareMemoryWriter.cpp \
Coordinates.cpp

OBJECTS=$(SOURCES:.cpp=.o)

EXECUTABLE=main

all: $(SOURCES) $(EXECUTABLE)

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

.cpp.o:
    $(CC) $(CFLAGS)  -c $<  -o $@

clean:
    $(RM) *.o *~ $(MAIN)

depend: $(SRCS)
    makedepend  $^

Make compile output:

make

g++ -Wall -g -std=c++11 -lpthread -lrt   -c main.cpp  -o main.o
g++ -Wall -g -std=c++11 -lpthread -lrt   -c daq.cpp  -o daq.o
g++ -Wall -g -std=c++11 -lpthread -lrt   -c srs.cpp  -o srs.o
g++ -Wall -g -std=c++11 -lpthread -lrt   -c fec.cpp  -o fec.o
g++ -Wall -g -std=c++11 -lpthread -lrt   -c chip.cpp  -o chip.o
g++ -Wall -g -std=c++11 -lpthread -lrt   -c detector.cpp  -o detector.o
g++ -Wall -g -std=c++11 -lpthread -lrt   -c chamber.cpp  -o chamber.o
g++ -Wall -g -std=c++11 -lpthread -lrt   -c chamberSpecs.cpp  -o chamberSpecs.o
g++ -Wall -g -std=c++11 -lpthread -lrt   -c multilayer.cpp  -o multilayer.o
g++ -Wall -g -std=c++11 -lpthread -lrt   -c layer.cpp  -o layer.o
g++ -Wall -g -std=c++11 -lpthread -lrt   -c readout.cpp  -o readout.o
g++ -Wall -g -std=c++11 -lpthread -lrt   -c connector.cpp  -o connector.o
g++ -Wall -g -std=c++11 -lpthread -lrt   -c connectorSpecs.cpp  -o connectorSpecs.o
g++ -Wall -g -std=c++11 -lpthread -lrt   -c createEvents.cpp  -o createEvents.o
g++ -Wall -g -std=c++11 -lpthread -lrt   -c event.cpp  -o event.o
g++ -Wall -g -std=c++11 -lpthread -lrt   -c shareMemoryWriter.cpp  -o shareMemoryWriter.o
g++ -Wall -g -std=c++11 -lpthread -lrt   -c Coordinates.cpp  -o Coordinates.o
g++ -std=c++11 -lpthread -lrt main.o daq.o srs.o fec.o chip.o detector.o chamber.o chamberSpecs.o multilayer.o layer.o readout.o connector.o connectorSpecs.o createEvents.o event.o shareMemoryWriter.o Coordinates.o -o main
shareMemoryWriter.o: In function `boost::interprocess::shared_memory_object::priv_open_or_create(boost::interprocess::ipcdetail::create_enum_t, char const*, boost::interprocess::mode_t, boost::interprocess::permissions const&)':
/usr/include/boost/interprocess/shared_memory_object.hpp:309: undefined reference to `shm_open'
/usr/include/boost/interprocess/shared_memory_object.hpp:315: undefined reference to `shm_open'
/usr/include/boost/interprocess/shared_memory_object.hpp:327: undefined reference to `shm_open'
/usr/include/boost/interprocess/shared_memory_object.hpp:334: undefined reference to `shm_open'
shareMemoryWriter.o: In function `boost::interprocess::ipcdetail::mutexattr_wrapper::mutexattr_wrapper(bool)':
/usr/include/boost/interprocess/sync/posix/pthread_helpers.hpp:37: undefined reference to `pthread_mutexattr_init'
/usr/include/boost/interprocess/sync/posix/pthread_helpers.hpp:38: undefined reference to `pthread_mutexattr_setpshared'
/usr/include/boost/interprocess/sync/posix/pthread_helpers.hpp:40: undefined reference to `pthread_mutexattr_settype'
shareMemoryWriter.o: In function `boost::interprocess::ipcdetail::mutexattr_wrapper::~mutexattr_wrapper()':
/usr/include/boost/interprocess/sync/posix/pthread_helpers.hpp:45: undefined reference to `pthread_mutexattr_destroy'
shareMemoryWriter.o: In function `boost::interprocess::ipcdetail::posix_condition::posix_condition()':
/usr/include/boost/interprocess/sync/posix/condition.hpp:132: undefined reference to `pthread_condattr_setpshared'
shareMemoryWriter.o: In function `boost::interprocess::ipcdetail::semaphore_open(sem_t*&, boost::interprocess::ipcdetail::create_enum_t, char const*, unsigned int, boost::interprocess::permissions const&)':
/usr/include/boost/interprocess/sync/posix/semaphore_wrapper.hpp:61: undefined reference to `sem_open'
/usr/include/boost/interprocess/sync/posix/semaphore_wrapper.hpp:69: undefined reference to `sem_open'
/usr/include/boost/interprocess/sync/posix/semaphore_wrapper.hpp:77: undefined reference to `sem_open'
shareMemoryWriter.o: In function `boost::interprocess::ipcdetail::semaphore_close(sem_t*)':
/usr/include/boost/interprocess/sync/posix/semaphore_wrapper.hpp:105: undefined reference to `sem_close'
shareMemoryWriter.o: In function `boost::interprocess::ipcdetail::semaphore_post(sem_t*)':
/usr/include/boost/interprocess/sync/posix/semaphore_wrapper.hpp:148: undefined reference to `sem_post'
shareMemoryWriter.o: In function `boost::interprocess::ipcdetail::semaphore_try_wait(sem_t*)':
/usr/include/boost/interprocess/sync/posix/semaphore_wrapper.hpp:164: undefined reference to `sem_trywait'
collect2: error: ld returned 1 exit status
make: *** [main] Error 1
Angelos
  • 533
  • 6
  • 19

1 Answers1

0

Don't do this:

CFLAGS=-Wall -g -std=c++11 -lpthread -lrt

Linker flags do not belong on your compile line, only on link line.

Don't do this either:

LDFLAGS= -std=c++11 -lpthread -lrt

The -std=c++11 is a compile flag, and doesn't belong on your link line.

This:

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

puts libraries at the beginning of the link line. They should be at the end. This answer explains why that's important.

Community
  • 1
  • 1
Employed Russian
  • 199,314
  • 34
  • 295
  • 362
  • 1
    Thank you for the answer I change CFLAGS=-Wall -g -std=c++11 -lpthread -lrt to CFLAGS=-std=c++11 -Wall -g and LDFLAGS= -std=c++11 -lpthread -lrt to LDFLAGS=-lpthread -lrt and $(LDFLAGS) $(CC) $(LDFLAGS) $(OBJECTS) -o $@ to $(CC) $(OBJECTS) -o $@ $(LDFLAGS) it works – Angelos Apr 11 '15 at 12:02