条件编译三文件工程
conditional compiling three-file project
我有三个文件:
main.c
#include <stdio.h>
#include "file3.h"
#define PERMISSION
int main()
{
#ifdef PERMISSION
myfunction1();
#else
myfunction2();
#endif // PERMISSION
return 0;
}
file2.c
#include <stdio.h>
#include "file3.h"
#ifdef PERMISSION
void myfunction1()
{
printf("Inside myfunction1 on file2.c \n");
}
#else
void myfunction2()
{
printf("Inside myfunction2 on file2.c \n");
}
#endif
file3.h
#ifndef FILE3_H_INCLUDED
#define FILE3_H_INCLUDED
#ifdef PERMISSION
void myfunction1();
#else
void myfunction2();
#endif
#endif
我想通过主文件或头文件中的宏 PERMISSION 来控制 3 文件项目的编译。如果已定义,我应该在所有三个文件上编译所有 myfunction1 部分。如果不是,则 myfunction2 部分。但是当我将它放在 main.c 或 file3.h
上时,出现“#define PERMISSION”时出现错误
Codeblocks 告诉我 file2 和 file3 默认为 else 部分,因此它们没有看到定义的 PERMISSION。我不想把它放在所有文件上并在我不希望它定义时修改所有文件
现在它因对 myfunction1 的未定义引用而出错
也许我不应该将#ifdef 放在头文件中 file3.h 但它无论如何都不起作用。
我该如何做对?
您需要在所有文件中#define PERMISSION。要么
明确的地方在每个文件中
放在普通的header
在编译器中传递 -DPERMISSIONS(如果编译器允许)
将它添加到你的编译器命令行那里就是它的位置
> I am using Codeblocks. I haven't been able to find the way to pass compiling options to it like in the command line – juan carlos vega oliver 3 hours ago
在“项目”->“构建选项”下尝试,在“编译器设置”选项卡内 select 选项卡#defines
我有三个文件: main.c
#include <stdio.h>
#include "file3.h"
#define PERMISSION
int main()
{
#ifdef PERMISSION
myfunction1();
#else
myfunction2();
#endif // PERMISSION
return 0;
}
file2.c
#include <stdio.h>
#include "file3.h"
#ifdef PERMISSION
void myfunction1()
{
printf("Inside myfunction1 on file2.c \n");
}
#else
void myfunction2()
{
printf("Inside myfunction2 on file2.c \n");
}
#endif
file3.h
#ifndef FILE3_H_INCLUDED
#define FILE3_H_INCLUDED
#ifdef PERMISSION
void myfunction1();
#else
void myfunction2();
#endif
#endif
我想通过主文件或头文件中的宏 PERMISSION 来控制 3 文件项目的编译。如果已定义,我应该在所有三个文件上编译所有 myfunction1 部分。如果不是,则 myfunction2 部分。但是当我将它放在 main.c 或 file3.h
上时,出现“#define PERMISSION”时出现错误Codeblocks 告诉我 file2 和 file3 默认为 else 部分,因此它们没有看到定义的 PERMISSION。我不想把它放在所有文件上并在我不希望它定义时修改所有文件 现在它因对 myfunction1 的未定义引用而出错 也许我不应该将#ifdef 放在头文件中 file3.h 但它无论如何都不起作用。
我该如何做对?
您需要在所有文件中#define PERMISSION。要么
明确的地方在每个文件中
放在普通的header
在编译器中传递 -DPERMISSIONS(如果编译器允许)
将它添加到你的编译器命令行那里就是它的位置
> I am using Codeblocks. I haven't been able to find the way to pass compiling options to it like in the command line – juan carlos vega oliver 3 hours ago
在“项目”->“构建选项”下尝试,在“编译器设置”选项卡内 select 选项卡#defines