Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to instruct Makefile to use different compilers

Tags:

makefile

I have to build my code for two different platforms at once using two different compilers and libraries. How can I do this with single make file.

Currently my makefile contents are given below. How can I instruct it to change the compiler to gcc within the same makefile?

VPATH = /root/Documents/project/src 

CROSS_COMPILE  = /opt/compilers/bin/compiler1
CXX = $(CROSS_COMPILE)-g++
CC = $(CROSS_COMPILE)-gcc
AS = $(CROSS_COMPILE)-as
AR = $(CROSS_COMPILE)-ar
NM = $(CROSS_COMPILE)-nm
LD = $(CROSS_COMPILE)-ld
OBJDUMP = $(CROSS_COMPILE)-objdump
OBJCOPY = $(CROSS_COMPILE)-objcopy
RANLIB = $(CROSS_COMPILE)-ranlib
STRIP = $(CROSS_COMPILE)-strip


CFLAGS       = -c -Wall -D_REENTRANT -DACE_HAS_AIO_CALLS -D_GNU_SOURCE -DACE_HAS_EXCEPTIONS -D__ACE_INLINE__  
LDFLAGS      = -L. -L/etc/ACE_wrappers/lib 
CPPFLAGS     += -I/etc/ACE_wrappers -I/etc/ACE_wrappers/ace 
LDLIBS       = -lACE

OUTPUT_DIRECTORY=/root/Documents/bin
OBJ=/root/Documents/obj

ifneq ($(OUTPUT_DIRECTORY),)
all: $(OUTPUT_DIRECTORY)
$(OUTPUT_DIRECTORY):
    -@$(MKDIR) "$(OUTPUT_DIRECTORY)"
endif

ifneq ($(OBJ),)
all: $(OBJ)
$(OBJ_DIR):
    -@$(MKDIR) "$(OBJ)"
endif

SOURCES=File_1.cpp File_2.cpp 

OBJECTS=$(SOURCES:%.cpp=$(OBJ)/%.o)

$(OBJ)/%.o: %.cpp
    @echo Building Objects
    $(CC) $(CFLAGS) $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $<


EXECUTABLE=MyExecutable

all: $(SOURCES) $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS) 
    @echo $(SOURCES)
    $(CC) $(LDFLAGS) $(OBJECTS) $(LDLIBS) -o $(OUTPUT_DIRECTORY)/$@

File_1.o:File_1.cpp
File_1.o:File_1.cpp


.PHONY: clean
clean:
    rm $(OBJECTS) $(OUTPUT_DIRECTORY)/$(EXECUTABLE) 
like image 952
Sirish Avatar asked Jun 22 '10 04:06

Sirish


People also ask

Can I have multiple compilers?

Many open source projects already have a CI pipeline that leverages more than one compiler, configuration, or platform. That is ideal, and you should try to do this for all of your projects if it makes sense for them.

Is G ++ and gcc the same?

DIFFERENCE BETWEEN g++ & gccg++ is used to compile C++ program. gcc is used to compile C program.

What compiler does make use?

Example makefiles c" using the system's C compiler and also provides a "clean" target to remove the generated files if the user desires to start over. The $@ and $< are two of the so-called internal macros (also known as automatic variables) and stand for the target name and "implicit" source, respectively.

Can G ++ compile C code?

G++ is the name of the compiler. (Note: G++ also compiles C++ code, but since C is directly compatible with C++, so we can use it.).


1 Answers

I'd start by putting all the platform-specific defines in a separate makefile. That way you can do:

include $(TARGET).make

Where $(TARGET).make defines CC and other variables for each particular platform. Then you can call make recursively setting TARGET to what you want. Something like:

build:
    $(MAKE) TARGET=platform1 all
    $(MAKE) TARGET=platform2 all

But really there are many, many ways in which you can achieve the same thing.

like image 87
vanza Avatar answered Sep 21 '22 11:09

vanza