Thread: compiling makefile - error

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    58

    Cool compiling makefile - error

    hi ,

    I've a problem compiling my makefile.
    the additional files are enclosed.

    the error I get:
    $make ./main
    gcc -ggdb main.c
    /tmp/ccPIxwjP.o: In function `main':
    /home/ilan/Embedded_linux/Lesson-2-Makefiles/lesson-2.1/main.c:6: undefined reference to `func1'
    /home/ilan/Embedded_linux/Lesson-2-Makefiles/lesson-2.1/main.c:7: undefined reference to `func2'
    collect2: error: ld returned 1 exit status
    make: *** [main.o] Error 1
    $


    Code:
    my make file:
    
    main : main.o file1.o file2.o
        gcc -ggdb main.o file1.o file2.o -o gdb-main 
    
    main.o : main.c file1.h file2.h
        gcc -ggdb main.c
    
    file1.o:file1.c file1.h
        gcc -ggdb file1.c
    
    file2.o:file2.c file2.h
        gcc -ggdb file2.c
    
    
    PHONY : clean
    
    clean :
        rm *.o
    thanks
    Ilan
    Attached Files Attached Files

  2. #2
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    ./main isn't a rule in your makefile. You want “make main”.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code:
    main : main.o file1.o file2.o
        gcc -ggdb main.o file1.o file2.o -o gdb-main
     
    main.o : main.c file1.h file2.h
        gcc -c -ggdb main.c
     
    file1.o:file1.c file1.h
        gcc -c -ggdb file1.c
     
    file2.o:file2.c file2.h
        gcc -c -ggdb file2.c
    Your compiled target should have the same name as your make rule.
    Compiling a .c file to a .o file needs the -c flag.
    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.

  4. #4
    Registered User
    Join Date
    Feb 2013
    Posts
    58
    fixed to :
    main : main.o file1.o file2.o
    gcc -ggdb -c main.o file1.o file2.o -o gdb-main

    main.o : main.c file1.h file2.h
    gcc -ggdb -c main.c

    file1.o:file1.c file1.h
    gcc -ggdb -c file1.c

    file2.o:file2.c file2.h
    gcc -ggdb -c file2.c


    PHONY : clean

    clean :
    rm *.o

    the error:

    $make ./main
    gcc -ggdb -c main.c
    gcc -ggdb -c file1.c
    gcc -ggdb -c file2.c
    gcc -ggdb -c main.o file1.o file2.o -o gdb-main
    gcc: warning: main.o: linker input file unused because linking not done
    gcc: warning: file1.o: linker input file unused because linking not done
    gcc: warning: file2.o: linker input file unused because linking not done
    $
    $

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Take a close look at Salem's post. You only need the -c flag when compiling a .c file into a .o file (the compilation proper phase). You don't need it when combining your .o files into the final executable (the linking phase).
    Code:
    main : main.o file1.o file2.o
    gcc -ggdb -c main.o file1.o file2.o -o gdb-main
    Remove the -c parameter from that one line only.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. makefile error
    By shuo in forum Tech Board
    Replies: 4
    Last Post: 11-21-2007, 07:42 AM
  2. compiling error!
    By Nora in forum C++ Programming
    Replies: 3
    Last Post: 12-25-2006, 05:34 AM
  3. makefile error
    By Renegade in forum C++ Programming
    Replies: 0
    Last Post: 07-15-2005, 04:54 PM
  4. Compiling error (Maybe a syntax error)
    By davidvoyage200 in forum C++ Programming
    Replies: 6
    Last Post: 03-27-2003, 10:09 PM
  5. Very odd compiling error...
    By Captain Penguin in forum C++ Programming
    Replies: 5
    Last Post: 09-30-2002, 09:06 PM