Tryer
Tryer

Reputation: 4080

Compiling different source files with different flags

My makefile currently is:

# Environment
...
CC=gcc
CCC=g++
CXX=g++

# Macros
...

# Object Files
OBJECTFILES= \
    ${OBJECTDIR}/_ext/511e4115/File1.o \
    ${OBJECTDIR}/_ext/511e4115/File2.o
    
# CC Compiler Flags
CCFLAGS=-m64 -fno-common -fPIC -fno-strict-aliasing -fexceptions -fopenmp -Wall -Wextra
CXXFLAGS=-m64 -fno-common -fPIC -fno-strict-aliasing -fexceptions -fopenmp -Wall -Wextra

Is it possible at this stage to have:

CCFLAGS_DIFF=-m64 -fno-common -fPIC -fno-strict-aliasing -fexceptions -fopenmp
CXXFLAGS_DIFF=-m64 -fno-common -fPIC -fno-strict-aliasing -fexceptions -fopenmp

The difference between the original CCFLAGS, CXXFLAGS and the later CCFLAGS_DIFF, CXXFLAGS_DIFF is that the latter does not have -Wall -Wextra.

I want to selectively apply the later flags (the ones with _DIFF) to File2.cpp below while File1.cpp gets compiled with the original flags.

//Makefile continues

${OBJECTDIR}/_ext/511e4115/File1.o: ../src/File1.cpp
    ${MKDIR} -p ${OBJECTDIR}/_ext/511e4115
    ${RM} "[email protected]"
    $(COMPILE.cc) -O2 -DNDEBUG -I../include -std=c++14 -MMD -MP -MF "[email protected]" -o ${OBJECTDIR}/_ext/511e4115/File1.o ../src/File1.cpp

${OBJECTDIR}/_ext/511e4115/File2.o: ../src/File2.cpp
    ${MKDIR} -p ${OBJECTDIR}/_ext/511e4115
    ${RM} "[email protected]"
    $(COMPILE.cc) -O2 -DNDEBUG -I../include -std=c++14 -MMD -MP -MF "[email protected]" -o ${OBJECTDIR}/_ext/511e4115/File2.o ../src/File2.cpp

As of now, it is not clear to me where exactly the original flags, CCFLAGS and CXXFLAGS are referred to. As a result, File2.cpp gets compiled with -Wall -Wextra.

Upvotes: 0

Views: 376

Answers (1)

Botje
Botje

Reputation: 31055

COMPILE.cc is a built-in variable:

COMPILE.cc = $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c

and you can override variables on a per-rule level, so with the following Makefile:

CXXFLAGS := all the rules 
CXXFLAGS_DIFF := $(subst all the, fewer, $(CXXFLAGS))
bad.o: CXXFLAGS=$(CXXFLAGS_DIFF)

I get:

% make -n good.o
c++ all the rules   -c -o good.o good.cc
% make -n bad.o
c++ fewer rules   -c -o bad.o bad.cc

Upvotes: 3

Related Questions