简体   繁体   English

在编译命令中包含头文件时出现编译错误

[英]Compilation error while including header file in compile command

I have two files main.c and header.c .我有两个文件main.cheader.c

main.c has some macro STR who value I want to define conditionally according to some #define in the file. main.c有一些宏STR ,我想根据文件中的一些#define有条件地定义它们的值。

Case 1 :案例一

When I include header.c in main.c file, the program is working fine as shown below:当我在main.c文件中包含header.c时,程序运行正常,如下所示:

main.c主文件

#include<stdio.h>

#define _flag_b
#include "header.c"

void main(){
    printf("%s", STR);
}

header.c头文件

#ifndef _flag_a
#define STR "flag a is activated.\n" 
#endif

#ifndef _flag_b
#define STR "flag b is activated.\n" 
#endif

Compilation汇编

anupam@g3:~/Desktop/OS 2020/so$ gcc main.c
anupam@g3:~/Desktop/OS 2020/so$ ./a.out
flag a is activated.

Case 2 :案例2

But for some reason, I want to include header.c in the compile command and not inside main.c .但出于某种原因,我想在编译命令中包含header.c而不是在main.c Which is creating this issue for me as shown below:这为我创造了这个问题,如下所示:

main.c主文件

#include<stdio.h>

#define _flag_b
// #include "header.c"

void main(){
    printf("%s", STR);
}

header.c头文件

#ifndef _flag_a
#define STR "flag a is activated.\n" 
#endif

#ifndef _flag_b
#define STR "flag b is activated.\n" 
#endif

Compilation汇编

anupam@g3:~/Desktop/OS 2020/so$ gcc main.c header.c
main.c: In function ‘main’:
main.c:7:15: error: ‘STR’ undeclared (first use in this function)
    7 |  printf("%s", STR);
      |               ^~~
main.c:7:15: note: each undeclared identifier is reported only once for each function it appears in
header.c:6: warning: "STR" redefined
    6 | #define STR "flag b is activated.\n"
      | 
header.c:2: note: this is the location of the previous definition
    2 | #define STR "flag a is activated.\n"
      | 

I have done a lot of research on this issue, and able to understand why the problem is arising.我对这个问题做了很多研究,并且能够理解为什么会出现这个问题。 But I am not able to solve this issue.但我无法解决这个问题。

Please help me in understanding this problem better and suggest some solutions to this.请帮助我更好地理解这个问题,并为此提出一些解决方案。 Also help me in rephrasing the problem.也帮助我重新表述这个问题。

#define defines a macro for a preprocessor - it means that before compilation, every instance of defined macro (after its definition) is replaced, in Your case after #define STR ... every instance of STR is replaced with specified constant. #define 为预处理器定义了一个宏 - 这意味着在编译之前,已定义宏的每个实例(在其定义之后)都被替换,在您的情况下,在 #define STR 之后......每个 STR 实例都被替换为指定的常量。 More about macros here更多关于这里的

#include just copy a file and paste it in specified place. #include 只是复制一个文件并将其粘贴到指定的位置。 More about headers here更多关于标题在这里

First example works because you included your header and code looks like this:第一个示例有效,因为您包含了标头和代码,如下所示:

/*
  stuff included by stdio.h
*/
int main(void) {
  printf("%s", "flag a is activated.\n");
}

And it can compile easily.它可以轻松编译。 But in the second example you try to compile every file separately, so the first file looks like this:但是在第二个示例中,您尝试分别编译每个文件,因此第一个文件如下所示:

/*
  stuff included by stdio.h
*/
int main(void) {
  printf("%s", STR); //preprocessor doesn't recognise STR as a macro
}

And the second file is empty.第二个文件是空的。 So now the compiler tries to compile it and it doesn't know what STR is, so you have an error.所以现在编译器试图编译它,但它不知道 STR 是什么,所以你有一个错误。

If you want to keep it as a #define then you need to include the header.如果要将其保留为 #define,则需要包含标题。

You can read more about preprocessing here .您可以在此处阅读有关预处理的更多信息。 If you want to see the output of preprocessor then you need to use a -E flag, for example: gcc main.c -E -o mainPreprocessed.c如果要查看预处理器的输出,则需要使用 -E 标志,例如:gcc main.c -E -o mainPreprocessed.c

Please, next time include code as a text, not an image - it will be easier for people to answer.请下次将代码作为文本而不是图像包含 - 人们会更容易回答。

One more thing: *.c files are for code (that you add in your g++ command) and *.h files are for headers (that you include with #include).还有一件事:*.c 文件用于代码(您在 g++ 命令中添加),*.h 文件用于标头(您用 #include 包含)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM