Thread: Undefined Reference to function?

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    6

    Undefined Reference to function?

    Our assignment is to write a program that tests our teacher's code. He has written 4 files for us to "test". However, when I try and compile it, it cant find only 1 of his functions, but the rest it does fine on. Ill attach the relevent part in my code, and how he has his code set up.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "ssm.h"
    #define	DEBUG	1
    int l;
    l = eat_comment(argv[2]);
    The only difference between the rest of the functions, and "eat_comment()" is that it is in the same file as read_route, and ssm.h doesnt mention eat_comment. Is there a way to get it to recognize eat_comment without changing his code?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "ssm.h"
    
    #define VERSION "0.5"
    
    static int eat_comment(FILE *fp);  // eats commnts and white space
    static int line;                   // line in file currently being read.
    
    // int read_route(const char *file, const Route *route) {
    int read_route(const char *file, Route *route) {
    .
    .
    .
    }
    
    // eat_comment() eats white space and commnets, incrementiong
    // the line counter as it goes. If one of the fscanfs above
    // eats a '\n' between parameters, the line count will be off.
    // Perhaps the "line" count is not such a good idea here after all
    // That can be fixed if above '\n' is not accepted as a delimiter
    // between parameters. hummm....
    static int eat_comment(FILE *fp) {
    .
    .
    .
    }
    And the ssm.h part where it includes read_route and write_route :
    Code:
    defines a bunch of variables/structs...
    int read_route(const char *file, Route *route);
    int write_route(const char *file, const Route *route);

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    What you can do is put a prototype for the function somewhere in your code before the "int main()" line. Like this:
    Code:
    static int eat_comment(FILE *fp);
    But you also have a problem with your call:
    Code:
    l = eat_comment(argv[2]);
    You are passing a pointer to char, but eat_comment() takes a pointer to FILE. Once you add the function prototype, the compiler will point this out to you.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >int main()
    Make that:
    Code:
    int main(void)
    Absentmindedly thought I was reading the C++ board.

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    6
    When I just add the prototype up top, it just says its used, but never defined. And yes, ill have to cast the arg someway else.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >When I just add the prototype up top, it just says its used, but never defined.

    >int l;
    >l = eat_comment(argv[2]);
    You do realize this code goes in your main program?

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    6
    When I add the prototype to the header file, and remove the static keyword, it works out.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    >int main()
    Make that:
    Code:
    int main(void)
    Absentmindedly thought I was reading the C++ board.
    () only means "no information available about function arguments" in a function prototype (in C). For a function declaration, it means the same thing as (void). So
    Code:
    int main() {
        return 0;
    }
    is the same as
    Code:
    int main(void) {
        return 0;
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM