John S
John S

Reputation: 231

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.

Upvotes: 0

Views: 83

Answers (2)

paulsm4
paulsm4

Reputation: 121871

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.

Upvotes: 1

Ryan
Ryan

Reputation: 14659

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

gcc myfile.c -o myfile -I .

The . symbol means look in the current directory.

Upvotes: 0

Related Questions