Mokus
Mokus

Reputation: 10400

How can I switch between compilers in the makefile?

I have the following makefile:

CFLAGS=-c -Wall -std=c++11
MCFLAGS=-c -Wall -std=c++11

LDFLAGS= -shared
MLDFLAGS= 

MSOURCES=main.cpp MCC.cpp Point3D.cpp
SOURCES= mainDLL.cpp MCC.cpp Point3D.cpp

OBJECTS=$(SOURCES:.cpp=.o)
MOBJECTS=$(MSOURCES:.cpp=.o)

EXECUTABLE=h2r.dll
MEXECUTABLE=h2r

CC=i686-w64-mingw32-g++
CC=g++

all: clean $(MSOURCES) $(MEXECUTABLE)


dll: clean $(SOURCES) $(EXECUTABLE)


$(MEXECUTABLE): $(MOBJECTS)
    $(CC) $(MLDFLAGS) $(MOBJECTS) -o $@

$(EXECUTABLE): $(OBJECTS)
    $(CC) $(LDFLAGS) $(OBJECTS) -o $@

.cpp.o:
    $(CC) $(CFLAGS) $< -o $@

clean: 
    rm *.o $(MEXECUTABLE) $(EXECUTABLE)

How can I initialize the CC with the cross compiler(CC=i686-w64-mingw32-g++) when the make dll command is emitted and how can I use the gnu compiler when the make all is emitted?

Upvotes: 4

Views: 1056

Answers (3)

Tomasz Kaminski
Tomasz Kaminski

Reputation: 910

Your overwriting whatever is in the CC variable. Why don't you just have:

CC_DLL=i686-w64-mingw32-g++
CC=g++

And simply use the relevant one in your targets.

Upvotes: 1

simon
simon

Reputation: 2164

To set a variable based on what target is being executed you can do something like:

all: CC=g++
all: clean $(MSOURCES) $(MEXECUTABLE)

dll: CC=i686-w64-mingw32-g++
dll: clean $(SOURCES) $(EXECUTABLE)

Upvotes: 6

Rolf Lussi
Rolf Lussi

Reputation: 615

Define two different CC instead of redefining the one. Since you have different rules for the all and dll, you can just use the other compiler in the other rule. Somehow like this:

CCDLL=i686-w64-mingw32-g++
CCALL=g++

all: clean $(MSOURCES) $(MEXECUTABLE)


dll: clean $(SOURCES) $(EXECUTABLE)


$(MEXECUTABLE): $(MOBJECTS)
    $(CCALL) $(MLDFLAGS) $(MOBJECTS) -o $@

$(EXECUTABLE): $(OBJECTS)
    $(CCDLL) $(LDFLAGS) $(OBJECTS) -o $@

Upvotes: 3

Related Questions