user2287387
user2287387

Reputation: 47

Makefile with OpenMP: cannot specify -o with -c, -S or -E with multiple files

I have a problem including parallel calculation with OpenMP in my makefile. The error I got is

cannot specify -o with -c, -S or -E with multiple files

Here is my makefile:

GSLFLAGS := pkg-config --cflags gsl
LIBGSL := pkg-config --libs gsl

CFLAGS = -c -C -O3 -openmp=parallel

lateral.o:lateral.cxx

    g++ -c lateral.cxx 

main.o:main.cxx 

    g++ -c main.cxx  $< ${GSLFLAGS} ${CFLAGS}

alg:main.o lateral.o

    g++ -o  $@ $^ ${LIBGSL}

Upvotes: 0

Views: 1310

Answers (2)

Thibaut
Thibaut

Reputation: 2420

The line

g++ -c main.cxx $< ${GSLFLAGS} ${CFLAGS}

Should read

g++ -c main.cxx ${GSLFLAGS} ${CFLAGS}

because, $< expands to the first prerequisite, main.cxx, giving g++ -c main.cxx main.cxx. To avoid that, you can even write generic rules like:

%.o: %.cxx
    g++ -c -o $@ ${GSLFLAGS} ${CFLAGS} $^

And you don't need special rules for main.o and lateral.o, the complete makefile would be:

GSLFLAGS := pkg-config --cflags gsl
LIBGSL := pkg-config --libs gsl

all: alg

%.o: %.cxx
    g++ -c -o $@ ${GSLFLAGS} ${CFLAGS} $^

alg: main.o lateral.o
    g++ -o  $@ $^ ${LIBGSL}

You can find a detailed explanation of the syntax above here or a much more detailed documentation here.

Edit:

Sorry I missed the C flags, there is also an error there:

CFLAGS = -c -C -O3 -openmp=parallel

Your should remove the -c since you are already using it in the rule, which is what gcc is complaining about.

Upvotes: 2

Schnouki
Schnouki

Reputation: 7717

IMO (but can't test right now) there's something wrong with this line:

CFLAGS = -c -C -O3 -openmp=parallel

I think -openmp=parallel should just be -fopenmp. First because that's the correct compile flag for OpenMP, second because what's after -o will be interpreted as the name of the output file. And as your error message says, you can't use -c and -o together.

Upvotes: 1

Related Questions