1

I am trying to write a C header file in this manner:

// header.h
#ifndef HEADER_H
#define HEADER_H

extern int add(int a, int b);

#endif
// header.c
#include "header.h"

int add(int a, int b)
{
    return (a+b);
}

Now suppose I have a main.c file like this:

// main.c
#include <stdio.h>
#include "header.h"

int main(void)
{
    print("%d", add(2,3));
    return 0;
}

I get an error when compiling main.c and GCC says " undefined reference to 'add' ". Can you please explain where have I gone wrong?

EDIT:
I think I didn't make myself very clear previously. What I am actually trying to say is that, I've seen that most C programmers follow the above mentioned style of writing the header files, and I've also heard that this reduces the size of the final binary that's generated. I'm trying to do something similar but I get an error every time I try to compile my code. I added that extern keyword so that the compiler understands that the function is defined somewhere externally, but it still failed to compile.

How can I compile the above code successfully with the implementation of the said programming style?

arijitkd
  • 103
  • 7
  • 1
    Search for "undefined reference", it's not in the header file (apart from the trailing '/'). – Ulrich Eckhardt Jun 14 '23 at 14:09
  • 1
    No need of `extern` for function. It is only for variables. – i486 Jun 14 '23 at 14:14
  • How do you compile? What is your OS? What is your IDE (if any)? – Jabberwocky Jun 14 '23 at 14:44
  • @UlrichEckhardt wrong duplicate, this is C, not C++. I reopened. – Jabberwocky Jun 14 '23 at 14:46
  • 2
    gcc -o progname main.c header.c – ulix Jun 14 '23 at 14:47
  • 3
    You need to link both `header.o` and `main.o` to create the executable: `cc -o program main.o header.o` or equivalent. – Jonathan Leffler Jun 14 '23 at 14:48
  • Thank you @JonathanLeffler. Your suggestion worked. – arijitkd Jun 14 '23 at 14:52
  • @MisterTux you should remove the edit in your question. The question was perfectly clear before. The edit OTOH is rather confusing. – Jabberwocky Jun 14 '23 at 15:00
  • 1
    Worth recognizing that the error doesn't actually occur while compiling main.c. It's not a compilation error. It occurs when gcc invokes the linker ld and ld tries to link main.o into an executable. Because you didn't tell gcc to tell ld to use header.o when linking, the symbol `add` is undefined at link time. – jarmod Jun 14 '23 at 15:02
  • Possible duplicate of [C error: undefined reference to function, but it IS defined](https://stackoverflow.com/questions/5559250/c-error-undefined-reference-to-function-but-it-is-defined). – pmacfarlane Jun 14 '23 at 20:06

1 Answers1

2

There are two problems:

header.h

// header.h
#ifndef HEADER_H
#define HEADER_H

int add(int a, int b);

#endif
  • You don't need "extern"
  • You shouldn't have the trailing "/"
  • You should have a newline after the final "#endif"

Example gcc command:

gcc -g -Wall -pedantic main.c header.c -o myprog

You need to compile both "main.c" and "header.c", and include both ".o" files in your build.

It sounds like the "undefined reference" was a linker error: you probably failed to include "header.o".

paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • 1
    The extern does no damage, even though it isn't necessary. – Jonathan Leffler Jun 14 '23 at 15:02
  • Actually that trailing '/' was a typo. I edited the question and removed it. – arijitkd Jun 14 '23 at 15:06
  • ALSO: the "#ifndef HEADER_H" convention is an example of a "header guard". It's ensures you can safely (re)use the header wherever you need it in more complex C/C++ programs: https://en.wikipedia.org/wiki/Include_guard – paulsm4 Jun 14 '23 at 15:20