可以禁用/排除预处理器指令#include吗?

发布于 2024-12-18 22:24:30 字数 542 浏览 6 评论 0原文

例如:如果我有两个 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

拒绝两难 2024-12-25 22:24:30

不,您不能“取消包含”文件。将所有预处理器指令(以 # 开头的行)视为发生在实际 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.

乄_柒ぐ汐 2024-12-25 22:24:30

不,您不应该尝试编写具有两个同名函数的程序。

在特殊情况下,函数实际上是在头文件中定义的(而不仅仅是原型),您可以这样做:

#define function function_file1
#include "file1.h"
#undef function

#define function function_file2
#include "file2.h"
#undef function

int
main (void)
  {
    int a = 2;

    printf ("%d\n", function_file1 (a));
    printf ("%d\n", function_file2 (a));
  }

但是如果您重命名函数原型,那么您就没有实际上并没有重命名真正的函数,所以当你链接时你会得到未定义的符号错误。

无论如何,如果您定义了两个具有相同名称的函数,那么无论您在源代码中执行什么操作,它都不会链接。 (在 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:

#define function function_file1
#include "file1.h"
#undef function

#define function function_file2
#include "file2.h"
#undef function

int
main (void)
  {
    int a = 2;

    printf ("%d\n", function_file1 (a));
    printf ("%d\n", function_file2 (a));
  }

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()).

望笑 2024-12-25 22:24:30

为什么不使用函数指针数组。当然,您需要在开始时对其进行初始化,但我认为它可能可以解决您想要做的事情。

int process1_function(int var);
int process2_function(int var);

int main(void)
{
    int i, a = 10;
    int (* functions[2])(int);

    functions[0] = process1_function;
    functions[1] = process2_function;

    for(i=0; i < 2; i++)
    {
        printf("%d", (functions[i])(a));
    }

    return 0;
}

如果您不需要动态更改要调用的函数,您也可以只为函数添加前缀:

int process1_function(int var);
int process2_function(int var);

int main(void)
{
   printf("%d",process1_function(a));
   printf("%d",process2_function(a));

   return 0;
}

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.

int process1_function(int var);
int process2_function(int var);

int main(void)
{
    int i, a = 10;
    int (* functions[2])(int);

    functions[0] = process1_function;
    functions[1] = process2_function;

    for(i=0; i < 2; i++)
    {
        printf("%d", (functions[i])(a));
    }

    return 0;
}

If you do not need to dynamically change which function you're going to call you can also just prefix the functions:

int process1_function(int var);
int process2_function(int var);

int main(void)
{
   printf("%d",process1_function(a));
   printf("%d",process2_function(a));

   return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文