Overriding `CC` and `CXX` variables in makefiles

馋奶兔 提交于 2019-12-06 05:48:45

Split your master makefile into two files: master.macros and master.targets. The .macros file will contain any macros such as CC and CXX, and the .targets file will contain the actual targets to make.

Child makefile:

CC ?= color-avr-gcc
CXX ?= color-avr-g++
# Add other child macros here.

include master.macros

# Add child targets here.

include master.targets

master.macros:

CC = avr-gcc
CXX = avr-g++
# Add other master macros here.

master.targets:

# Add master targets here.

If you set CC on the command line, the entire project will use that. Otherwise if CC is set in the child makefile, the entire project will use that CC. If neither is used, the entire project will use the CC macro in master.macros.

If you need anything more complicated, such as a different CC being used in building master targets only, you will want to use a different CC variable such as MASTER_CC that defaults to $(CC), though you can override it as needed by using a command line like make MASTER_CC=avr-gcc if you don't want to use whatever CC is in the child makefile. You'd use the ?= assignment, and all rules would need to be explicit and you would substitute any $(CC) in a rule for $(MASTER_CC) of course:

master.macros:

MASTER_CC ?= $(CC)
MASTER_CXX ?= $(CXX)

It will use color-avr-gcc for example if that is the value of CC. Otherwise, you'd need to use make MASTER_CC=avr-gcc to use avr-gcc instead. I haven't tested this last bit, meaning there are probably bugs, but I'd imagine the first solution is what you need: split the master makefile into two files, and use CC ?= ... in the part containing only master macros and the child makefile.

After debugging it using the origin function, I finally made to work with the following combination.

Master makefile

CC = avr-gcc
CXX = avr-g++
# Add other master macros here.
# Add other master targets here.

Child makefile

include master.mk

CC = color-avr-gcc
CXX = color-avr-g++
# There are no child macros or targets

Now when I do make child.mk it picks up color-avr-gcc. And if I comment it in child makefile, then it uses avr-gcc from master makefile.

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