Results 1 to 2 of 2

Thread: Using Functions Question

  1. #1
    Join Date
    Jan 2013
    Beans
    25

    Using Functions Question

    Hi, I have function say(int x) in sample.c and I wanted to use it in my main.c program. How do I need to call it? I am compiling the program as g++ -o test main.c sample.c -lncurses. When I compile, I get undefined reference to 'say'. Please help. Thanks.

  2. #2
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    1,955
    Distro
    Kubuntu 12.10 Quantal Quetzal

    Re: Using Functions Question

    If it's c, compile with gcc

    typically you use:

    Code:
    --- sample.h ---
    /* just declares the function  */
    int sample(int x);
    -------
    --- sample.c ---
    /* includes the sample.h header file 
       this guarantees that the .h and .c file 
       are coherent */
    #include "sample.h"
    
    /* defines the function  */
    int sample(int x) {
       printf("called sample(%d)\n",x);
       return 0;
    }
    -------
    --- main.c ---
    /* includes the sample.h header file 
       because it needs the declaration */
    #include "sample.h"
    
    /* then uses the function  */
    int main(int argc, char** argv) {
       sample(2);
       return 0;
    }
    -------

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •