0

I'm working on some assignement and basically we're being introduced to splitting our files up. So far, we have only really dealt with one main file.

So, I have three files - main.c, function1.c, function2.c and a header file called header.h.

According to the instructions given to us, the header file is to contain the function prototypes of the actual functions coded in function1.c and in function2.c

We are then to include the header file in each .c files.

Inside the main.c file, the two functions that were coded in function1.c and function2.c are called. However, I am getting an error saying,

main.c:(.text+0xec): undefined reference to `func1'
main.c:(.text+0x110): undefined reference to `func2'

I'm not too sure why this is happening.

I'm running Ubuntu 16.04 LTS

melpomene
  • 84,125
  • 8
  • 85
  • 148
FShiwani
  • 181
  • 1
  • 10

2 Answers2

1

This looks like the linker complains about not finding the two functions.

Using function prototypes, is only a way of saying the compiler that the implementation of a function will be aviable when linking together in the final program. Gcc will then produce an object file that contains the compiled code of main and the information that the program still needs the addresses of some functions to be able to be executed. The linker (a program called by gcc) will then put together the object file of main.c with the ones of your two functions and insert the adresses of these in the machine code of your main program at the right place.

You have to compile your program using

gcc -o main main.c function1.c function2.c

so that the actual implementation of the two functions will be aviable at linking time.

sannaj
  • 360
  • 2
  • 8
0

Try this in GCC compiler,

gcc main.c function1.c function2.c
msc
  • 33,420
  • 29
  • 119
  • 214