Thread: linking .o from another directory with makefile

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    5

    linking .o from another directory with makefile

    Good evening. I can't link getch.o from /mainop to a makefile in /getanumber

    Code:
    make: *** No rule to make target `getch.o', needed by `maingetanumber'.  Stop.
    Code:
    maingetanumber : getanumber.o getch.o
        gcc -I ../mainop -o maingetanumber getanumber.o getch.o -Wall -pedantic
    
    getanumber.o : getanumber.c
        gcc -I ../headers -c getanumber.c

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Without seeing your directory structure I can't be sure, but can't you just write this?

    Code:
    maingetanumber : getanumber.o ../mainop/getch.o
        gcc -I ../mainop -o $@ $^ -Wall -pedantic
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    5
    Here's the tree:

    Code:
    ├── atof_version
    ├── atof_version.c
    ├── atof_version.c~
    ├── bitwise
    ├── bitwise.c
    ├── bitwise.c~
    ├── calc.h~
    ├── calc.h.gch
    ├── displaygraphics.h
    ├── displaygraphics.h~
    ├── getanumber
    │   ├── getanumber.c
    │   ├── getanumber.c~
    │   ├── getanumber.o
    │   ├── makefile
    │   └── makefile~
    ├── getanumber.c~
    ├── getanumber.h~
    ├── getch.c~
    ├── getop.c~
    ├── headers
    │   ├── calc.h
    │   └── getanumber.h
    ├── histogram_lengthwords.c
    ├── histogram_lengthwords.c~
    ├── longereightychar.c
    ├── macro_swap
    ├── macro_swap.c
    ├── macro_swap.c~
    ├── mainop
    │   ├── getch.c
    │   ├── getch.c~
    │   ├── getch.o
    │   ├── getop.c
    │   ├── getop.c~
    │   ├── getop.o
    │   ├── mainop.c
    │   ├── mainop.c~
    │   ├── mainop.o
    │   ├── makefile
    │   ├── makefile~
    │   ├── Makefile~
    │   ├── simplecalc
    │   ├── stack.c
    │   ├── stack.c~
    │   └── stack.o
    ├── mainop.c~
    ├── op.c~
    ├── stack.c~
    ├── stringindex
    ├── stringindex.c
    ├── stringindex.c~
    ├── str_reverse
    ├── str_reverse.c
    ├── str_reverse.c~
    ├── strtohex
    ├── strtohex.c
    ├── strtohex.c~
    ├── wordperline
    ├── wordperline.c
    └── wordperline.c~

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Where do you call make from? File names have to be relative to the makefiles position.
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    Jun 2012
    Posts
    5
    Quote Originally Posted by GReaper View Post
    Where do you call make from? File names have to be relative to the makefiles position.
    I'm calling make from the getanumber dir.

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Quote Originally Posted by loaded View Post
    I'm calling make from the getanumber dir.
    So, obviously, getch.o( and everything else not in that directory ), need to be prefixed with the relative or absolute path, eg:
    ../mainop/getch.o
    Devoted my life to programming...

  7. #7
    Registered User
    Join Date
    Jun 2012
    Posts
    5
    Quote Originally Posted by GReaper View Post
    So, obviously, getch.o( and everything else not in that directory ), need to be prefixed with the relative or absolute path, eg:
    ../mainop/getch.o
    Sorry, I didn't understand. You mean I need to include all the .c? but I just need the .o of getch.c since the getanumber.c only use two functions from this source. Hum, the header calc.h declares the functions of getch.c and the others functions that are inside mainop directory. Still, I'd like to compile getch only.

    I tried some variations but it didn't work. gcc -I ../mainop -o maingetanumber getanumber.o -include ../mainop/getch.o -Wall -pedantic

    Code:
     
      maingetanumber : getanumber.o getch.o
        gcc -include ../mainop/getch.o -o maingetanumber getanumber.o -Wall -pedantic
    
    getanumber.o : getanumber.c
        gcc -I ../headers -c getanumber.c
    Code:
     
      maingetanumber : getanumber.o getch.o
         gcc -I ../mainop -o maingetanumber getanumber.o ../mainop/getch.o -Wall -pedantic
     
     getanumber.o : getanumber.c
         gcc -I ../headers -c getanumber.c
    Code:
    gcc -I ../mainop -o maingetanumber getanumber.o -include ../mainop/getch.o -Wall -pedantic

  8. #8
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    it didn't work
    That's not much information. Did you get the same error as before?

    Do you need a rule in the makefile for getch.o?
    Code:
    getch.o : getch.c ../headers/calc.h
        gcc -I ../headers -c getch.c
    If getanumber.c includes calc.h then calc.h should also be a dependency for getanumber.c.

    And it might be easier if your makefile was at the top level instead of in mainop.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  9. #9
    Registered User
    Join Date
    Jun 2012
    Posts
    5
    Thank you for your help and sorry I forgot to write main earlier. An error showed me this:

    Code:
    /usr/lib/gcc/x86_64-linux-gnu/4.4.3/../../../../lib/crt1.o: In function `_start':
    (.text+0x20): undefined reference to `main'
    collect2: ld returned 1 exit status
    make: *** [maingetanumber] Error 1
    This code did the trick:

    Code:
    getanumberprog : getanumber.o ../mainop/getch.o maingetanumber.o
        gcc -o progetanumber maingetanumber.o getanumber.o ../mainop/getch.o -Wall -pedantic
    
    maingetanumber.o : maingetanumber.c
        gcc -I ../headers -c maingetanumber.c
    
    getanumber.o : getanumber.c
        gcc -I ../headers -c getanumber.c
    I even didn't need to create another getch.o .
    Last edited by loaded; 06-19-2012 at 10:38 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linking to libraries in the current directory
    By Thiago in forum C Programming
    Replies: 7
    Last Post: 06-09-2010, 07:11 PM
  2. Replies: 2
    Last Post: 08-11-2009, 06:45 AM
  3. Multi-directory Makefile
    By reelbigtim in forum C Programming
    Replies: 3
    Last Post: 10-29-2007, 10:16 AM
  4. difference makefile makefile.am makefile.in
    By Bargi in forum Linux Programming
    Replies: 7
    Last Post: 10-28-2007, 02:08 PM
  5. help: makefile: directory rules
    By paulur in forum C Programming
    Replies: 5
    Last Post: 03-19-2006, 11:43 PM