Jūs esateŽurnalai / Ernestas Kardzys's blog / Makefile example

Makefile example


ParašėErnestas Kardzys - 2009 Birželio 10

Some time ago I created my first Makefile. Now I have an Embedded Linux lecture. And the first task is to create a Makefile for the program. I hope this will be useful. So, I have two source files:
/* first.c */ #include "all_func.h" int foo() { printf("int foo()\r\n"); } /* second.c */ #include "all_func.h" void foo2() { printf("void foo2()\r\n"); }
And I also have a header file:
/* all_func.h */ #ifndef ALL_FUNC_H #define ALL_FUNC_H #include int foo(); void foo2(); #endif
I also need one file for testing my functions:
/* test.c */ #include "all_func.h" int main() { foo(); foo2(); }
So, I need to build all this mess. Solution: gcc first.c second.c test.c -o test. The problem: if I'll have more files the gcc line will be too huge. Solution: create a makefile. The problem: it takes some time to create one if you don't use IDE for creating your project. Well, let's make a Makefile. It looks like that:
# File: Makefile # Makefile for my Embedded Linux assignment CC = gcc CFLAGS = -Wall CMDTEST = test_all LIBOBJS = first.o second.o CMDOBJS = test.o $(LIBOBJS) # Make a test application $(CMDTEST): $(CMDOBJS) $(CC) $(CFLAGS) $(CMDOBJS) -o $@ # Make object file from the first file first.o: first.c all_func.h $(CC) $(CFLAGS) -c first.c -o $@ # Make object file from the second file second.o: second.c all_func.h $(CC) $(CFLAGS) -c second.c -o $@ clean: -rm -f *.o -rm -f $(LIBRARY) -rm -f $(CMDTEST)
It works! Try make or make clean :)

Skelbti naują komentarą

Šio laukelio turinys bus laikomas privatus ir nerodomas viešai.
  • Web puslapiu adresai ir el. pašto adresai automatiškai tampa nuorodomis.
  • Leidžiamos HTML žymės: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Linijos ir paragrafai atskiriami automatiškai
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. The supported tag styles are: <foo>, [foo].

Daugiau informacijos apie teksto formatavimą

CAPTCHA
Šis klausimas yra skirtas įsitikinti, jog jūs esate žmogus, ir sustabdyti automatinį šlamšto siuntimą.
5 + 1 =
Išspręskite šią paprastą matematinę užduotį ir įveskite atsakymą. Pvz., jei užduotis yra 1+3, įveskite 4.