一、概念
gcc的-D選項可以定義宏,這是什么意思呢?
C語言源程序中有#define定義的宏,可以起到替換、條件編譯的功能;定義宏的方式是放在頭文件或者C文件中。gcc提供了另外一種宏定義的方法,當然可以想象這樣的宏定義方法並不是很好用,但畢竟編譯器給我們提供了額外的宏定義的方法。假設程序需要很多宏,不可能這些宏都在編譯器中定義,可以說比較重要的宏才會放在gcc的D選項后邊。
-Dname 定義宏name,默認定義內容為字符串“1” -Dname=defn 定義宏name,並且內容為defn
二、實驗
測試程序:
#include <stdio.h> int main() { #ifdef HELLO printf("HELLO defined !\n"); printf("HELLO = %d\n",HELLO);
#else printf("HELLO not define!\n"); #endif return 0; }
1.執行編譯命令:
#gcc main.c -o main
main的執行結果 :
HELLO not define!
2.執行編譯命令:(條件編譯測試)
#gcc -DHELLO main.c -o main
main的執行結果 :
HELLO defined!
HELLO = 1
3.執行編譯命令:(宏替換測試)
#gcc -DHELLO=36 main.c -o main
main的執行結果 :
HELLO defined!
HELLO = 36
三、u-boot上的應用
u-boot編譯cpu/start.S的過程如下框中內容所示,紅色部分就是gcc的-D選項應用
arm-linux-gcc -D__ASSEMBLY__ -g -Os -fno-strict-aliasing -fno-common -ffixed-r8 -msoft-float -malignment-traps -D__KERNEL__ -DTEXT_BASE=0x33F80000 -I/share/u-boot-1.1.6/include -fno-builtin -ffreestanding -nostdinc -isystem /opt/EmbedSky/crosstools_3.4.5_softfloat/bin/../lib/gcc/arm-linux/3.4.5/include -pipe -DCONFIG_ARM -D__ARM__ -march=armv4 -mapcs-32 -c -o start.o start.S
參考博客:gcc -D:gcc的預定義功能