el.mojito
el.mojito

Reputation: 170

OpenMP Makefile, -fopenmp won't work

I'm trying to compile my project using a Makefile, but somehow the -fopenmp flag won't work. Here's the Makefile:

TARGET=isaac
CC=g++
CFLAGS=-Wall -O2 -fopenmp
LDFLAGS=-lm -lpthread -lrt

OBJ=src/main.o src/bhtree.o src/body.o src/configreader.o src/diagnostics.o src/output.o src/quad.o src/timing.o src/vector2.o

isaac: $(OBJ)
$(CC) $(CFLAGS) -o $(TARGET) $(OBJ) $(LDFLAGS)

%.o: src/%.cpp
$(CC) $(CFLAGS) -c $<

clean: 
rm src/*.o src/*~ isaac

and here is the output when calling "make"

g++    -c -o src/main.o src/main.cpp
g++    -c -o src/bhtree.o src/bhtree.cpp
g++    -c -o src/body.o src/body.cpp
g++    -c -o src/configreader.o src/configreader.cpp
g++    -c -o src/diagnostics.o src/diagnostics.cpp
g++    -c -o src/output.o src/output.cpp
g++    -c -o src/quad.o src/quad.cpp
g++    -c -o src/timing.o src/timing.cpp
g++    -c -o src/vector2.o src/vector2.cpp
g++ -Wall -O2 -fopenmp -o isaac src/main.o src/bhtree.o src/body.o src/configreader.o src/diagnostics.o src/output.o src/quad.o src/timing.o src/vector2.o -lm -lpthread -lrt

the -fopenmp flag is missing when the source files are compiled, so the finished executable is serial, not parallel.

How can I fix this?

Upvotes: 3

Views: 5074

Answers (1)

Lol4t0
Lol4t0

Reputation: 12547

The problem is that your rule does not apply at all. You are free to remove

%.o: src/%.cpp
    $(CC) $(CFLAGS) -c $<

and you'll get the same result as before. That's because some predefined rule is used instead of yours (I'm not great makefile expert though).

The core of the problem is that your rule is for ./*.o files, but you need ./src/*.o for isaac. You can change your rule

src/%.o: src/%.cpp
    $(CC) $(CFLAGS) -c $<

Or (better) move all autogenerated staff somewhere from src.

Upvotes: 2

Related Questions