基于cygwin构建u-boot(二)gcc的C语言标准版本号(-std=)

时间:2021-01-30 12:24:51

接上文,修改Makefile和config.mk中,.depend相关文件格式后继续编译。

四、tools文件夹的文件编译错误

错误告警如下【多行信息,请使用右侧滚动条查看】:

 gcc -Wall -Wstrict-prototypes -O2 -fomit-frame-pointer -idirafter /u-boot-2010.09/u-boot-2010.09/include 
-idirafter /u-boot-2010.09/u-boot-2010.09/include2 -idirafter /u-boot-2010.09/u-boot-2010.09/include
-I /u-boot-2010.09/u-boot-2010.09/lib/libfdt -I /u-boot-2010.09/u-boot-2010.09/tools
-DTEXT_BASE=0x11080000 -DUSE_HOSTCC -D__KERNEL_STRICT_NAMES -ansi -o default_image.o default_image.c -c
In file included from fdt_host.h::,
from mkimage.h:,
from default_image.c::
../include/libfdt.h::: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘void’
../include/libfdt.h::: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘void’
../include/libfdt.h::: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘void’
../include/libfdt.h::: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘void’
../include/libfdt.h::: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘void’
../include/libfdt.h::: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘void’
../include/libfdt.h::: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘void’
../include/libfdt.h::: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘void’
../include/libfdt.h::: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘void’
../include/libfdt.h::: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘void’
../include/libfdt.h::: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘void’
../include/libfdt.h::: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘struct’
../include/libfdt.h::: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘void’
../include/libfdt.h::: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘int’
../include/libfdt.h::: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘int’
../include/libfdt.h::: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘int’
In file included from default_image.c:::
/u-boot-2010.09/u-boot-2010.09/include/image.h::: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘uint32_t’
/u-boot-2010.09/u-boot-2010.09/include/image.h::: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘uint32_t’
/u-boot-2010.09/u-boot-2010.09/include/image.h::: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘uint32_t’
/u-boot-2010.09/u-boot-2010.09/include/image.h::: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘uint32_t’
/u-boot-2010.09/u-boot-2010.09/include/image.h::: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘uint32_t’
/u-boot-2010.09/u-boot-2010.09/include/image.h::: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘uint32_t’
/u-boot-2010.09/u-boot-2010.09/include/image.h::: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘uint32_t’
/u-boot-2010.09/u-boot-2010.09/include/image.h::: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘uint32_t’
/u-boot-2010.09/u-boot-2010.09/include/image.h::: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘uint8_t’
/u-boot-2010.09/u-boot-2010.09/include/image.h::: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘uint8_t’
/u-boot-2010.09/u-boot-2010.09/include/image.h::: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘uint8_t’
/u-boot-2010.09/u-boot-2010.09/include/image.h::: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘uint8_t’
/u-boot-2010.09/u-boot-2010.09/include/image.h::: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘char’
/u-boot-2010.09/u-boot-2010.09/include/image.h::: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘uint32_t’
/u-boot-2010.09/u-boot-2010.09/include/image.h::: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘ulong’
/u-boot-2010.09/u-boot-2010.09/include/image.h::: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘uint32_t’
/u-boot-2010.09/u-boot-2010.09/include/image.h::: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘ulong’
/u-boot-2010.09/u-boot-2010.09/include/image.h::: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘void’
/u-boot-2010.09/u-boot-2010.09/include/image.h::: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘void’
/u-boot-2010.09/u-boot-2010.09/include/image.h::: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘void’
/u-boot-2010.09/u-boot-2010.09/include/image.h::: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘void’
/u-boot-2010.09/u-boot-2010.09/include/image.h::: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘void’
/u-boot-2010.09/u-boot-2010.09/include/image.h::: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘void’
/u-boot-2010.09/u-boot-2010.09/include/image.h::: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘void’
/u-boot-2010.09/u-boot-2010.09/include/image.h::: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘void’
/u-boot-2010.09/u-boot-2010.09/include/image.h::: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘void’
/u-boot-2010.09/u-boot-2010.09/include/image.h::: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘void’
/u-boot-2010.09/u-boot-2010.09/include/image.h::: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘void’
/u-boot-2010.09/u-boot-2010.09/include/image.h::: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘void’
/u-boot-2010.09/u-boot-2010.09/include/image.h::: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘int’
/u-boot-2010.09/u-boot-2010.09/include/image.h::: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘int’
/u-boot-2010.09/u-boot-2010.09/include/image.h::: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘int’
/u-boot-2010.09/u-boot-2010.09/include/image.h::: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘int’
/u-boot-2010.09/u-boot-2010.09/include/image.h::: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘int’
/u-boot-2010.09/u-boot-2010.09/include/image.h::: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘int’
/u-boot-2010.09/u-boot-2010.09/include/image.h::: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘ulong’
/u-boot-2010.09/u-boot-2010.09/include/image.h::: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘ulong’
/u-boot-2010.09/u-boot-2010.09/include/image.h::: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘const’
default_image.c: 在函数‘image_set_header’中:
default_image.c::: 警告:隐式声明函数‘image_set_magic’
default_image.c::: 警告:隐式声明函数‘image_set_time’
default_image.c::: 警告:隐式声明函数‘image_set_size’
default_image.c::: 警告:隐式声明函数‘image_set_load’
default_image.c::: 警告:隐式声明函数‘image_set_ep’
default_image.c::: 警告:隐式声明函数‘image_set_dcrc’
default_image.c::: 警告:隐式声明函数‘image_set_os’
default_image.c::: 警告:隐式声明函数‘image_set_arch’
default_image.c::: 警告:隐式声明函数‘image_set_type’
default_image.c::: 警告:隐式声明函数‘image_set_comp’
default_image.c::: 警告:隐式声明函数‘image_set_name’
default_image.c::: 警告:隐式声明函数‘image_set_hcrc’
/u-boot-2010.09/u-boot-2010.09/rules.mk:: recipe for target `default_image.o' failed
make[]: *** [default_image.o] Error
make[]: 离开目录“/u-boot-2010.09/u-boot-2010.09/tools”
Makefile:: recipe for target `tools' failed
make: *** [tools] Error

1、首先,需要了解gcc的错误告警信息。

1)错误位置:文件名:行号:列号。【注意:gcc工具的告警信息不是很友好,显示的错误位置可能不是真实的位置】

2)引用关系:错误提示中,第2~4行,表示头文件的引用关系(一层一层的引用)。

default_image.c 第30行 -- mkimage.h 第35行 -- fdt_host.h第25行-- libfdt.h

3)主要的错误类型: 错误:expected '=', ',', ';', 'asm' or '__attribute__' before

初步搜索,发现该问题和 头文件中 inline 关键字相关。

 GCC implements three different semantics of declaring a function inline. One is available with ‘-std=gnu89’ or ‘-fgnu89-inline’ or when gnu_inline attribute is present on all inline declarations, another when ‘-std=c99’ or ‘-std=gnu99’ (without ‘-fgnu89-inline’), and the third is used when compiling C++.
To declare a function inline, use the inline keyword in its declaration, like this:
static inline int inc (int *a)
{
(*a)++;
}
If you are writing a header file to be included in ISO C89 programs, write __inline__ instead of inline

2、从错误信息可以看到,gcc包含了参数 -ansi。

而 -ansi,对于gcc工具就等于 -std=c89。

在libfdt.h文件中,第162行如下

static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen)

告警信息为第15列,void是错误的信息,gcc认为缺少 ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’

3、找到-ansi的位置并修改。

U-boot 编译工具相关的配置,都在主文件夹 config.mk文件中,

 ifeq ($(HOSTOS),cygwin)
HOSTCFLAGS += -ansi
endif

从配置中可以,u-boot认为在cygwin下,需要增加-ansi的编译选型。

但实际,由于u-boot-2010.09文件是根据C99规范编写的,应该删除该信息。

4、gcc工具编译错误的问题得到解决。

 gcc -Wall -Wstrict-prototypes -O2 -fomit-frame-pointer  -idirafter /u-boot-2010.09/u-boot-2010.09/include  
-idirafter /u-boot-2010.09/u-boot-2010.09/include2 -idirafter /u-boot-2010.09/u-boot-2010.09/include
-I /u-boot-2010.09/u-boot-2010.09/lib/libfdt -I /u-boot-2010.09/u-boot-2010.09/tools
-DTEXT_BASE=0x11080000 -DUSE_HOSTCC -D__KERNEL_STRICT_NAMES -pedantic -o img2srec.o img2srec.c -c
gcc -Wall -Wstrict-prototypes -O2 -fomit-frame-pointer -idirafter /u-boot-2010.09/u-boot-2010.09/include
-idirafter /u-boot-2010.09/u-boot-2010.09/include2 -idirafter /u-boot-2010.09/u-boot-2010.09/include
-I /u-boot-2010.09/u-boot-2010.09/lib/libfdt -I /u-boot-2010.09/u-boot-2010.09/tools
-DTEXT_BASE=0x11080000 -DUSE_HOSTCC -D__KERNEL_STRICT_NAMES -pedantic -o img2srec img2srec.o
strip img2srec

可以看到正常生成了 img2srec.o

5、后记

C语言标准比较多,gcc支持的也比较多,相关关键字有:

gcc-4.5支持的C语言标准
1 -ansi In C mode, this is equivalent to ‘-std=c89’. In C++ mode, it is equivalent to ‘-std=c++’.
-std= Determine the language standard. A value for this option must be provided; possible values are
‘c89’
‘iso9899:’
Support all ISO C90 programs (certain GNU extensions that conflict
with ISO C90 are disabled). Same as ‘-ansi’ for C code.
‘iso9899:’
ISO C90 as modified in amendment .
    ‘c99’
    ‘c9x’
11     ‘iso9899:’
12     ‘iso9899:199x’
ISO C99. Note that this standard is not yet fully supported;
see http://gcc.gnu.org/gcc-4.3/c99status.html for more information.
The names ‘c9x’ and ‘iso9899:199x’ are deprecated.
‘gnu89’ GNU dialect of ISO C90 (including some C99 features). This is the default for C code.
17 ‘gnu99’
‘gnu9x’ GNU dialect of ISO C99. When ISO C99 is fully implemented in GCC, this will become the default.
The name ‘gnu9x’ is deprecated.
‘c++’ The ISO C++ standard plus amendments. Same as ‘-ansi’ for C++ code.
‘gnu++’ GNU dialect of ‘-std=c++’. This is the default for C++ code.
‘c++0x’ The working draft of the upcoming ISO C++0x standard. This option enables experimental features
that are likely to be included in C++0x. The working draft is constantly changing,
and any feature that is enabled by this flag may be removed from future versions of GCC
if it is not part of the C++0x standard.
‘gnu++0x’ GNU dialect of ‘-std=c++0x’. This option enables experimental features
that may be removed in future versions of GCC.

===【未完待续】===