可以禁用/排除预处理器指令#include吗?
例如:如果我有两个 .h 文件
process1.h 和 process2.h
,它们包含两个具有不同输出变量的函数。
process1.h:
function(int var)
{
return 2*var;
}
process2.h:
function(int var)
{
return 10*var;
}
这可以在 main.c 中完成吗:
int main()
{
int a = 2;
#include "process1.h"
printf("%d",function(a)); //output is 4
EXCLUDE #INCLUDE "process1.h" ????? <----can this be done in any way??
#include "process2.h"
printf("%d",function(a)); //output is 20
}
For example: If I have two .h files
process1.h and process2.h
and they contain two function with different output variables.
process1.h:
function(int var)
{
return 2*var;
}
process2.h:
function(int var)
{
return 10*var;
}
Can this be done in main.c:
int main()
{
int a = 2;
#include "process1.h"
printf("%d",function(a)); //output is 4
EXCLUDE #INCLUDE "process1.h" ????? <----can this be done in any way??
#include "process2.h"
printf("%d",function(a)); //output is 20
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不,您不能“取消包含”文件。将所有预处理器指令(以
#
开头的行)视为发生在实际 C 编译器甚至看到源文件之前。它们只对文件的文本进行操作,并且预处理器可以作为一个单独的步骤来实现,只需将新文本输入到实际的编译器中。根据调用者修改
include
操作的最佳方法是在包含的文件中使用更多宏,您可以在包含它们之前#define
。尽管如此,你的整体语法还是关闭的,你不能(通常)在 C 中嵌套函数。
No, you cannot "un-include" a file. Think of all the preprocessor directives (lines starting with
#
) as happening before the actual C compiler even sees the source file. They just operate on the text of the file, and the preprocessor could be implemented as a separate step that just feeds new text into the actual compiler.The best way to modify the actions of an
include
depending on the caller is to use further macros inside the included files, that you can#define
before including them.Still, your overall syntax is off, you can't (typically) nest functions in C.
不,您不应该尝试编写具有两个同名函数的程序。
在特殊情况下,函数实际上是在头文件中定义的(而不仅仅是原型),您可以这样做:
但是如果您重命名函数原型,那么您就没有实际上并没有重命名真正的函数,所以当你链接时你会得到未定义的符号错误。
无论如何,如果您定义了两个具有相同名称的函数,那么无论您在源代码中执行什么操作,它都不会链接。 (在 C++ 中,有时可以定义两个具有相同名称的事物,但单定义规则意味着链接器可以假设它们实际上是同一事物,然后只选择一个。)
这就是为什么库应该被认为是使用不会在其他地方使用的名称 - 通常通过为所有符号名称添加公共前缀(例如
my_unique_lib_initialize()
)。No, and you should not try to write a program with two functions of the same name.
In the special case that the functions are actually defined in the header file (instead of just prototypes), you can do this:
BUT if you rename a function prototype then you haven't actually renamed the real function, so you'll get undefined symbol error when you link.
In any case, if you have two functions defined with the same name then it won't link anyway, not matter what else you do in the sources. (In C++, it is sometimes possible to define two things with the same name, but the One-Definition-Rule means the linker is allowed to assume they are both the same thing really and just pick one.)
This is why libraries are supposed to use names that won't be used elsewhere - usually by adding a common prefix to all symbol names (e.g.
my_unique_lib_initialize()
).为什么不使用函数指针数组。当然,您需要在开始时对其进行初始化,但我认为它可能可以解决您想要做的事情。
如果您不需要动态更改要调用的函数,您也可以只为函数添加前缀:
Why not use array of function pointers. Sure you need to initialize it at the start but I think it probably solves what you want to do.
If you do not need to dynamically change which function you're going to call you can also just prefix the functions: