Function calling problem

This is a discussion on Function calling problem within the C Programming forums, part of the General Programming Boards category; Hey guys, Im new to c and im having trouble calling a function from a file "that.c" and im making ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    1

    Function calling problem

    Hey guys,
    Im new to c and im having trouble calling a function from a file "that.c" and im making the call from a file "this.c" and they both have "common.h" in common

    i dont know what im doing wrong

    that.c
    Code:
    #import "common.h"
    #import <stdio.h>
    
    void sayHello(){
        printf("hello");
    }

    this.c
    Code:
    #import "common.h"
    #import <stdio.h>
    
    int main(void){
        sayHello();
    }
    and finally
    common.h
    Code:
    #ifndef HEADER_FILE
    #define HEADER_FILE
    
    // prototype
    void sayHello();
    
    
    #endif;
    and the error im getting is
    Code:
    $gcc this.c 
    /tmp/ccWzgg44.o: In function `main':
    this.c:(.text+0x12): undefined reference to `sayHello'
    collect2: ld returned 1 exit status
    please help

  2. #2
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    169
    Move the prototype from the header to that.c and create another in this.c with an extern.
    Code:
    extern void sayHello();

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Scarlet7 View Post
    Move the prototype from the header to that.c and create another in this.c with an extern.
    That won't help if that.c is not somehow #included in this.c, which if it were, you probably won't have to add anything.

    I would move the contents of that.c to "common.h", you do not have to have the prototype and the definition in separate files.
    Last edited by MK27; 08-06-2009 at 10:55 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    60
    Replace #import by #include

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    As has been danced around, your "gcc this.c" is wholly inadequate for compiling that.c, since that.c is not mentioned anywhere. Your command line needs to have all the necessary .c files on it.

  6. #6
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by tabstop View Post
    As has been danced around, your "gcc this.c" is wholly inadequate for compiling that.c, since that.c is not mentioned anywhere. Your command line needs to have all the necessary .c files on it.
    or .o files. You could compile these one at a time using -c for each, then at the end link the .o files together.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM
  4. problem with function calling
    By Andystudent in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2004, 07:12 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21