Thread: Format and Undefined Symbols error.

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    6

    Format and Undefined Symbols error.

    Hello everyone.

    I am new to C programming and I am following the O'Reilly book called Practical C 3rd Edition.

    I was trying to write a problem that reads a line from the keyboard and reports its length. I am writing my code based on what I have learned in the chapter. Below is the following code I used to do what I mentioned above:

    Code:
    //
    //  length.c
    //  
    //
    //  Created by Manuel Velez on 2/10/12.
    //  Copyright (c) 2012 AEVTech. All rights reserved.
    //
    
    
    #include <stdio.h>
    #include <string.h>
    
    
    char line[100]; // Line we are looking at
    
    
    int main()
    {
        
        printf("Enter a line: ");
        fget(line, sizeof(line), stdin);
        
        printf("The length of the line is: %d\n", strlen(line));
        return (0);
    }
    When I try to build the program with cc I get the following error:

    Code:
    Manuel-Velezs-MacBook-Pro:c_files Manolo$ cc -g -o length length.c
    length.c: In function ‘main’:
    length.c:20: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘size_t’
    length.c:20: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘size_t’
    Undefined symbols for architecture x86_64:
      "_fget", referenced from:
          _main in ccyXoU0g.o
    ld: symbol(s) not found for architecture x86_64
    collect2: ld returned 1 exit status
    I'm not too sure where to go from here. I went back to the chapter and read everything again but I'm just not seeing what I did wrong. I would be thankful for any help possible even if it's just sending me the right way to begin finding why I'm getting this error. Thank you to everyone in advanced!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    First, the function is fgets(), not fget()

    Second, to print a size_t using a new compiler (which you seem to have), you have to be rather more specific.

    Try one of these
    Code:
    printf("The length of the line is: %d\n", (int)strlen(line));
    printf("The length of the line is: %lu\n", (unsigned long)strlen(line));
    printf("The length of the line is: %zd\n", strlen(line));
    The last example is the best one.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    6
    Thank you!!!!! I can't believe I did not see that I was missing a "s" in fgets. I need to get my eyes corrected! lol

    I also used the first print statement you sent and everything works perfect! Thank you so very much for your help. I have also looked up the info on the other ways you did the print statements to get a better understanding as I am sure I am going to run into those again. Thanks!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 07-06-2010, 06:26 PM
  2. Help with Undefined symbols error
    By waterborne in forum C++ Programming
    Replies: 0
    Last Post: 06-16-2010, 11:32 AM
  3. undefined symbols
    By cDev in forum C Programming
    Replies: 6
    Last Post: 09-24-2006, 12:23 PM
  4. ld: undefined symbols
    By StarOrbs in forum C++ Programming
    Replies: 9
    Last Post: 04-08-2005, 03:15 PM
  5. Undefined symbols from ld? ; making bundle; gcc 3.3
    By profxtjb in forum C Programming
    Replies: 5
    Last Post: 06-15-2004, 01:31 AM