C语言 你能在另一个文件中extern一个#define变量吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16279975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can you extern a #define variable in another file?
提问by user1295872
For example abc.ccontains a variable
例如abc.c包含一个变量
#define NAME "supreeth"
Can extern the variable NAMEin def.c?
可以NAME在def.c 中extern 变量吗?
采纳答案by Sudhee
If you have #define NAME "supreeth"in abc.c, you can surely have a extern variable by same name in another file def.c, this is as far as the compiler is concerned. If you are implying some kind of dependency between these two, that dependency/linkage will not happen.
Obviously it is confusing and a bad idea to do something like this.
如果你#define NAME "supreeth"在 abc.c 中有,你肯定可以在另一个文件中有一个同名的 extern 变量def.c,这是编译器所关心的。如果您暗示这两者之间存在某种依赖关系,则不会发生这种依赖关系/链接。显然,做这样的事情令人困惑,也是一个坏主意。
回答by MOHAMED
You can not use externwith macro. but if you want your macro seen by many C files
不能extern与宏一起使用。但是如果你想让你的宏被许多 C 文件看到
put your macro definition
把你的宏定义
#define NAME "supreeth"
in a header file like def.h
在像def.h这样的头文件中
then include your def.hin your C code and then you can use your macro in your C file in all other C file if you include def.h
然后在您的 C 代码中包含您的def.h,然后您可以在所有其他 C 文件中的 C 文件中使用您的宏,如果您包含def.h
回答by unwind
In your code NAMEis not a variable. It's a pre-processor symbol, which means the text NAMEwill be replaced everywhere in the input with the string "supreeth". This happens per-file, so it doesn't make sense to talk about it being "external".
在您的代码NAME中不是变量。它是一个预处理器符号,这意味着文本NAME将在输入中的任何地方替换为 string "supreeth"。这发生在每个文件中,因此谈论它是“外部的”是没有意义的。
If a particular C file is compiled without that #define, any use of NAMEwill remain as-is.
如果在没有那个的情况下编译了一个特定的 C 文件#define,则任何使用都NAME将保持原样。

