Makefile说变量是空的但不是

Ove*_*ler 0 c++ makefile

我正在尝试使用以下内容创建makefile:

$(CXX)=g++
$(SRC)=../src
$(INCL)=../include
all: cpu ram temperature swap statusshooter
    $(CXX) main.cpp cpu.o ram.o temperature.o swap.o statusshooter.o -o main.o -I$(INCL) -Ofast -Wall -lyaml-cpp -lglog -lpqxx -lpq
cpu:
    $(CXX) -c $(SRC)/CCpu.cpp -o cpu.o -Ofast -Wall 
ram:
    $(CXX) -c $(SRC)/CRam.cpp -o ram.o -Ofast -Wall 
temperature:
    $(CXX) -c $(SRC)/CTemperature.cpp -o temperature.o -Ofast -Wall
swap:
    $(CXX) -c $(SRC)/CSwap.cpp -o swap.o -Ofast -Wall  
statusshooter:
    $(CXX) -c $(SRC)/CStatusShooter.cpp -o statusshooter.o -Ofast -Wall  
Run Code Online (Sandbox Code Playgroud)

执行该makefile: make CXX=g++-4.7抛出以下错误:

makefile:2: *** empty variable name.  Stop.
Run Code Online (Sandbox Code Playgroud)

怎么解决这个?

Joh*_*ger 7

语法$(CXX)用于评估make变量,而不是分配给它.你要

CXX  = g++
SRC  = ../src
INCL = ../include

[...]
Run Code Online (Sandbox Code Playgroud)

  • $(CXX)= g ++`之所以起作用,是因为CXX变量的默认值为g ++,因此在替换变量变量后,该行变为有效的g ++ = g ++(将g ++变量设置为g ++)。但是SRC和INCL没有默认值,因此`$(SRC)= .. / src`将变成== .. / src`,这不是十分有效的Makefile语法 (2认同)