invalid application of `sizeof' to incomplete type `char[] '

invalid application of `sizeof' to incomplete type `char[] '

在写代码时,我想用extern来关联一个数组,然后利用sizeof计算数组的大小,代码如下:

...
extern char a[];
#define b size=(sizeof(a)/sizeof(a[0]))
...

编译报错:

invalid application of `sizeof' to incomplete type `char[] '

主要原因是sizeof不能用在extern变量: 
sizeof 的计算是在代码编译的时刻。 
而extern是在链接的时刻。 
所以 sizeof 在计算时,并不知道这个extern修饰的符号到底占用了多少空间。

解决办法:

1.把sizeof()和数组写到一个文件中

2.不用宏定义b,而是在定义数组a的那个文件里定义 size=(sizeof(a)/sizeof(a[0]));
然后在其他文件中
extern int size;
extern char a[];

posted @ 2019-02-15 16:41 小时候挺菜 阅读( ...) 评论( ...) 编辑 收藏
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 ofsizeofto 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 ofsizeofto 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 ofsizeofto 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 ofsizeofto 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 ofsizeofto 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 ofsizeofto 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 ofsizeofto 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 ofsizeofto 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 ofsizeofto 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 ofsizeofto 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
以下是common.c代码,可以结合server_process理解,写好注释给我#include "common.h" void send_response(int client_fd, int status, const char* title, const char* text) { char response[BUFFER_SIZE]; int len = snprintf(response, BUFFER_SIZE, "HTTP/1.1 %d %s\r\n" "Content-Type: text/html\r\n" "Connection: close\r\n" "\r\n" "<html><head><title>%d %s</title></head>" "<body><h1>%d %s</h1><p>%s</p></body></html>", status, title, status, title, status, title, text); send(client_fd, response, len, 0); DEBUG("Sent %d response\n", status); } void send_file(int client_fd, const char* path) { int fd = open(path, O_RDONLY); if (fd < 0) { perror("open"); send_response(client_fd, 404, "Not Found", "File not found"); return; } struct stat file_stat; if (fstat(fd, &file_stat) < 0) { perror("fstat"); close(fd); send_response(client_fd, 500, "Internal Error", "Could not get file stats"); return; } // 确定Content-Type const char *content_type = "text/plain"; if (strstr(path, ".html")) content_type = "text/html"; else if (strstr(path, ".css")) content_type = "text/css"; else if (strstr(path, ".js")) content_type = "application/javascript"; else if (strstr(path, ".png")) content_type = "image/png"; else if (strstr(path, ".jpg")) content_type = "image/jpeg"; else if (strstr(path, ".json")) content_type = "application/json"; char header[BUFFER_SIZE]; int header_len = snprintf(header, BUFFER_SIZE, "HTTP/1.1 200 OK\r\n" "Content-Type: %s\r\n" "Content-Length: %ld\r\n" "Connection: close\r\n\r\n", content_type, file_stat.st_size); send(client_fd, header, header_len, 0); sendfile(client_fd, fd, NULL, file_stat.st_size); DEBUG("Sent file: %s (%ld bytes)\n", path, file_stat.st_size); close(fd); } // URL解码函数 char* url_decode(const char* str) { if (!str) return NULL; char* decoded = malloc(strlen(str) + 1); if (!decoded) return NULL; char* d = decoded; for (const char* s = str; *s; s++) { if (*s == '+') { *d++ = ' '; } else if (*s == '%' && isxdigit(s[1]) && isxdigit(s[2])) { char hex[3] = {s[1], s[2], '\0'}; *d++ = (char)strtol(hex, NULL, 16); s += 2; } else { *d++ = *s; } } *d = '\0'; return decoded; } // 处理POST请求 void handle_post_request(int client_fd, const char* path, const char* post_data, size_t data_len) { DEBUG("Handling POST request to %s\n", path); // 简单的表单处理 if (strcmp(path, "/submit") == 0) { // 解析表单数据 char* decoded = url_decode(post_data); if (!decoded) { send_response(client_fd, 500, "Internal Error", "Failed to decode data"); return; } DEBUG("Decoded POST data: %s\n", decoded); // 提取表单字段 char name[256] = {0}; char email[256] = {0}; char message[1024] = {0}; char* token = strtok(decoded, "&"); while (token) { char* eq = strchr(token, '='); if (eq) { *eq = '\0'; char* key = token; char* value = eq + 1; if (strcmp(key, "name") == 0) { strncpy(name, value, sizeof(name)-1); } else if (strcmp(key, "email") == 0) { strncpy(email, value, sizeof(email)-1); } else if (strcmp(key, "message") == 0) { strncpy(message, value, sizeof(message)-1); } } token = strtok(NULL, "&"); } // 构建响应页面 char response[BUFFER_SIZE]; int len = snprintf(response, BUFFER_SIZE, "HTTP/1.1 200 OK\r\n" "Content-Type: text/html\r\n" "Connection: close\r\n" "\r\n" "<html><head><title>Form Submitted</title></head>" "<body><h1>Thank You!</h1>" "<p>Name: %s</p>" "<p>Email: %s</p>" "<p>Message: %s</p>" "<p><a href=\"/\">Back to home</a></p></body></html>", name, email, message); send(client_fd, response, len, 0); free(decoded); } // 简单的API处理 else if (strcmp(path, "/api/data") == 0) { char response[BUFFER_SIZE]; int len = snprintf(response, BUFFER_SIZE, "HTTP/1.1 200 OK\r\n" "Content-Type: application/json\r\n" "Connection: close\r\n" "\r\n" "{\"status\":\"success\",\"message\":\"Data received\",\"length\":%zu}", data_len); send(client_fd, response, len, 0); } else { send_response(client_fd, 404, "Not Found", "POST endpoint not found"); } } void handle_request(int client_fd) { char buffer[BUFFER_SIZE]; ssize_t bytes_read = recv(client_fd, buffer, BUFFER_SIZE - 1, 0); if (bytes_read <= 0) { DEBUG("Connection closed or error\n"); close(client_fd); return; } buffer[bytes_read] = '\0'; DEBUG("Request:\n%s\n", buffer); // 检测HTTP方法 HttpMethod method = HTTP_UNKNOWN; char *method_start = buffer; char *method_end = strchr(method_start, ' '); if (method_end) { *method_end = '\0'; if (strcmp(method_start, "GET") == 0) method = HTTP_GET; else if (strcmp(method_start, "POST") == 0) method = HTTP_POST; else if (strcmp(method_start, "PUT") == 0) method = HTTP_PUT; else if (strcmp(method_start, "DELETE") == 0) method = HTTP_DELETE; *method_end = ' '; // 恢复原始数据 } // 解析请求路径 char *path_start = buffer; if (method != HTTP_UNKNOWN) { path_start = strchr(buffer, ' ') + 1; } char *path_end = strchr(path_start, ' '); if (!path_end) { send_response(client_fd, 400, "Bad Request", "Invalid request"); close(client_fd); return; } *path_end = '\0'; // 处理POST请求 if (method == HTTP_POST) { // 查找Content-Length char *content_length_start = strstr(buffer, "Content-Length: "); size_t content_length = 0; if (content_length_start) { content_length_start += 15; // 跳过"Content-Length: " char *content_length_end = strchr(content_length_start, '\r'); if (content_length_end) { *content_length_end = '\0'; content_length = atoi(content_length_start); *content_length_end = '\r'; // 恢复原始数据 } } // 限制POST数据大小 if (content_length > MAX_POST_SIZE) { send_response(client_fd, 413, "Payload Too Large", "POST data too large"); close(client_fd); return; } // 查找POST数据开始位置 char *post_data = strstr(buffer, "\r\n\r\n"); if (post_data) { post_data += 4; // 跳过空行 // 确保不超出缓冲区 size_t data_size = bytes_read - (post_data - buffer); if (data_size < content_length) { // 需要读取更多数据(简化处理) DEBUG("Incomplete POST data, need to read more\n"); send_response(client_fd, 400, "Bad Request", "Incomplete POST data"); close(client_fd); return; } // 处理POST请求 handle_post_request(client_fd, path_start, post_data, content_length); close(client_fd); return; } } // 构建完整路径(处理GET请求) char full_path[256]; if (strcmp(path_start, "/") == 0) { snprintf(full_path, sizeof(full_path), "%s/Index.html", ROOT_DIR); } else { snprintf(full_path, sizeof(full_path), "%s%s", ROOT_DIR, path_start); } // 检查文件是否存在 struct stat st; if (stat(full_path, &st) == 0 && S_ISREG(st.st_mode)) { send_file(client_fd, full_path); } else { send_response(client_fd, 404, "Not Found", "File not found"); } close(client_fd); }
最新发布
08-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值