简体   繁体   中英

Compilation error while including header file in compile command

I have two files main.c and header.c .

main.c has some macro STR who value I want to define conditionally according to some #define in the file.

Case 1 :

When I include header.c in main.c file, the program is working fine 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
anupam@g3:~/Desktop/OS 2020/so$ ./a.out
flag a is activated.

Case 2 :

But for some reason, I want to include header.c in the compile command and not inside 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. More about macros here

#include just copy a file and paste it in specified place. 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.

If you want to keep it as a #define then you need to include the header.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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