One makefile for two compilers

拈花ヽ惹草 提交于 2019-12-05 01:08:40

You can assign/append variables for specific targets by using the syntax target:assignment on a line. Here is an example:

native: CC=cc
native:
    echo $(CC)

cross: CC=arm-linux-gnueabihf-g++
cross:
    echo $(CC)

calling

make native

(or just make, here) prints

echo cc
cc

and calling

make cross

prints

echo arm-linux-gnueabihf-g++
arm-linux-gnueabihf-g++

So you can use your usual compilation line with $(CC)

Anon

You can pass parameters to make.
e.g. make TARGET=native and make TARGET=cross then use this

ifeq ($(TARGET),cross)
        CC = arm-linux-gnueabihf-g++
else
        CC = g++
endif

not exactly what you wanted but you can read CC as an environment variable. consider the following Makefile:

all:
        echo $(CC)

and you can call it with CC=g++ make which gives you:

echo g++
g++

or call it with CC=arm-linux-gnueabihf-g++ make which gives you:

echo arm-linux-gnueabihf-g++
arm-linux-gnueabihf-g++

and the best part is you can put these in your ~/.bashrc as export CC=g++ and export CC=arm-linux-gnueabihf-g++ respectively and do the calling with only make.

Another way to do it which is more portable than the gnu make ifeq way is this one:

CROSS contains either arm-linux-gnueabihf- or is empty.

CC=$(CROSS)g++

CC will contain the expansion result.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!