问题
The following is defined in the makefile:
CXX = g++
CXXFLAGS = -std=c++11
I would like to compile my code with OpenMP directives without changing the original makefile (which runs perfectly). The manual suggests a way to do this, is by changing it on the command line. However, when I run,
make CXXFLAGS=-std=c++11 -fopenmp
the error mentioned before pops up. Can someone help me understand what I'm doing wrong?
回答1:
The problem here is the space between -std=c++11
and -fopenmp
. It splits these two arguments up and the -fopenmp
is interpreted as the option -f
then openmp
is interpreted as a makefile
that make
attempts to build because it can't find it in the current directory.
You need to execute make
with quotes ("
) like
$ make CXXFLAGS="-std=c++11 -fopenmp"
this will pass CXXFLAGS= -std=c++11 -fopenmp
to make
. We can see this behaviour with the following simple makefile
.
CXX = g++
CXXFLAGS = -std=c++11
all:
@echo $(CXXFLAGS)
Running make
will produce the output
$ make
-std=c++11
Running make CXXFLAGS="-std=c++11 -fopenmp"
will produce
$ make
-std=c++11 -fopenmp
the correct output and what we were hoping to achieve.
As an aside the command, make CXXFLAGS= -std=c++11 -fopenmp
, in your question should produce more errors than just
make: openmp: No such file or directory
make: *** No rule to make target 'openmp'. Stop.
because of the space between CXXFLAGS=
and -std=c++11
. I get
$ make CXXFLAGS= -std=c++11 -fopenmp
make: invalid option -- =
make: invalid option -- c
make: invalid option -- +
make: invalid option -- +
make: invalid option -- 1
make: invalid option -- 1
If for instance you want to compile with -std=c++14
instead of -std=c++11
you would need to execute make
with
$ make CXXFLAGS=-std=c++14
note: no space, or equivalently with
$ make CXXFLAGS="-std=c++14"
again without a space.
回答2:
Space - The Final Frontier. You need quotes as in
make CXXFLAGS="-std=c++11 -fopenmp"
The shell splits command line words at word boundaries delimited by white space. Quoting is used to avoid word-splitting.
来源:https://stackoverflow.com/questions/32233074/no-rule-to-make-target-openmp