user2064676 user2064676 - 3 months ago 19
C Question

Linker error undefined reference to `print'

I have a program named "main.c" containing the main() that calls a function whose definition is available in other source file named "nim.c". I made a header file named "nim.h" that contains the prototype of the required method. This header file "nim.h" is already included it in my "main.c". I am putting up all the files that are part of this program.

//main.c
#include <stdio.h>
#include "nim.h"
int main()
{
print();
return 0;
}
//nim.h
#include<stdio.h>
void print();
//nim.c
#include<stdio.h>

void print()
{
printf("hello !!");
}

Answer

you have to tell the linker, that your executable consist of two object files (main.o and nim.o) along with all the burocratic stuff (like libc, win32, etc).

with gcc you would compile the C-sources:

gcc nim.c -o nim.o
gcc main.c nim.o <libraries> -o main.exe