解决:invalid application of `sizeof' to incomplete type `char[] '错误

本文详细解析了C语言中使用sizeof运算符与extern关键字的常见错误,特别是在数组大小计算上的误区。通过实例说明了sizeof不能应用于extern声明的变量,并提供了两种解决方案:将相关函数与数组定义放在同一文件中,或直接在文件中定义数组大小。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文:https://blog.youkuaiyun.com/he__yuan/article/details/79731923

//a.c文件
#include "a.h"
 char array[]={0XED,0X34,0X40,0X34};
//a.h文件
#ifndef _A_H
#define _A_H
#define size   (sizeof(array)/sizeof(array[0]))
//main.c
#include "a.h"
extern char zimo1[];
u16 tmp;
char *p;
 int main(void)
 {  
     int  i;
    while(1)
    {
        p=array;
        for(i=0;i<size;i++)//error:invalid application of `sizeof' to incomplete type `char[] '
                            ------------------------------------------------------------------

        {   
        }
    } 
}

这个错误的原因:

sizeof不能用在extern变量,
sizeof 的计算发生在代码编译 的时刻。。
extern 标注的符号 在链接的时刻解析。。。
所以 sizeof 不知道 这个符号到底占用了多少空间。
**

就我这个例子的解决办法:
①:把用到for(i=0;i< size; i+ +)的函数和定义数组的文件写到一块写到
②:不要用宏定义#define size (sizeof(array)/sizeof(array[0]))了,直接在a.c文件里定义 size=(sizeof(array)/sizeof(array[0]));
然后再main.c里用
extern int zimo1_size;
extern char zimo1[];

loginutils/getty.c: In function ‘open_tty’: loginutils/getty.c:224:17: warning: ignoring return value of ‘fchown’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 224 | fchown(0, 0, 0); /* 0:0 */ | ^~~~~~~~~~~~~~~ CC loginutils/login.o loginutils/login.c: In function ‘login_main’: loginutils/login.c:558:9: warning: ignoring return value of ‘fchown’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 558 | fchown(0, pw->pw_uid, pw->pw_gid); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ CC loginutils/passwd.o CC loginutils/su.o CC loginutils/sulogin.o loginutils/sulogin.c: In function ‘sulogin_main’: loginutils/sulogin.c:55:17: warning: ignoring return value of ‘dup’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 55 | dup(xopen(argv[0], O_RDWR)); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~ loginutils/sulogin.c:57:17: warning: ignoring return value of ‘dup’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 57 | dup(0); | ^~~~~~ CC loginutils/vlock.o AR loginutils/lib.a LD mailutils/built-in.o CC mailutils/mail.o CC mailutils/makemime.o mailutils/makemime.c: In function ‘makemime_main’: mailutils/makemime.c:204:17: warning: ignoring return value of ‘freopen’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 204 | freopen(opt_output, "w", stdout); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ CC mailutils/popmaildir.o CC mailutils/reformime.o CC mailutils/sendmail.o AR mailutils/lib.a LD miscutils/built-in.o CC miscutils/adjtimex.o CC miscutils/ascii.o CC miscutils/bc.o CC miscutils/beep.o CC miscutils/chat.o CC miscutils/conspy.o CC miscutils/crond.o CC miscutils/crontab.o miscutils/crontab.c: In function ‘crontab_main’: miscutils/crontab.c:161:17: warning: ignoring return value of ‘fchown’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 161 | fchown(src_fd, pas->pw_uid, pas->pw_gid); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ CC miscutils/devmem.o CC miscutils/fbsplash.o CC miscutils/hdparm.o CC miscutils/hexedit.o CC miscutils/i2c_tools.o miscutils/i2c_tools.c: In function ‘list_i2c_busses_and_exit’: miscutils/i2c_tools.c:1152:46: warning: ‘%s’ directive output may be truncated writing up to 255 bytes into a region of size 236 [-Wformat-truncation=] 1152 | snprintf(path, NAME_MAX, "%s/%s/name", | ^~ In file included from /usr/arm-linux-gnueabi/include/stdio.h:980, from include/libbb.h:31, from miscutils/i2c_tools.c:66: In function ‘snprintf’, inlined from ‘list_i2c_busses_and_exit’ at miscutils/i2c_tools.c:1152:3: /usr/arm-linux-gnueabi/include/bits/stdio2.h:54:10: note: ‘__builtin___snprintf_chk’ output between 25 and 280 bytes into a destination of size 255 54 | return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1, | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 55 | __glibc_objsize (__s), __fmt, | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 56 | __va_arg_pack ()); | ~~~~~~~~~~~~~~~~~ miscutils/i2c_tools.c: In function ‘list_i2c_busses_and_exit’: miscutils/i2c_tools.c:1157:38: warning: ‘%s’ directive output may be truncated writing up to 255 bytes into a region of size 236 [-Wformat-truncation=] 1157 | "%s/%s/device/name", | ^~ In function ‘snprintf’, inlined from ‘list_i2c_busses_and_exit’ at miscutils/i2c_tools.c:1156:4: /usr/arm-linux-gnueabi/include/bits/stdio2.h:54:10: note: ‘__builtin___snprintf_chk’ output between 32 and 287 bytes into a destination of size 255 54 | return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1, | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 55 | __glibc_objsize (__s), __fmt, | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 56 | __va_arg_pack ()); | ~~~~~~~~~~~~~~~~~ miscutils/i2c_tools.c: In function ‘list_i2c_busses_and_exit’: miscutils/i2c_tools.c:1165:38: warning: ‘%s’ directive output may be truncated writing up to 255 bytes into a region of size 236 [-Wformat-truncation=] 1165 | "%s/%s/device/name", | ^~ In function ‘snprintf’, inlined from ‘list_i2c_busses_and_exit’ at miscutils/i2c_tools.c:1164:4: /usr/arm-linux-gnueabi/include/bits/stdio2.h:54:10: note: ‘__builtin___snprintf_chk’ output between 32 and 287 bytes into a destination of size 255 54 | return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1, | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 55 | __glibc_objsize (__s), __fmt, | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 56 | __va_arg_pack ()); | ~~~~~~~~~~~~~~~~~ miscutils/i2c_tools.c: In function ‘list_i2c_busses_and_exit’: miscutils/i2c_tools.c:1177:54: warning: ‘%s’ directive output may be truncated writing up to 255 bytes into a region of size 236 [-Wformat-truncation=] 1177 | "%s/%s/device/%s/name", | ^~ In function ‘snprintf’, inlined from ‘list_i2c_busses_and_exit’ at miscutils/i2c_tools.c:1176:6: /usr/arm-linux-gnueabi/include/bits/stdio2.h:54:10: note: ‘__builtin___snprintf_chk’ output between 33 and 543 bytes into a destination of size 255 54 | return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1, | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 55 | __glibc_objsize (__s), __fmt, | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 56 | __va_arg_pack ()); | ~~~~~~~~~~~~~~~~~ CC miscutils/less.o CC miscutils/lsscsi.o CC miscutils/makedevs.o CC miscutils/man.o miscutils/man.c: In function ‘run_pipe’: miscutils/man.c:160:9: warning: ignoring return value of ‘system’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 160 | system(cmd); | ^~~~~~~~~~~ CC miscutils/microcom.o miscutils/microcom.c: In function ‘microcom_main’: miscutils/microcom.c:160:25: warning: ignoring return value of ‘write’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 160 | write(sfd, &c, 1); | ^~~~~~~~~~~~~~~~~ CC miscutils/mt.o CC miscutils/nandwrite.o CC miscutils/partprobe.o CC miscutils/raidautorun.o CC miscutils/readahead.o CC miscutils/runlevel.o CC miscutils/rx.o CC miscutils/seedrng.o CC miscutils/setfattr.o CC miscutils/setserial.o CC miscutils/strings.o CC miscutils/time.o CC miscutils/tree.o CC miscutils/ts.o CC miscutils/ttysize.o CC miscutils/ubi_tools.o CC miscutils/ubirename.o CC miscutils/volname.o CC miscutils/watchdog.o miscutils/watchdog.c: In function ‘watchdog_main’: miscutils/watchdog.c:161:17: warning: ignoring return value of ‘write’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 161 | write(3, "", 1); /* write zero byte */ | ^~~~~~~~~~~~~~~ miscutils/watchdog.c: In function ‘shutdown_watchdog’: miscutils/watchdog.c:71:9: warning: ignoring return value of ‘write’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 71 | write(3, &V, 1); /* Magic, see watchdog-api.txt in kernel */ | ^~~~~~~~~~~~~~~ AR miscutils/lib.a LD modutils/built-in.o CC modutils/modinfo.o CC modutils/modprobe-small.o CC modutils/modutils.o modutils/modutils.c: In function ‘filename2modname’: modutils/modutils.c:115:1: warning: function may return address of local variable [-Wreturn-local-addr] 115 | } | ^ modutils/modutils.c:94:14: note: declared here 94 | char local_modname[MODULE_NAME_LEN]; | ^~~~~~~~~~~~~ AR modutils/lib.a LD networking/built-in.o CC networking/arp.o In file included from networking/arp.c:43: networking/arp.c: In function ‘arp_show’: include/libbb.h:236:28: warning: ignoring return value of ‘fgets_unlocked’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 236 | # define fgets(s,n,stream) fgets_unlocked(s,n,stream) | ^~~~~~~~~~~~~~~~~~~~~~~~~~ networking/arp.c:430:9: note: in expansion of macro ‘fgets’ 430 | fgets(line, sizeof(line), fp); | ^~~~~ CC networking/arping.o CC networking/brctl.o CC networking/dnsd.o CC networking/ether-wake.o CC networking/ftpd.o networking/ftpd.c: In function ‘popen_ls’: networking/ftpd.c:718:17: warning: ignoring return value of ‘dup’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 718 | dup(STDOUT_FILENO); /* copy will become STDIN_FILENO */ | ^~~~~~~~~~~~~~~~~~ CC networking/ftpgetput.o CC networking/hostname.o CC networking/httpd.o CC networking/ifconfig.o CC networking/ifenslave.o CC networking/ifplugd.o CC networking/ifupdown.o CC networking/inetd.o CC networking/interface.o In file included from networking/interface.c:33: networking/interface.c: In function ‘if_readlist_proc’: include/libbb.h:236:28: warning: ignoring return value of ‘fgets_unlocked’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 236 | # define fgets(s,n,stream) fgets_unlocked(s,n,stream) | ^~~~~~~~~~~~~~~~~~~~~~~~~~ networking/interface.c:554:9: note: in expansion of macro ‘fgets’ 554 | fgets(buf, sizeof buf, fh); /* eat line */ | ^~~~~ include/libbb.h:236:28: warning: ignoring return value of ‘fgets_unlocked’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 236 | # define fgets(s,n,stream) fgets_unlocked(s,n,stream) | ^~~~~~~~~~~~~~~~~~~~~~~~~~ networking/interface.c:555:9: note: in expansion of macro ‘fgets’ 555 | fgets(buf, sizeof buf, fh); | ^~~~~ CC networking/ip.o CC networking/ipcalc.o CC networking/isrv.o CC networking/isrv_identd.o CC networking/nameif.o CC networking/nbd-client.o networking/nbd-client.c: In function ‘nbdclient_main’: networking/nbd-client.c:281:25: warning: ignoring return value of ‘daemon’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 281 | daemon(0, 0); | ^~~~~~~~~~~~ CC networking/nc.o CC networking/netstat.o CC networking/nslookup.o networking/nslookup.c: In function ‘send_queries’: networking/nslookup.c:622:33: warning: ignoring return value of ‘write’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 622 | write(pfd.fd, G.query[qn].query, G.query[qn].qlen); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ CC networking/ntpd.o networking/ntpd.c: In function ‘update_local_clock’: networking/ntpd.c:1624:49: warning: format ‘%ld’ expects argument of type ‘long int’, but argument 2 has type ‘long long int’ [-Wformat=] 1624 | bb_error_msg("p adjtimex freq:%ld offset:%+ld status:0x%x tc:%ld", | ~~^ | | | long int | %lld 1625 | tmx.freq, tmx.offset, tmx.status, tmx.constant); | ~~~~~~~~ | | | long long int networking/ntpd.c:1624:61: warning: format ‘%ld’ expects argument of type ‘long int’, but argument 3 has type ‘long long int’ [-Wformat=] 1624 | bb_error_msg("p adjtimex freq:%ld offset:%+ld status:0x%x tc:%ld", | ~~~^ | | | long int | %+lld 1625 | tmx.freq, tmx.offset, tmx.status, tmx.constant); | ~~~~~~~~~~ | | | long long int networking/ntpd.c:1624:80: warning: format ‘%ld’ expects argument of type ‘long int’, but argument 5 has type ‘long long int’ [-Wformat=] 1624 | bb_error_msg("p adjtimex freq:%ld offset:%+ld status:0x%x tc:%ld", | ~~^ | | | long int | %lld 1625 | tmx.freq, tmx.offset, tmx.status, tmx.constant); | ~~~~~~~~~~~~ | | | long long int networking/ntpd.c:1739:48: warning: format ‘%ld’ expects argument of type ‘long int’, but argument 3 has type ‘long long int’ [-Wformat=] 1739 | VERB4 bb_error_msg("adjtimex:%d freq:%ld offset:%+ld status:0x%x", | ~~^ | | | long int | %lld 1740 | rc, tmx.freq, tmx.offset, tmx.status); | ~~~~~~~~ | | | long long int networking/ntpd.c:1739:60: warning: format ‘%ld’ expects argument of type ‘long int’, but argument 4 has type ‘long long int’ [-Wformat=] 1739 | VERB4 bb_error_msg("adjtimex:%d freq:%ld offset:%+ld status:0x%x", | ~~~^ | | | long int | %+lld 1740 | rc, tmx.freq, tmx.offset, tmx.status); | ~~~~~~~~~~ | | | long long int CC networking/parse_pasv_epsv.o CC networking/ping.o CC networking/pscan.o CC networking/route.o CC networking/slattach.o networking/slattach.c: In function ‘slattach_main’: networking/slattach.c:225:17: warning: ignoring return value of ‘system’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 225 | system(extcmd); | ^~~~~~~~~~~~~~ CC networking/ssl_client.o CC networking/tc.o networking/tc.c: In function ‘cbq_print_opt’: networking/tc.c:236:27: error: ‘TCA_CBQ_MAX’ undeclared (first use in this function); did you mean ‘TCA_CBS_MAX’? 236 | struct rtattr *tb[TCA_CBQ_MAX+1]; | ^~~~~~~~~~~ | TCA_CBS_MAX networking/tc.c:236:27: note: each undeclared identifier is reported only once for each function it appears in networking/tc.c:249:16: error: ‘TCA_CBQ_RATE’ undeclared (first use in this function); did you mean ‘TCA_TBF_RATE64’? 249 | if (tb[TCA_CBQ_RATE]) { | ^~~~~~~~~~~~ | TCA_TBF_RATE64 networking/tc.c:255:16: error: ‘TCA_CBQ_LSSOPT’ undeclared (first use in this function) 255 | if (tb[TCA_CBQ_LSSOPT]) { | ^~~~~~~~~~~~~~ networking/tc.c:256:61: error: invalid application of ‘sizeofto incomplete type ‘struct tc_cbq_lssopt’ 256 | if (RTA_PAYLOAD(tb[TCA_CBQ_LSSOPT]) < sizeof(*lss)) | ^ networking/tc.c:261:16: error: ‘TCA_CBQ_WRROPT’ undeclared (first use in this function) 261 | if (tb[TCA_CBQ_WRROPT]) { | ^~~~~~~~~~~~~~ networking/tc.c:262:61: error: invalid application of ‘sizeofto incomplete type ‘struct tc_cbq_wrropt’ 262 | if (RTA_PAYLOAD(tb[TCA_CBQ_WRROPT]) < sizeof(*wrr)) | ^ networking/tc.c:267:16: error: ‘TCA_CBQ_FOPT’ undeclared (first use in this function) 267 | if (tb[TCA_CBQ_FOPT]) { | ^~~~~~~~~~~~ networking/tc.c:268:59: error: invalid application of ‘sizeofto incomplete type ‘struct tc_cbq_fopt’ 268 | if (RTA_PAYLOAD(tb[TCA_CBQ_FOPT]) < sizeof(*fopt)) | ^ networking/tc.c:273:16: error: ‘TCA_CBQ_OVL_STRATEGY’ undeclared (first use in this function) 273 | if (tb[TCA_CBQ_OVL_STRATEGY]) { | ^~~~~~~~~~~~~~~~~~~~ networking/tc.c:274:67: error: invalid application of ‘sizeofto incomplete type ‘struct tc_cbq_ovl’ 274 | if (RTA_PAYLOAD(tb[TCA_CBQ_OVL_STRATEGY]) < sizeof(*ovl)) | ^ networking/tc.c:277:50: error: invalid application of ‘sizeofto incomplete type ‘struct tc_cbq_ovl’ 277 | (unsigned) sizeof(*ovl)); | ^ networking/tc.c:293:23: error: invalid use of undefined type ‘struct tc_cbq_lssopt’ 293 | if (lss && lss->flags) { | ^~ networking/tc.c:296:24: error: invalid use of undefined type ‘struct tc_cbq_lssopt’ 296 | if (lss->flags&TCF_CBQ_LSS_BOUNDED) { | ^~ networking/tc.c:296:32: error: ‘TCF_CBQ_LSS_BOUNDED’ undeclared (first use in this function) 296 | if (lss->flags&TCF_CBQ_LSS_BOUNDED) { | ^~~~~~~~~~~~~~~~~~~ networking/tc.c:300:24: error: invalid use of undefined type ‘struct tc_cbq_lssopt’ 300 | if (lss->flags&TCF_CBQ_LSS_ISOLATED) { | ^~ networking/tc.c:300:32: error: ‘TCF_CBQ_LSS_ISOLATED’ undeclared (first use in this function) 300 | if (lss->flags&TCF_CBQ_LSS_ISOLATED) { | ^~~~~~~~~~~~~~~~~~~~ networking/tc.c:308:24: error: invalid use of undefined type ‘struct tc_cbq_wrropt’ 308 | if (wrr->priority != TC_CBQ_MAXPRIO) | ^~ networking/tc.c:308:38: error: ‘TC_CBQ_MAXPRIO’ undeclared (first use in this function) 308 | if (wrr->priority != TC_CBQ_MAXPRIO) | ^~~~~~~~~~~~~~ networking/tc.c:309:46: error: invalid use of undefined type ‘struct tc_cbq_wrropt’ 309 | printf("prio %u", wrr->priority); | ^~ networking/tc.c:313:43: error: invalid use of undefined type ‘struct tc_cbq_wrropt’ 313 | printf("/%u ", wrr->cpriority); | ^~ networking/tc.c:314:32: error: invalid use of undefined type ‘struct tc_cbq_wrropt’ 314 | if (wrr->weight != 1) { | ^~ networking/tc.c:315:65: error: invalid use of undefined type ‘struct tc_cbq_wrropt’ 315 | print_rate(buf, sizeof(buf), wrr->weight); | ^~ networking/tc.c:318:32: error: invalid use of undefined type ‘struct tc_cbq_wrropt’ 318 | if (wrr->allot) | ^~ networking/tc.c:319:57: error: invalid use of undefined type ‘struct tc_cbq_wrropt’ 319 | printf("allot %ub ", wrr->allot); | ^~ networking/tc.c:236:24: warning: unused variable ‘tb’ [-Wunused-variable] 236 | struct rtattr *tb[TCA_CBQ_MAX+1]; | ^~ make[1]: *** [scripts/Makefile.build:198:networking/tc.o] 错误 1 make: *** [Makefile:744:networking] 错误 2 CC networking/tc.o networking/tc.c: In function ‘cbq_print_opt’: networking/tc.c:236:27: error: ‘TCA_CBQ_MAX’ undeclared (first use in this function); did you mean ‘TCA_CBS_MAX’? 236 | struct rtattr *tb[TCA_CBQ_MAX+1]; | ^~~~~~~~~~~ | TCA_CBS_MAX networking/tc.c:236:27: note: each undeclared identifier is reported only once for each function it appears in networking/tc.c:249:16: error: ‘TCA_CBQ_RATE’ undeclared (first use in this function); did you mean ‘TCA_TBF_RATE64’? 249 | if (tb[TCA_CBQ_RATE]) { | ^~~~~~~~~~~~ | TCA_TBF_RATE64 networking/tc.c:255:16: error: ‘TCA_CBQ_LSSOPT’ undeclared (first use in this function) 255 | if (tb[TCA_CBQ_LSSOPT]) { | ^~~~~~~~~~~~~~ networking/tc.c:256:61: error: invalid application of ‘sizeofto incomplete type ‘struct tc_cbq_lssopt’ 256 | if (RTA_PAYLOAD(tb[TCA_CBQ_LSSOPT]) < sizeof(*lss)) | ^ networking/tc.c:261:16: error: ‘TCA_CBQ_WRROPT’ undeclared (first use in this function) 261 | if (tb[TCA_CBQ_WRROPT]) { | ^~~~~~~~~~~~~~ networking/tc.c:262:61: error: invalid application of ‘sizeofto incomplete type ‘struct tc_cbq_wrropt’ 262 | if (RTA_PAYLOAD(tb[TCA_CBQ_WRROPT]) < sizeof(*wrr)) | ^ networking/tc.c:267:16: error: ‘TCA_CBQ_FOPT’ undeclared (first use in this function) 267 | if (tb[TCA_CBQ_FOPT]) { | ^~~~~~~~~~~~ networking/tc.c:268:59: error: invalid application of ‘sizeofto incomplete type ‘struct tc_cbq_fopt’ 268 | if (RTA_PAYLOAD(tb[TCA_CBQ_FOPT]) < sizeof(*fopt)) | ^ networking/tc.c:273:16: error: ‘TCA_CBQ_OVL_STRATEGY’ undeclared (first use in this function) 273 | if (tb[TCA_CBQ_OVL_STRATEGY]) { | ^~~~~~~~~~~~~~~~~~~~ networking/tc.c:274:67: error: invalid application of ‘sizeofto incomplete type ‘struct tc_cbq_ovl’ 274 | if (RTA_PAYLOAD(tb[TCA_CBQ_OVL_STRATEGY]) < sizeof(*ovl)) | ^ networking/tc.c:277:50: error: invalid application of ‘sizeofto incomplete type ‘struct tc_cbq_ovl’ 277 | (unsigned) sizeof(*ovl)); | ^ networking/tc.c:293:23: error: invalid use of undefined type ‘struct tc_cbq_lssopt’ 293 | if (lss && lss->flags) { | ^~ networking/tc.c:296:24: error: invalid use of undefined type ‘struct tc_cbq_lssopt’ 296 | if (lss->flags&TCF_CBQ_LSS_BOUNDED) { | ^~ networking/tc.c:296:32: error: ‘TCF_CBQ_LSS_BOUNDED’ undeclared (first use in this function) 296 | if (lss->flags&TCF_CBQ_LSS_BOUNDED) { | ^~~~~~~~~~~~~~~~~~~ networking/tc.c:300:24: error: invalid use of undefined type ‘struct tc_cbq_lssopt’ 300 | if (lss->flags&TCF_CBQ_LSS_ISOLATED) { | ^~ networking/tc.c:300:32: error: ‘TCF_CBQ_LSS_ISOLATED’ undeclared (first use in this function) 300 | if (lss->flags&TCF_CBQ_LSS_ISOLATED) { | ^~~~~~~~~~~~~~~~~~~~ networking/tc.c:308:24: error: invalid use of undefined type ‘struct tc_cbq_wrropt’ 308 | if (wrr->priority != TC_CBQ_MAXPRIO) | ^~ networking/tc.c:308:38: error: ‘TC_CBQ_MAXPRIO’ undeclared (first use in this function) 308 | if (wrr->priority != TC_CBQ_MAXPRIO) | ^~~~~~~~~~~~~~ networking/tc.c:309:46: error: invalid use of undefined type ‘struct tc_cbq_wrropt’ 309 | printf("prio %u", wrr->priority); | ^~ networking/tc.c:313:43: error: invalid use of undefined type ‘struct tc_cbq_wrropt’ 313 | printf("/%u ", wrr->cpriority); | ^~ networking/tc.c:314:32: error: invalid use of undefined type ‘struct tc_cbq_wrropt’ 314 | if (wrr->weight != 1) { | ^~ networking/tc.c:315:65: error: invalid use of undefined type ‘struct tc_cbq_wrropt’ 315 | print_rate(buf, sizeof(buf), wrr->weight); | ^~ networking/tc.c:318:32: error: invalid use of undefined type ‘struct tc_cbq_wrropt’ 318 | if (wrr->allot) | ^~ networking/tc.c:319:57: error: invalid use of undefined type ‘struct tc_cbq_wrropt’ 319 | printf("allot %ub ", wrr->allot); | ^~ networking/tc.c:236:24: warning: unused variable ‘tb’ [-Wunused-variable] 236 | struct rtattr *tb[TCA_CBQ_MAX+1]; | ^~ make[1]: *** [scripts/Makefile.build:198:networking/tc.o] 错误 1 make: *** [Makefile:744:networking] 错误 2
06-12
使用上面代码编译出现以下问题:make -C ../../ /root/bpf/linux-4.19.90/samples/bpf/ BPF_SAMPLES_PATH=/root/bpf/linux-4.19.90/samples/bpf make[1]: Entering directory '/root/bpf/linux-4.19.90' CALL scripts/checksyscalls.sh DESCEND objtool make -C /root/bpf/linux-4.19.90/samples/bpf/../../tools/lib/bpf/ RM='rm -rf' LDFLAGS= srctree=/root/bpf/linux-4.19.90/samples/bpf/../../ O= Warning: Kernel ABI header at 'tools/include/uapi/linux/bpf.h' differs from latest version at 'include/uapi/linux/bpf.h' HOSTCC /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.o /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c: In function ‘print_violation’: /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:53:26: warning: implicit declaration of function ‘localtime’; did you mean ‘st_atime’? [-Wimplicit-function-declaration] struct tm *tm_info = localtime(&ts); ^~~~~~~~~ st_atime /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:53:26: warning: initialization makes pointer from integer without a cast [-Wint-conversion] /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:55:5: warning: implicit declaration of function ‘strftime’ [-Wimplicit-function-declaration] strftime(time_buf, sizeof(time_buf), "%Y-%m-%d %H:%M:%S", tm_info); ^~~~~~~~ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:55:5: warning: incompatible implicit declaration of built-in function ‘strftime’ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:55:5: note: include ‘<time.h>’ or provide a declaration of ‘strftime’ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c: At top level: /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:69:13: warning: function declaration isn’t a prototype [-Wstrict-prototypes] static void read_violations() { ^~~~~~~~~~~~~~~ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c: In function ‘read_violations’: /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:77:12: error: variable ‘attr’ has initializer but incomplete type struct perf_event_attr attr = { ^~~~~~~~~~~~~~~ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:78:10: error: ‘struct perf_event_attr’ has no member named ‘sample_type’ .sample_type = PERF_SAMPLE_RAW, ^~~~~~~~~~~ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:78:24: error: ‘PERF_SAMPLE_RAW’ undeclared (first use in this function) .sample_type = PERF_SAMPLE_RAW, ^~~~~~~~~~~~~~~ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:78:24: note: each undeclared identifier is reported only once for each function it appears in /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:78:24: warning: excess elements in struct initializer /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:78:24: note: (near initialization for ‘attr’) /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:79:10: error: ‘struct perf_event_attr’ has no member named ‘type’ .type = PERF_TYPE_SOFTWARE, ^~~~ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:79:17: error: ‘PERF_TYPE_SOFTWARE’ undeclared (first use in this function) .type = PERF_TYPE_SOFTWARE, ^~~~~~~~~~~~~~~~~~ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:79:17: warning: excess elements in struct initializer /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:79:17: note: (near initialization for ‘attr’) /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:80:10: error: ‘struct perf_event_attr’ has no member named ‘config’ .config = PERF_COUNT_SW_BPF_OUTPUT, ^~~~~~ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:80:19: error: ‘PERF_COUNT_SW_BPF_OUTPUT’ undeclared (first use in this function) .config = PERF_COUNT_SW_BPF_OUTPUT, ^~~~~~~~~~~~~~~~~~~~~~~~ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:80:19: warning: excess elements in struct initializer /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:80:19: note: (near initialization for ‘attr’) /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:81:10: error: ‘struct perf_event_attr’ has no member named ‘size’ .size = sizeof(attr), ^~~~ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:81:23: error: invalid application of ‘sizeofto incomplete type ‘struct perf_event_attr’ .size = sizeof(attr), ^ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:81:17: warning: excess elements in struct initializer .size = sizeof(attr), ^~~~~~ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:81:17: note: (near initialization for ‘attr’) /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:82:10: error: ‘struct perf_event_attr’ has no member named ‘wakeup_events’ .wakeup_events = 1, ^~~~~~~~~~~~~ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:82:26: warning: excess elements in struct initializer .wakeup_events = 1, ^ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:82:26: note: (near initialization for ‘attr’) /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:77:28: error: storage size of ‘attr’ isn’t known struct perf_event_attr attr = { ^~~~ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:85:23: error: ‘__NR_perf_event_open’ undeclared (first use in this function); did you mean ‘bpf_perf_event_ret’? perf_fd = syscall(__NR_perf_event_open, &attr, -1, 0, -1, 0); ^~~~~~~~~~~~~~~~~~~~ bpf_perf_event_ret /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:100:11: warning: implicit declaration of function ‘mmap’ [-Wimplicit-function-declaration] buf = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED, perf_fd, 0); ^~~~ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:100:33: error: ‘PROT_READ’ undeclared (first use in this function); did you mean ‘IPPROTO_RAW’? buf = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED, perf_fd, 0); ^~~~~~~~~ IPPROTO_RAW /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:100:45: error: ‘PROT_WRITE’ undeclared (first use in this function); did you mean ‘PROT_READ’? buf = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED, perf_fd, 0); ^~~~~~~~~~ PROT_READ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:100:57: error: ‘MAP_SHARED’ undeclared (first use in this function) buf = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED, perf_fd, 0); ^~~~~~~~~~ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:101:16: error: ‘MAP_FAILED’ undeclared (first use in this function); did you mean ‘MAP_SHARED’? if (buf == MAP_FAILED) { ^~~~~~~~~~ MAP_SHARED /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:113:55: error: dereferencing pointer to incomplete type ‘struct perf_event_mmap_page’ __u64 data_head = __sync_fetch_and_add(&header->data_head, 0); ^~ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:131:5: warning: implicit declaration of function ‘munmap’ [-Wimplicit-function-declaration] munmap(buf, mmap_size); ^~~~~~ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c: In function ‘load_xdp’: /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:194:15: warning: implicit declaration of function ‘if_nametoindex’ [-Wimplicit-function-declaration] ifindex = if_nametoindex(iface); ^~~~~~~~~~~~~~ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:214:31: warning: implicit declaration of function ‘bpf_object__find_program_by_name’; did you mean ‘bpf_object__find_program_by_title’? [-Wimplicit-function-declaration] prog_fd = bpf_program__fd(bpf_object__find_program_by_name(obj, "xdp_whitelist")); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ bpf_object__find_program_by_title /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:214:31: warning: passing argument 1 of ‘bpf_program__fd’ makes pointer from integer without a cast [-Wint-conversion] In file included from /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:11:0: ./tools/lib/bpf/libbpf.h:128:5: note: expected ‘struct bpf_program *’ but argument is of type ‘int’ int bpf_program__fd(struct bpf_program *prog); ^~~~~~~~~~~~~~~ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c: At top level: /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:238:13: warning: function declaration isn’t a prototype [-Wstrict-prototypes] static void unload_xdp() { ^~~~~~~~~~ make[2]: *** [scripts/Makefile.host:107: /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.o] Error 1 make[1]: *** [Makefile:1695: /root/bpf/linux-4.19.90/samples/bpf/] Error 2 make[1]: Leaving directory '/root/bpf/linux-4.19.90' make: *** [Makefile:224: all] Error 2
最新发布
07-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值