36

Alright, I've been trying to work with this for the longest time, and I simply can't seem to get it to work right. I have three files, main.c, hello_world.c, and hello_world.h. For whatever reason they don't seem to compile nicely, and I really just can't figure out why...

Here are my source files. First hello_world.c:

#include <stdio.h>
#include "hello_world.h"

int hello_world(void) {
  printf("Hello, Stack Overflow!\n");
  return 0;
}

Then hello_world.h, simple:

int hello_world(void);

And then finally main.c:

#include "hello_world.h"

int main() {
  hello_world();
  return 0;
}

When I put it into GCC, this is what I get:

cc     main.c   -o main
/tmp/ccSRLvFl.o: In function `main':
main.c:(.text+0x5): undefined reference to `hello_world'
collect2: ld returned 1 exit status
make: *** [main] Error 1

Anyone able to help me out? I'm really stuck on this, but I'm 99 percent sure it's a really simple fix.

mtvec
  • 17,046
  • 4
  • 49
  • 81
user1018501
  • 463
  • 1
  • 4
  • 4

5 Answers5

50
gcc main.c hello_world.c -o main

Also, always use header guards:

#ifndef HELLO_WORLD_H
#define HELLO_WORLD_H

/* header file contents go here */

#endif /* HELLO_WORLD_H */
Lundin
  • 174,148
  • 38
  • 234
  • 367
  • 2
    Thanks! One of those little things that trip programmers up. :) – user1018501 Apr 27 '12 at 20:10
  • 1
    Though the header guards are unnecessary (in this example) it's a good hint – KevinDTimm Apr 27 '12 at 20:20
  • So the different .c files are compiled separately to produce one executable? – redpix_ Dec 26 '14 at 18:08
  • @Lundin whats the use of the so called `header guards` ? – Xsmael Feb 28 '17 at 17:31
  • 1
    @Xsmael To ensuring that multiple C files including the same H file don't run into issues of declaring/defining the same identifiers several times in the same program. The compiler works with "translation units", which is a C file plus all the headers included by that C file. Meaning that the same H file could exist in multiple translation units. – Lundin Mar 01 '17 at 08:31
  • Trying with identical files, in the same directory, and getting the same error. :/ – Riley Fitzpatrick Jan 22 '20 at 21:16
  • Or we can use #pragma once but its **non-standard** but widely supported – HaseeB Mir Sep 11 '21 at 02:17
  • @HaseeBMir There exists no sound reason why you would ever use a non-standard feature which is 100% equivalent to an existing standard feature. – Lundin Sep 11 '21 at 11:41
  • On Visual Studio its by default so we dont have to add Header guards to every .h files out there. Yes i am not recommending this to use just stating it out – HaseeB Mir Sep 11 '21 at 13:35
9

You are not including hello_world.c in compilation.

   gcc hello_world.c main.c   -o main
P.P
  • 112,354
  • 20
  • 166
  • 226
6

You are not linking against hello_world.c.

An easy way to do this is to run this compilation command:

cc -o main main.c hello_world.c

More complicated projects often use build scripts or make files that separate the compilation and linking commands, but the above command (combining both steps) should do fine for small projects.

Tom
  • 17,922
  • 14
  • 67
  • 79
4

You should link object file compled from your second .c file hello_world.c with your main.o

try this

cc -c main.c
cc -c hello_world.c
cc *.o -o hello_world
vard
  • 2,072
  • 4
  • 28
  • 40
1

Ya it seems you have forgotten to link hello_world.c. I will be gcc hello_world.c main.c -o main. If the number of files are less we can use this approach but in larger projects better to use Make files or some compilation scripts.

john
  • 1,311
  • 2
  • 18
  • 30