user2064676 user2064676 - 1 year ago 174
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.

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

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

Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download