0

I'm having a conundrum with CodeBlocks in sharing code among different files using headers (.h) and source code (.c).

I have three files: main.c, anotherSourceFile.c, sampleHeader.h

Long story short, when I'm trying to include a function in the main.c file - e.g. calling a sample foo() function inside the main() function - I get an undefined reference to 'foo' error.

This is the code inside each file:

Inside the main.c file

// Include the header
#include "sampleHeader.h"

// Include standard libraries
#include <stdio.h>
#include <stdlib.h>

int main() {
    
    // Call to the would-be imported function
    foo();    // WHERE THE ERROR OCCUR: undefined reference to 'foo'

    return 0;
}

Inside the sampleHeader.h file...

#ifndef SAMPLEHEADER_H_INCLUDED
#define SAMPLEHEADER_H_INCLUDED

// foo() function prototype
void foo(void);

#endif // SAMPLEHEADER_H_INCLUDED

Inside the anotherSourceCode.c file...

// Include the header
#include "sampleHeader.h"

// Include standard libraries
#include <stdlib.h>
#include <stdio.h>

// foo() function implementation
void foo() {
    printf("Hello from foo.");
}

I don't get where is the kink and how to work it out, neither do I know where to look in order to find a solution. How can I get rid of the undefined reference to 'foo' error?

Dan Filban
  • 47
  • 4

0 Answers0