Makefile and includes

This is a discussion on Makefile and includes within the C Programming forums, part of the General Programming Boards category; My makefile: Code: main : main.c linked_list.o number.o gcc -o main main.c linked_list.o number.o linked_list.o : linked_list.c gcc -c -o ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    11

    Makefile and includes

    My makefile:

    Code:
    main : main.c linked_list.o number.o
    	gcc -o main main.c linked_list.o number.o
    
    linked_list.o : linked_list.c
    	gcc -c -o linked_list.o linked_list.c
    
    number.o : number.c
    	gcc -c -o number.o number.c
    linked_list.c includes linked_list.h where a struct called LINK_HEAD is defined.
    number.c includes number.h

    main.c includes linked_list.c and number.c

    I have a function in number.h that has a return type of LINK_HEAD

    I get this error
    Code:
    number.h:4: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
    
    Line 4:
    LIST_HEAD *add_two_numbers(LIST_HEAD *list, LIST_HEAD *listTwo);
    I'm assuming that it doesn't know what LIST_HEAD is - is there something wrong with my makefile that's causing this? Or am I including files in the wrong place?

    Thanks in advance,

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Maybe "number.h" needs to include "linked_list.h" or number.c needs to #include "linked_list.h" before "number.h"?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    11
    When I do what you describe, I get errors saying that functions have been declared twice.

    When I do this to main.c

    Code:
    #include "linked_list.h"
    #include "linked_list.c"
    #include "number.h"
    #include "number.c"
    It gives me the errors that number.h had in linked_list.c
    Last edited by Lang; 03-17-2008 at 10:15 PM.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by Lang View Post
    When I do what you describe, I get errors saying that functions have been declared twice.
    http://en.wikipedia.org/wiki/Include_guard
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Lang View Post
    When I do what you describe, I get errors saying that functions have been declared twice.

    When I do this to main.c

    Code:
    #include "linked_list.h"
    #include "linked_list.c"
    #include "number.h"
    #include "number.c"
    It gives me the errors that number.h had in linked_list.c
    Aside from what Dave Sinkula says, you should NOT include .c files in your main.c

    You are compiling number.c and linked_list.c separately, and linking them with main.c, so including the source inside main.c is not only "bad style", but also not consistant with your makefile.

    As a side note:
    If you include a header file in a .c file, the same include file(s) should be listed as dependencies for the source file, e.g.:
    Code:
    main : main.c linked_list.o number.o number.h linked_list.h
    	gcc -o main main.c linked_list.o number.o
    
    linked_list.o : linked_list.c linked_list.h
    	gcc -c -o linked_list.o linked_list.c
    
    number.o : number.c linked_list.h number.h
    	gcc -c -o number.o number.c
    That way, if you change linked_list.h, number.c will be recompiled "automatically".

    There are tools, such as "gcc -m" that will generate a list of dependencies for your source file, and you can then use that as a include in your makefile. But for a small project, that's probably overkill.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Building a project using a Makefile
    By starcatcher in forum Windows Programming
    Replies: 2
    Last Post: 11-23-2008, 11:50 PM
  2. A simple question about writing a Makefile
    By meili100 in forum C++ Programming
    Replies: 3
    Last Post: 04-29-2008, 09:07 PM
  3. Borland 5.5 Makefile woes...
    By gprogga in forum C++ Programming
    Replies: 3
    Last Post: 06-01-2003, 03:20 AM
  4. makefile dependencies (again)
    By *ClownPimp* in forum Linux Programming
    Replies: 2
    Last Post: 05-06-2002, 04:13 AM
  5. makefile dependencies
    By *ClownPimp* in forum C++ Programming
    Replies: 7
    Last Post: 05-01-2002, 12:33 PM

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