简体   繁体   中英

C preprocessor directive error

I have a problem when i want use his scripts:

lib1.h
...
#ifdef LIB1_01
int lib1func(void);
#endif
...

lib1.c
...
#ifdef LIB1_01
int lib1func(void){
   ...
}
#endif
...

main.c

#define LIB1_01
#include <lib1.h>
int main(){
   ...
   int x = lib1func(void);
   ...
...

I want use lib1func() when #define LIB1_01 is declared but I have an 'warning : implicit declaration of function' error when i use it...why ? Can you help me ? Best regards.

Recommended alternative:

lib1.h

#ifndef LIB1_H
#define LIB1_H
int lib1func(void);
#endif
...

lib1.c

#include "lib1.h"
int lib1func(void){
   ...
}

main.c

#include "lib1.h"
int main(){
   ...
   int x = lib1func(void);
   ...
...

NOTE:

1) You should declare "int lib1func(void)" in the header, but you may define it anywhere. In lib1.c (if you prefer), or even main.c. Just make sure you only define it once.

2) Note the use of the guard around the entire header body.

3) Also note the use of include "myheader.h" (for your own header files), vs. #include <systemheader.h> . The " <> " syntax should be used only for system headers.

To use that kind of includes, compile with option I .

gcc myfile.c -o myfile -I .

The . symbol means look in the current directory.

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