public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* help regarding linking
@ 2004-11-27  5:14 rganti
  2004-11-27  8:31 ` selvam
  2004-11-27 12:22 ` Patrick Percot
  0 siblings, 2 replies; 4+ messages in thread
From: rganti @ 2004-11-27  5:14 UTC (permalink / raw)
  To: gcc-help


Hi,
      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
a.out
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-------------*/
#include<stdio.h>
#include<stdlib.h>

int main()
{
    float a,b;
    a=1.2;
    b=1.3;
    to_print(a,b);
    return 0;
}
---------------file2.c-------------
#include<stdio.h>
void to_print(float a, float b)
{
    printf("%f , %f \n", a,b);

}



-regards
Radha Krishna Ganti

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: help regarding linking
  2004-11-27  5:14 help regarding linking rganti
@ 2004-11-27  8:31 ` selvam
  2004-11-27 12:22 ` Patrick Percot
  1 sibling, 0 replies; 4+ messages in thread
From: selvam @ 2004-11-27  8:31 UTC (permalink / raw)
  To: rganti, gcc-help

Hello Radha Krishna Ganti,

          I beleive by including a prototype for the function to_print in
file1.c should solve your problem.

          Best Regards,
          Selva.

----- Original Message -----
From: <rganti@nd.edu>
To: <gcc-help@gcc.gnu.org>
Sent: Saturday, November 27, 2004 10:44 AM
Subject: help regarding linking


>
> Hi,
>       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
> a.out
> 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-------------*/
> #include<stdio.h>
> #include<stdlib.h>
>
> int main()
> {
>     float a,b;
>     a=1.2;
>     b=1.3;
>     to_print(a,b);
>     return 0;
> }
> ---------------file2.c-------------
> #include<stdio.h>
> void to_print(float a, float b)
> {
>     printf("%f , %f \n", a,b);
>
> }
>
>
>
> -regards
> Radha Krishna Ganti
>
> --
> Radha Krishna Ganti
> EE-Grad Student
> Univ. Of Notre Dame
>
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: help regarding linking
  2004-11-27  5:14 help regarding linking rganti
  2004-11-27  8:31 ` selvam
@ 2004-11-27 12:22 ` Patrick Percot
  1 sibling, 0 replies; 4+ messages in thread
From: Patrick Percot @ 2004-11-27 12:22 UTC (permalink / raw)
  To: gcc-help

On Sat, 27 Nov 2004 00:14:27 -0500, rganti@nd.edu wrote

> 
> Hi,

Hi Radha,


>       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
> a.out
> 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. 

When  there is  no prototype  for a  function, floats  are automatically
converted  to double  when they  are transmitted  as parameters  to this
function. So, their  size is modified : you can  use the operator sizeof
to compute the size of each type on your machine. 

As your  function to_print  waits for float  (4 bytes) and  the compiler
transmits double (8 bytes),  the function to_print actually prints parts
of the first variable. 

Representation of the Stack :

What is actually on the stack
------------------------------------------------------------------------
|                a                |                 b                  |
------------------------------------------------------------------------

What to_print expects to find
-----------------------------------
|       a        |      b         |
-----------------------------------


[..Zappé 36 lignes et 449 caractères..]

À+
PP
-- 
Groupe Morbihannais d'Utilisateurs de Logiciels Libres http://www.tuxbihan.org
GPG fingerprint = 1A4F E154 3D2C A20E E4CA  A543 7951 C5C2 E44A A0B5

Patrick Percot.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* RE: help regarding linking
@ 2004-11-29 17:24 lrtaylor
  0 siblings, 0 replies; 4+ messages in thread
From: lrtaylor @ 2004-11-29 17:24 UTC (permalink / raw)
  To: rganti, gcc-help

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);

	#endif

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).

Thanks,
Lyle


-----Original Message-----
From: gcc-help-owner@gcc.gnu.org [mailto:gcc-help-owner@gcc.gnu.org] On
Behalf Of rganti@nd.edu
Sent: Friday, November 26, 2004 10:14 PM
To: gcc-help@gcc.gnu.org
Subject: help regarding linking


Hi,
      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
a.out
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-------------*/
#include<stdio.h>
#include<stdlib.h>

int main()
{
    float a,b;
    a=1.2;
    b=1.3;
    to_print(a,b);
    return 0;
}
---------------file2.c-------------
#include<stdio.h>
void to_print(float a, float b)
{
    printf("%f , %f \n", a,b);

}



-regards
Radha Krishna Ganti

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2004-11-29 17:24 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-11-27  5:14 help regarding linking rganti
2004-11-27  8:31 ` selvam
2004-11-27 12:22 ` Patrick Percot
2004-11-29 17:24 lrtaylor

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).