Archyvas

Įrašo tag'ai: ‘makefile’

Makefile example

Birželis 10th, 2009 Ernestas Kardzys Nėra komentarų

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 :)

makefile pavyzdys

Vasaris 20th, 2008 Ernestas Kardzys 4 komentarai

Jeigu programuojate su C/C++, turbūt dažnai tenka nurodyti daugybę C++ failų g++ programai (kažkas tokio: g++ failas.cpp kitas_failas.cpp desimtas_failas.cpp ir t.t.). Nepatogu.

Problemą padės išspręsti makefile failiukas, kuris įvedus make tvarkingai sukompiliuos programą.

CC=g++
CFLAGS=-c -Wall
LDFLAGS=
SOURCES=failas.cpp kitas_failas.cpp dar_vienas_failas.cpp paskutinis_failas.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=programos_pavadinimas

all: $(SOURCES) $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS)
$(CC) $(LDFLAGS) $(OBJECTS) -o $@

.cpp.o:
$(CC) $(CFLAGS) $< -o $@

clean:
rm -rf *o
programos_pavadinimas

SOURCES eilutėje, atskirdami tarpu, išvadinkite savo kompiliuojamus failus, EXECUTABLE eilutėje įrašykite sukompiliuotos programos vardą (komanda: g++ -o programos_vardas). Kai norėsite išvalyti katalogą nuo objektinių failų, įveskite make clean

Visas smagumas atrodo maždaug taip:

$ cd a048/

$ make
g++ -c -Wall a048.cpp -o a048.o
g++ -c -Wall sorting.cpp -o sorting.o
g++ -c -Wall actions.cpp -o actions.o
g++ -c -Wall filesystem.cpp -o filesystem.o
g++  a048.o sorting.o actions.o filesystem.o -o a048

$ ./a048 

$ make clean