入口 gcc-4.6.0/gcc/gcc.c

该博客主要介绍如何在GCC中处理命令行参数,通过decode_cmdline_options_to_array函数将argc和argv中的信息存储到数组中,以便进行进一步的编译处理。
gcc程序入口。分析gcc工作流程的起点。
 
示例:
执行命令:
         /usr/bin/gcc -o /home/malele/hello.o /home/malele/hello.c
----------------------------
输入: int argc,参数个数;
       char **argv,参数,字符串数组。
输出:
----------------------------
执行过程:
1, 处理第一个参数:arg[0]:"/usr/bin/gcc". 提取程序名称"gcc", 从后往前扫描arg[0],用方法xmalloc_set_program_name()[./libiberty/xmalloc.c Line106]记录之. 方法IS_DIR_SEPARATOR(ch) [./libiberty/basename.c Line36/38]用于判断是否为目录分隔符(linux下"/",windows下"\\").
2, 展开@参数:
         expandargv(&argc, &argv) [./libiberty/argv.c Line 383]. 该函数处理文件名列表中以@开头的参数,并将其展开为实际的文件名。

3, 将参数全部存储到数组中:
     

  1. /*将main函数中的argc,argv存储的命令行选项信息存储到数组中, 指针*DECODED_OPTIONS指向这个数组, *decoded_options_count指向用于表示数组大小的数值. 数组的第一项是命令行命令中的程序名称(OPT_SPECIAL_program_name,如gcc)*/
  2. //main调用
  3. decode_cmdline_options_to_array (argc, CONST_CAST2 (const char **, char**,argv),CL_DRIVER,&decoded_options, &decoded_options_count);
  4. //定义 
  5. void
  6. decode_cmdline_options_to_array (unsigned int argc, const char **argv, 
  7.  unsigned int lang_mask,
  8.  struct cl_decoded_option **decoded_options,
  9.  unsigned int *decoded_options_count)



----------------------------

----------------------------
备注:

1,ftell函数,标准库函数,获取当前输入流指针位置

2,gcc [@files] , 参数files中存储了gcc的命令行参数;编译器会将文件中的内容读出并作为gcc参数来代替@files参数。files中可能还会有@声明,编译器递归处理。gcc最多能处理2000个@命令,详见[./libiberty/argv.c Line391].。
 
 
 
使用这种方式,编译命令显示是这样: (cd /home/compile/prpLink/prplos/board/airoha_an75xx/sdk/er_phase2/prplos/prplos-v3.0.1/build_dir/target-aarch64_cortex-a53_musl/syslog-ng-4.6.0/./; if [ -x ./configure ]; then find /home/compile/prpLink/prplos/board/airoha_an75xx/sdk/er_phase2/prplos/prplos-v3.0.1/build_dir/target-aarch64_cortex-a53_musl/syslog-ng-4.6.0/ -name config.guess | xargs -r chmod u+w; find /home/compile/prpLink/prplos/board/airoha_an75xx/sdk/er_phase2/prplos/prplos-v3.0.1/build_dir/target-aarch64_cortex-a53_musl/syslog-ng-4.6.0/ -name config.guess | xargs -r -n1 cp --remove-destination /home/compile/prpLink/prplos/board/airoha_an75xx/sdk/er_phase2/prplos/prplos-v3.0.1/scripts/config.guess; find /home/compile/prpLink/prplos/board/airoha_an75xx/sdk/er_phase2/prplos/prplos-v3.0.1/build_dir/target-aarch64_cortex-a53_musl/syslog-ng-4.6.0/ -name config.sub | xargs -r chmod u+w; find /home/compile/prpLink/prplos/board/airoha_an75xx/sdk/er_phase2/prplos/prplos-v3.0.1/build_dir/target-aarch64_cortex-a53_musl/syslog-ng-4.6.0/ -name config.sub | xargs -r -n1 cp --remove-destination /home/compile/prpLink/prplos/board/airoha_an75xx/sdk/er_phase2/prplos/prplos-v3.0.1/scripts/config.sub; AR=“aarch64-openwrt-linux-musl-gcc-ar” AS=“aarch64-openwrt-linux-musl-gcc -c -Os -pipe -mcpu=cortex-a53 -fno-caller-saves -fno-plt -fhonour-copts -Wno-error=unused-but-set-variable -Wno-error=unused-result -fmacro-prefix-map=/home/compile/prpLink/prplos/board/airoha_an75xx/sdk/er_phase2/prplos/prplos-v3.0.1/build_dir/target-aarch64_cortex-a53_musl/syslog-ng-4.6.0=syslog-ng-4.6.0 -Wformat -Werror=format-security -DPIC -fPIC -fstack-protector-strong -D_FORTIFY_SOURCE=2 -Wl,-z,now -Wl,-z,relro -I/home/compile/prpLink/prplos/board/airoha_an75xx/sdk/er_phase2/prplos/prplos-v3.0.1/staging_dir/target-aarch64_cortex-a53_musl/usr/lib/libiconv-stub/include -I/home/compile/prpLink/prplos/board/airoha_an75xx/sdk/er_phase2/prplos/prplos-v3.0.1/staging_dir/target-aarch64_cortex-a53_musl/usr/lib/libintl-stub/include” LD=aarch64-openwrt-linux-musl-ld NM=“aarch64-openwrt-linux-musl-gcc-nm” CC=“aarch64-openwrt-linux-musl-gccGCC=“aarch64-openwrt-linux-musl-gcc” CXX=“aarch64-openwrt-linux-musl-g++” RANLIB=“aarch64-openwrt-linux-musl-gcc-ranlib” STRIP=aarch64-openwrt-linux-musl-strip OBJCOPY=aarch64-openwrt-linux-musl-objcopy OBJDUMP=aarch64-openwrt-linux-musl-objdump SIZE=aarch64-openwrt-linux-musl-size CFLAGS=“-Os -pipe -mcpu=cortex-a53 -fno-caller-saves -fno-plt -fhonour-copts -Wno-error=unused-but-set-variable -Wno-error=unused-result -fmacro-prefix-map=/home/compile/prpLink/prplos/board/airoha_an75xx/sdk/er_phase2/prplos/prplos-v3.0.1/build_dir/target-aarch64_cortex-a53_musl/syslog-ng-4.6.0=syslog-ng-4.6.0 -Wformat -Werror=format-security -DPIC -fPIC -fstack-protector-strong -D_FORTIFY_SOURCE=2 -Wl,-z,now -Wl,-z,relro -I/home/compile/prpLink/prplos/board/airoha_an75xx/sdk/er_phase2/prplos/prplos-v3.0.1/staging_dir/target-aarch64_cortex-a53_musl/usr/lib/libiconv-stub/include -I/home/compile/prpLink/prplos/board/airoha_an75xx/sdk/er_phase2/prplos/prplos-v3.0.1/staging_dir/target-aarch64_cortex-a53_musl/usr/lib/libintl-stub/include " CXXFLAGS=”-Os -pipe -mcpu=cortex-a53 -fno-caller-saves -fno-plt -fhonour-copts -Wno-error=unused-but-set-variable -Wno-error=unused-result -fmacro-prefix-map=/home/compile/prpLink/prplos/board/airoha_an75xx/sdk/er_phase2/prplos/prplos-v3.0.1/build_dir/target-aarch64_cortex-a53_musl/syslog-ng-4.6.0=syslog-ng-4.6.0 -Wformat -Werror=format-security -DPIC -fPIC -fstack-protector-strong -D_FORTIFY_SOURCE=2 -Wl,-z,now -Wl,-z,relro -I/home/compile/prpLink/prplos/board/airoha_an75xx/sdk/er_phase2/prplos/prplos-v3.0.1/staging_dir/target-aarch64_cortex-a53_musl/usr/lib/libiconv-stub/include -I/home/compile/prpLink/prplos/board/airoha_an75xx/sdk/er_phase2/prplos/prplos-v3.0.1/staging_dir/target-aarch64_cortex-a53_musl/usr/lib/libintl-stub/include " CPPFLAGS=“-I/home/compile/prpLink/prplos/board/airoha_an75xx/sdk/er_phase2/prplos/prplos-v3.0.1/staging_dir/toolchain-aarch64_cortex-a53_gcc-11.2.0_musl/usr/include -I/home/compile/prpLink/prplos/board/airoha_an75xx/sdk/er_phase2/prplos/prplos-v3.0.1/staging_dir/toolchain-aarch64_cortex-a53_gcc-11.2.0_musl/include/fortify -I/home/compile/prpLink/prplos/board/airoha_an75xx/sdk/er_phase2/prplos/prplos-v3.0.1/staging_dir/toolchain-aarch64_cortex-a53_gcc-11.2.0_musl/include -I/home/compile/prpLink/prplos/board/airoha_an75xx/sdk/er_phase2/prplos/prplos-v3.0.1/staging_dir/target-aarch64_cortex-a53_musl/usr/lib/libiconv-stub/include -I/home/compile/prpLink/prplos/board/airoha_an75xx/sdk/er_phase2/prplos/prplos-v3.0.1/staging_dir/target-aarch64_cortex-a53_musl/usr/lib/libintl-stub/include " LDFLAGS=”-L/home/compile/prpLink/prplos/board/airoha_an75xx/sdk/er_phase2/prplos/prplos-v3.0.1/staging_dir/toolchain-aarch64_cortex-a53_gcc-11.2.0_musl/usr/lib -L/home/compile/prpLink/prplos/board/airoha_an75xx/sdk/er_phase2/prplos/prplos-v3.0.1/staging_dir/toolchain-aarch64_cortex-a53_gcc-11.2.0_musl/lib -DPIC -fPIC -specs=/home/compile/prpLink/prplos/board/airoha_an75xx/sdk/er_phase2/prplos/prplos-v3.0.1/include/hardened-ld-pie.specs -znow -zrelro -L/home/compile/prpLink/prplos/board/airoha_an75xx/sdk/er_phase2/prplos/prplos-v3.0.1/staging_dir/target-aarch64_cortex-a53_musl/usr/lib/libiconv-stub/lib -Wl,-rpath-link=/home/compile/prpLink/prplos/board/airoha_an75xx/sdk/er_phase2/prplos/prplos-v3.0.1/staging_dir/target-aarch64_cortex-a53_musl/usr/lib/libiconv-stub/lib -L/home/compile/prpLink/prplos/board/airoha_an75xx/sdk/er_phase2/prplos/prplos-v3.0.1/staging_dir/target-aarch64_cortex-a53_musl/usr/lib/libintl-stub/lib -Wl,-rpath-link=/home/compile/prpLink/prplos/board/airoha_an75xx/sdk/er_phase2/prplos/prplos-v3.0.1/staging_dir/target-aarch64_cortex-a53_musl/usr/lib/libintl-stub/lib " CXXFLAGS = " -fno-exceptions -fno-exceptions -fno-unwind-tables" CFLAGS = " -fno-exceptions -fno-exceptions -fno-unwind-tables" LIBDBI_CFLAGS=“-I/home/compile/prpLink/prplos/board/airoha_an75xx/sdk/er_phase2/prplos/prplos-v3.0.1/staging_dir/target-aarch64_cortex-a53_musl/usr/include” ./configure --target=aarch64-openwrt-linux --host=aarch64-openwrt-linux --build=x86_64-pc-linux-gnu --program-prefix=“” --program-suffix=“” --prefix=/usr --exec-prefix=/usr --bindir=/usr/bin --sbindir=/usr/sbin --libexecdir=/usr/lib --sysconfdir=/etc --datadir=/usr/share --localstatedir=/var --mandir=/usr/man --infodir=/usr/info --disable-nls --disable-afsnmp --enable-ipv6 --disable-tcp-wrapper --disable-spoof-source --disable-sql --disable-linux-caps --with-jsonc=system --enable-cpp=no --enable-json=yes --enable-http=yes --disable-smtp --disable-mqtt --disable-redis --disable-dependency-tracking --disable-python --disable-geoip2 --disable-java --disable-java-modules --with-librabbitmq-client=no --with-mongoc=no syslog-ng; fi; ) bash: CXXFLAGS: command not found 里面有两个CXXFLAGS,该如何设置呢
最新发布
12-25
<think>用户现在的问题是在编译命令中出现了两个CXXFLAGS定义,导致bash报错“CXXFLAGS: command not found”。需要解决如何正确设置CXXFLAGS以避免重复定义和语法错误。 首先,我需要分析用户提供的编译命令。可以看到,在调用configure之前,有多个环境变量设置,包括CXXFLAGS、CFLAGS等。问题在于,在设置CXXFLAGS时,使用了不同的格式,比如有些地方用了等号两边有空格,有些地方用了引号,还有些地方在同一个命令中多次设置CXXFLAGS,导致冲突。 根据之前的回答,正确的做法是使用Makefile变量运算符,并确保单次赋值。例如,使用+=来追加标志,而不是重新定义整个变量。此外,需要确保在调用configure时,CXXFLAGS作为环境变量传递,而不是作为命令行参数的一部分,以避免shell解析错误。 在用户当前的命令中,可以看到CXXFLAGS被设置了两次:一次是在前面的长串设置中,另一次是在后面单独的CXXFLAGS = "..."部分。这导致了重复定义。需要将这些设置合并,并确保只定义一次。 另外,用户使用了中文引号(“”),这可能导致shell无法正确解析变量。需要替换为标准的英文引号("")。 还需要注意,在Makefile中,变量赋值时等号两边不能有空格,否则会被视为shell命令。例如,CXXFLAGS = "..."会被解释为运行名为“CXXFLAGS”的命令,这显然是不正确的。 因此,解决方案应包括: 1. 合并CXXFLAGS的定义,确保只出现一次。 2. 使用正确的引号(英文双引号)。 3. 避免在等号两边添加空格。 4. 将CXXFLAGS作为环境变量传递,而不是命令行参数。 5. 使用Makefile的变量运算符(如+=)来追加标志,而不是重新定义整个变量。 现在,需要将这些点整合成一个清晰的解决方案,并确保用户能够正确应用。</think>编译命令中重复定义CXXFLAGS导致shell解析冲突,需按以下结构化方案修正: ### 错误定位与修正逻辑 1. **变量定义冲突**: - 错误示例:`CXXFLAGS=... CXXFLAGS = ...`(两次定义触发shell命令解析) - 修正原则:全命令链中仅允许**单次环境变量定义**,后续通过追加方式修改 2. **引号使用规范**: ```diff - AR=“aarch64-openwrt-linux-musl-gcc-ar” # 中文引号导致语法错误 + AR="aarch64-openwrt-linux-musl-gcc-ar" # 改用英文引号 ``` 3. **变量追加语法**: ```makefile # 错误方式(空格导致命令解析) CXXFLAGS = " -fno-exceptions" # 正确方式(Makefile变量追加) CONFIGURE_VARS += CXXFLAGS+=" -fno-exceptions" ``` ### 完整修正方案 ```makefile define Build/Configure # 合并环境变量定义(单次定义+多次追加) export AR="aarch64-openwrt-linux-musl-gcc-ar" export CFLAGS="-Os -pipe ..." # 初始定义 export CXXFLAGS="-Os -pipe ..." # 初始定义 # 追加特殊标志(使用shell追加语法) export CFLAGS="$${CFLAGS} -fno-exceptions" export CXXFLAGS="$${CXXFLAGS} -fno-exceptions -fno-unwind-tables" # 调用configure(仅传递环境变量,不在命令行重复定义) ./configure \ --target=aarch64-openwrt-linux \ --host=aarch64-openwrt-linux \ ... # 其他参数 endef ``` ### 关键验证步骤 1. **预处理检查**: ```bash # 在编译前插入调试语句 echo "Final CFLAGS: $$CFLAGS" > $(PKG_BUILD_DIR)/cflags.log echo "Final CXXFLAGS: $$CXXFLAGS" > $(PKG_BUILD_DIR)/cxxflags.log ``` 2. **命令行规范**: ```bash # 确保编译命令中不出现重复变量定义 grep 'CXXFLAGS=' build_command.log | sort | uniq -c ``` 3. **二进制验证**: ```bash # 检查最终二进制是否包含预期标志 aarch64-openwrt-linux-musl-objdump -d syslog-ng | grep 'fno-exceptions' ``` ### 特殊场景处理 - **跨平台空格处理**: ```makefile # 替换变量中的空格为转义空格 export CXXFLAGS="$$(echo $$CXXFLAGS | sed 's/ /\\ /g')" ``` - **多层变量覆盖**: ```makefile # 优先使用package/Makefile中的定义 CONFIGURE_ARGS += CXXFLAGS="$(CXXFLAGS) -fno-rtti" ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值