help regarding linking
Mon Nov 29 17:24:00 GMT 2004

When putting your code in different source files, you should create
header files that contain the declarations of functions and variables
that you want to have access to in other modules.  So, in this case, you
would create a header file named, perhaps, file2.h that contains the
prototype of your to_print function defined in file2.c:

	#ifndef FILE2_H
	#define FILE2_H

	void to_print(float a, float b);


Then you would include file2.h in file1.c:

	#include "file2.h"

Note the #ifdef, etc., around your declaration.  Put all the guts of
your header file between those preprocessor directives.  That
effectively keeps the header file from being processed more than once if
it happens to get included more than once (which happens easily and
perhaps frequently in large projects).


-----Original Message-----
From: [] On
Behalf Of
Sent: Friday, November 26, 2004 10:14 PM
Subject: help regarding linking

      I am a newbie at using gcc.  I wanted to write functions and the
main file
in different files

 I am using the following to compile
gcc -c file1.c
gcc  -c file2.c
gcc file1.o file2.o
2.00000 1.90000

I am not able to understand why that is the output. But if write the
function to_print in file1.c and compile, it works normally.
If I replace all floats  in both files by double it works

What is happening? Am I compiling them wrong.

/*----------- file1.c-------------*/

int main()
    float a,b;
    return 0;
void to_print(float a, float b)
    printf("%f , %f \n", a,b);


Radha Krishna Ganti

Radha Krishna Ganti
EE-Grad Student
Univ. Of Notre Dame

More information about the Gcc-help mailing list