I try to make a stb stile one header file library and can't get it to work

Joined
Oct 24, 2024
Messages
2
Reaction score
0
To do this, I tried to make it as simple as possible:
teststb.h :
C:
#ifndef TESTSTB_H
#define TESTSTB_H

extern void test(void);                            //test() implementation


#endif //TESTSTB_H

#ifdef TESTSTB_H_IMPLEMENTATION

extern void test(void)                                //test definition
{
    printf("test function executed");       
}


#endif //TESTSTB_H_IMPLEMENTATION

and teststb.c:
C:
#include <stdio.h>
#include "teststb.h"

#define TESTSTB_H_IMPLEMENTATION

int main(int argc, char **argv)
{
    test();
    return 0;
}
when i compile this withgcc -o out2 teststb.c
i get the following error message:
/usr/bin/ld: /tmp/ccFC3JIF.o: in function `main': teststb.c:(.text+0x14): undefined reference to `test' collect2: error: ld returned 1 exit status

Can someone tell me what I'm doing wrong?
And why are all functions written with the extern keyword ?

Any help would be appreciated.
 
Joined
Oct 24, 2024
Messages
2
Reaction score
0
I solved it, it was a brain fard really.
You should #define TESTSTB_H_IMPLEMENTATION before #include "teststb.h".
Can anybody tell me how I can change the title to [solved]?
Kind regards.
Bert.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,039
Messages
2,570,376
Members
47,031
Latest member
AndreBucki

Latest Threads

Top