Thread: problem with a header, please help make it better

  1. #1
    Registered User
    Join Date
    Aug 2022
    Posts
    40

    problem with a header, please help make it better

    Hi Guys,

    I'm struggling with header files. I'm getting compile errors on the attached two files:

    undefined reference to 'read_line'

    I copied everything right out of the text book and I can't get it to compile. Any insight is greatly appreciated.

    Thank you,
    Attached Files Attached Files

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    So where is your readline.c file?

    Or wherever you've implemented
    Code:
    int read_line(char str[], int n) {
      // do stuff here to fill n chars of str and return an int
    }
    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
    Aug 2022
    Posts
    40
    Hey Salem,

    Thanks for replying. I had a tough time with the chapter that covered header files. I should have figured out my issue back then rather than simply plowing forward. I went back and reviewed the chapter but, still stumped.

    All 3 files (inventory.c, readline.h, readline.c) are in the same directory. Here is the readline.c file:

    Code:
    #include <ctype.h>
    #include <stdio.h>
    #include "readline.h"
    
    
    int read_line(char str[], int n)
    {
        int ch, i=0;
        while (isspace(ch= getchar()))
            ;
        while (ch != '\n' && ch != EOF) {
            if (i < n)
                str[i++] = ch;
            ch = getchar();
        }
        str[i] = '\0';
        return i;
    }
    When I try to compile inventory.c, the error is:

    C:\work\.....\inventory.o:inventory.c|| undefined reference to `read_line'

    Not sure if it matters but, notice the different single quotes around `read_line'. I can't find any place in my code where I used different single quotes ` vs '.

    When I try to compile readline.c, the error is:

    ~ undefined reference to `WinMain'


    Thank you.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Your compilation command line should be something like

    gcc readline.c inventory.c
    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.

  5. #5
    Registered User
    Join Date
    Aug 2022
    Posts
    40
    Thank you. That worked. I write in code in Code::Blocks 20.03. There is probably a special way to execute multifile projects that I'm not aware of. In the future, I will use the CLI as you suggested.

  6. #6
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,106
    You should also add debugging options on the command line. I would run gcc as:
    Code:
    gcc -Wall -Wextra -Wpedantic -g -o inventory readline.c inventory.c
    -g adds debugging information for use by gdb

    -o names the final executable, otherwise a.out is the executable name.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do you make a header file
    By Inkinsarto in forum C++ Programming
    Replies: 12
    Last Post: 05-03-2013, 07:33 AM
  2. Trying to make the best use of header files
    By alanb in forum C++ Programming
    Replies: 4
    Last Post: 08-31-2009, 04:45 PM
  3. How to make my own header/library?
    By Ash1981 in forum C Programming
    Replies: 7
    Last Post: 12-31-2005, 08:01 PM
  4. I want to make a *.lib for my header file
    By tudehopet in forum C++ Programming
    Replies: 6
    Last Post: 05-18-2002, 04:14 AM
  5. Help with Header/Make files
    By Unregistered in forum C Programming
    Replies: 0
    Last Post: 04-10-2002, 10:57 PM

Tags for this Thread