3

Sample1.c

#include "stdio.h"
int f1(int x, int y)
{
  printf("%d %d", x, y);
  return x+y;
}

Sample2.c

#include "stdio.h"
extern int f1(int x,int y);
int main(void)
{
  printf(" %d\n", f1(5,6));
  return 0;
}

I was trying to compile the Sample1.c file then Sample2.c on Turbo C/C++ Compiler (Windows XP). It shows the following error:

Compiling Sample2.c:
Linking Sample2.exe:
Linker Error : Undefined Symbol _f1 in module Sample2.c

Can anybody help me on this regard?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278

5 Answers5

5
  1. Do not use Turbo C compiler... it is dead.
  2. You need to link both compiler objects together to single executable.
Artyom
  • 31,019
  • 21
  • 127
  • 215
3

Turbo C really is quite an old product ; you might want to consider an upgrade.

In the C world, you need to compile your individual C files into object files, and then link the object files together. I haven't used Turbo C, but I would guess that there is some mechanism to "add file to project" or something like that to associate multiple files.

Also, while what you have is perfectly legal, on any real product I highly recommend making a header file instead of using extern. With a header file you can declare the same prototype and include it in both .c files, and the compiler will be able to warn you if your prototype doesn't exactly match your function declaration. As you currently have it written, you might won't catch a mistake in the function's arguments, and you would get hard-to-diagnose behavior from that.

Chris Arguin
  • 11,850
  • 4
  • 34
  • 50
1

Are you linking both object files together? What is the command line for the linker?

EFraim
  • 12,811
  • 4
  • 46
  • 62
0

You must compile these two and link them into the same program, was that what you did?

Kim Gräsman
  • 7,438
  • 1
  • 28
  • 41
0

djgpp is a good replacement for tc

it is gcc running dos

Mandrake
  • 363
  • 1
  • 3
  • 11