LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   directory access problem when compile .c into .o using GNU make (http://www.linuxquestions.org/questions/programming-9/directory-access-problem-when-compile-c-into-o-using-gnu-make-479049/)

George2 08-31-2006 05:36 AM

directory access problem when compile .c into .o using GNU make
 
Hello everyone,


I am meeting with a problem when using GNU make to compile two .c files into related .o files.

My situation is, foo.c is under foo sub-directory and goo.c is inder goo sub-directory. I want to put both output foo.o and goo.o into debug sub-directory.

Currently, I have written the following statements in Makefile,

SRC = foo/foo.c goo/goo.c
OBJ = debug/foo.o debug/goo.o

But I am confused about how to write a rule to compile them, since the source files are in different directories and the output directory for .o files are in another directory (debug).

I think I can write two rules, one for foo/foo.c --> debug/foo.o and the other for goo/goo.c --> debug/goo.o, but I think it is not a good way if I have a lot of source files and seems stupid.

Could anyone help please?


thanks in advance,
George

KenJackson 08-31-2006 06:42 AM

Try an implicit rule:
Code:

%.o : %.c
(TAB)$(CC) -c $(CCFLAGS) -o debug/$(F@) $<

%.o and %.c match an object file and the corresponing source file.
$(F@) is the filename portion of the target.
$< is the first prerequisite.

BTW, here is the GNU make manual.

George2 08-31-2006 11:26 AM

Cool, KenJackson!


Quote:

Originally Posted by KenJackson
Try an implicit rule:
Code:

%.o : %.c
(TAB)$(CC) -c $(CCFLAGS) -o debug/$(F@) $<

%.o and %.c match an object file and the corresponing source file.
$(F@) is the filename portion of the target.
$< is the first prerequisite.

BTW, here is the GNU make manual.

Let me try it!


regards,
George


All times are GMT -5. The time now is 05:58 AM.