This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: need help


On Tue, 2 May 2000, Claudiu BRASOVEAN wrote:

> Hello....
> 
> Please excuse my childish question but I am new in the field and I am lost due to a very simple problem.

Please note that this list is for questions specific to gcc; your question
is a general C question that is not specific to gcc.


> Here is my problem:
> Main program:
> //main.c
> #include <stdio.h>
> #include "funct.h"
> 
>  void main(void){

Note: the return type of main() should always be 'int'.

> 
>  printf("Calling external function\n");
> 
>  foo(3);
> 
>  }
> ////////////////////
> //funct.h
> .....
> extern void foo(int);
> .....
> 
> ////////////////////
> //funct.c
> #include <stdio.h>
> #include "funct.h"
> ..........
> void foo(int x){printf("My parameter was:%d\n",x);};
> ..................
> Here is what i get from gcc (in fact is ld, isn't it?)
> 
> /tmp/ccahK6eu1.o: In function `main':
> /tmp/ccahK6eu1.o(.text+0x29): undefined reference to `foo'

Did you link funct.c with main.c ?

You need to do something like this:
$gcc funct.c main.c
$./a.out

or:

$gcc -c funct.c
$gcc -c main.c
$gcc funct.o main.o
$./a.out

If I compile and link it this way, your code works fine on my
  i686-linux-pc-gnu box with gcc 2.95.2

> 
> I do not understand why ld can not resolve symbol. 

ld can only resolve what it can see. If you do not pass it the name of the
  module, it will know to look in that module for functions.

> The compiling phase works fine
> I do have a SuSE 6.3 Linux distribution whith gcc-2.7.2.3-63 on it.
> 
> Thank you in advance.
>  
> 


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]