Makefiles!

This is a discussion on Makefiles! within the C Programming forums, part of the General Programming Boards category; Can't get my head around them! I have myfunctions.h: Code: #ifndef MYFUNCTIONS_H #define MYFUNCTIONS_H extern int triangularnumber (int number); #endif ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Location
    Glasvegas, Scotland.
    Posts
    68

    Makefiles!

    Can't get my head around them!
    I have myfunctions.h:

    Code:
    #ifndef MYFUNCTIONS_H
    #define MYFUNCTIONS_H
    
    extern int triangularnumber (int number);
    #endif
    myfunctions.c:
    Code:
    #include <stdio.h>
    
    int triangularnumber ( int number )
    {
    int result, i, number_adj;
    result = number;
    for (i = 1; i < number; i++)
    {
    number_adj = number - i;
    result = result + number_adj;
    }
    return result;
    }
    And a program disjointsubset.c:
    Which is quite big but has this line:

    Code:
    if(number_equal >= triangularnumber (no_of_rows_count - 1))
    {
    //code
    }
    My makefile is called Makefile:

    Code:
    CFLAGS=-g
    
    default: disjointsubset
    
    myfunctions.o: myfunctions.c myfunctions.h
    
    disjointsubset.o: disjointsubset.c myfunctions.h
    
    main: disjointsubset.o myfunctions.o
    	gcc -g -o disjointsubset disjointsubset.o myfunctions.o
    clean:
    	rm -f *.o disjointsubset
    backup:
    	tar cf backup.tar disjointsubset.c myfunctions.h myfunctions.c Makefile
    And my error:
    (From terminal)

    Code:
    :>make
    cc   disjointsubset.o   -o disjointsubset
    disjointsubset.o(.text+0x9ed): In function `main':
    /home/shill/cprogramming/disjointsubset.c:184: undefined reference to `triangularnumber'
    collect2: ld returned 1 exit status
    make: *** [disjointsubset] Error 1
    Not sure what i'm doing wrong as this is about as simple as i can get it!
    Can you spot anything i'm doing wrong?
    It all worked when the function was declared inside disjointsubset.c!
    Thanks

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Code:
    #ifndef MYFUNCTIONS_H
    #define MYFUNCTIONS_H
    
    extern int triangularnumber (int number);
    #endif
    I don't think you need extern there (it's just a function prototype, it's not supposed to be have the code in this file).
    And I'm assuming you've #include'd "myfunctions.h" in your big file.

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,246
    default: disjointsubset

    should be

    default: main

    Or better, rename the main target to be disjointsubset.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I suspect you are not including myfunctions.h in your disjointsubset.c. That's what that warning says, anyways.

    tabstop is right, you don't need extern for function prototypes [but there's nothing wrong with having the extern there either - it's optional, and it's best if you are consistant - either always or never!]

    Also, the "default" target should really be called "all", as that's the common target to use for "make this thing"
    Code:
    $ make all
    --
    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.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Also (but not causing any problems), you have:
    Code:
    CFLAGS=-g
    but you never use CFLAGS. Instead you have this:
    Code:
    gcc -g -o disjointsubset disjointsubset.o myfunctions.o
    A better way would be to use your CFLAGS variable (especially once you start writing larger makefiles):
    Code:
    CFLAGS=-g -Wall -c
    LDFLAGS=-o
    
    default: disjointsubset
    
    myfunctions.o: myfunctions.c myfunctions.h
    	gcc $(CFLAGS) myfunctions.c
    
    disjointsubset.o: disjointsubset.c myfunctions.h
    	gcc $(CFLAGS) disjointsubset.c
    
    disjointsubset: disjointsubset.o myfunctions.o
    	gcc $(LDFLAGS) disjointsubset disjointsubset.o myfunctions.o
    clean:
    	rm -f *.o disjointsubset
    backup:
    	tar cf backup.tar disjointsubset.c myfunctions.h myfunctions.c Makefile
    Also turn on some warnings (or all warnings with -Wall).

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You often replace gcc with $(CC) as well.

    It should be noted that Make has built-in rules for compiling C files. It knows that a .o file can be generated from a .c file with
    Code:
    $(CC) $(CFLAGS) -c file.c -o file.o
    So cpjust's Makefile is much more concisely written with
    Code:
    CFLAGS=-g -Wall -c
    LDFLAGS=-o
    
    default: disjointsubset
    
    myfunctions.o: myfunctions.h
    
    disjointsubset.o: myfunctions.h
    
    disjointsubset: disjointsubset.o myfunctions.o
    	gcc $(LDFLAGS) disjointsubset disjointsubset.o myfunctions.o
    clean:
    	rm -f *.o disjointsubset
    backup:
    	tar cf backup.tar disjointsubset.c myfunctions.h myfunctions.c Makefile
    Note that, for example, in the rule disjointsubset.o, you don't have to depend on disjointsubset.c, because that's part of the built-in rule.
    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.

  7. #7
    Registered User
    Join Date
    Oct 2007
    Location
    Glasvegas, Scotland.
    Posts
    68
    Wow, thanks guys, i hadn't included the header file properly, and i've changed the makefile main to disjointsubset.

    Works a treat now.
    And i've copied dwks makefile example and understand it (which is always good)!

    Thanks again everyone.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Makefiles, GCC's -ggdb option, and Valgrind
    By Jesdisciple in forum C Programming
    Replies: 5
    Last Post: 03-14-2009, 04:25 PM
  2. Makefiles
    By TriKri in forum Windows Programming
    Replies: 3
    Last Post: 03-30-2008, 06:57 AM
  3. Benefits of makefiles
    By Ganoosh in forum C++ Programming
    Replies: 2
    Last Post: 06-27-2005, 09:42 PM
  4. programming linux with gcc, emacs, and makefiles
    By Captain Penguin in forum Linux Programming
    Replies: 1
    Last Post: 11-02-2002, 11:04 AM
  5. Help with Makefiles
    By WebmasterMattD in forum Linux Programming
    Replies: 3
    Last Post: 05-24-2002, 08:51 AM

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