GCC是否对使用不完整文件宽容?

huangapple go评论57阅读模式
英文:

Is GCC being lenient with the use of an incomplete file?

问题

I have a file main.c, which contains the following line of code:

struct args packed;

struct args is defined in another file named dct.c. However, that file is not included. They are passed to the compiler as gcc -Wall main.c dct.c ....

Shouldn't the compiler emit a warning about an incomplete type?

Note that struct args is being instantiated, and its members are accessed within main.c.

I only noticed this because of the linter warning me about an incomplete type, however GCC seems fine with it.

What am I missing? Because as I suspected, and according to this answer, they are indeed different translation units, and GCC can't see the definition in the second file.

英文:

I have a file main.c, which contains the following line of code:

struct args packed;

the struct args is defined in another file named dct.c. However that file is not included. They are passed to the compiler as gcc -Wall main.c dct.c ...

Shouldn't the compiler emit a warning about an incomplete type?

Note that struct args is being instantiated and its members are accessed within main.c.

I only noticed this because of the linter warning me about an incomplete type, however GCC seems fine with it.

What am I missing? Because as I suspected, and according to this answer they are indeed different translation units and GCC can't see the definition in the second file.

答案1

得分: 2

I will only translate the non-code parts of your text:

"Are you sure the struct isn't declared in a header file that your main.c includes? Are you using some IDE that is doing magic for you?"

如果该结构体没有在主文件 main.c 包含的头文件中声明,您确定吗?您是否使用了某个为您执行魔法的集成开发环境(IDE)?

"If I simply define a struct in one file and try to use it in the other it doesn't work as you describe:"

如果我仅在一个文件中定义结构体,然后尝试在另一个文件中使用它,它不会按您描述的方式工作:

英文:

Are you sure the struct isn't declared in a header file that your main.c includes? Are you using some IDE that is doing magic for you?

If I simply define a struct in one file and try to use it in the other it doesn't work as you describe:

gcc version 12.2.1 20230201 (GCC) 

$ gcc main.c args.c 
main.c: In function ‘main’:
main.c:5:15: error: storage size of ‘packed’ isn’t known
    5 |   struct args packed;
      |               ^~~~~~

This works:

$ cat args.h 
#ifndef _ARGS_H_
#define _ARGS_H_
struct args {
  int val;
};
#endif //_ARGS_H

$ cat args.c 
#include <stdlib.h>
#include "args.h"

struct args *makearg(int val) {
  struct args *ret = malloc(sizeof(struct args));
  ret->val = val;
  return ret;
}

$ cat main.c 
#include <stdio.h>
#include <stdlib.h>
#include "args.h"

extern struct args *makearg(int val);

int main(int argc,char **argv) {
  struct args *packed = makearg(3);
  printf("%d\n",packed->val);
  free(packed);
  return 0;
}

$ gcc main.c args.c 
$ ./a.out 
3

huangapple
  • 本文由 发表于 2023年5月14日 17:28:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76246741.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定