PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : Compilieren und Makefile (GCC)


Ganon
2003-11-08, 23:46:28
Hi.

Wenn ich Funktionen über eine Header-Datei auslagere, wie compiliere ich das dann mit gcc?

Also wenn ich jetzt folgendes habe:

main.cpp

#include <iostream>
#include "Funktion.hpp"

int main()
{
std::cout << Wert() << std::endl;
return 0;
}


Funktion.hpp

int Wert();


Funktion.cpp

#include "Funktion.hpp"

int Wert()
{
return 200;
}


Wie kompiliere ich das ganze jetzt mit gcc?

Und kennt einer ein Tutorial wie man das Ganze mit Hilfe eines Makefiles macht?

Danke!

Gnafoo
2003-11-09, 01:57:13
g++ -c main.cpp
g++ -c Funktion.cpp
g++ main.o Funktion.o -o prog


Bei Makefiles nehme ich immer meine fertigen Vorlagen :)
Vielleicht findest du ja bei Google etwas.

Meine Vorlage sieht folgendermaßen aus .. vielleicht reicht
es dir ja aus, die einfach zu verändern. Für kleinere Sachen
funzt es problemlos.


# Project: MapViewer

# global definitions
CPP = g++
OBJ = main.o
BIN = MapViewer
LIBS = ../Engine/Engine.a -lSDLmain -lSDL -lSDL_image -lGL -lGLU -lz
INCS = -I"../Engine"

# cpp flags
CXXFLAGS = -O2

# targets
all: $(BIN)

clean:
rm -f $(OBJ) $(BIN)

$(OBJ):
$(CPP) -c -o $@ $*.cc $(INCS) $(CXXFLAGS)

$(BIN): $(OBJ)
$(CPP) $(OBJ) -o $(BIN) $(LIBS) $(CXXFLAGS)

Vedek Bareil
2003-11-10, 01:59:55
ich würde das Makefile folgendermaßen machen:

CPP = g++
CPPFLAGS = -I. -c
OBJ = main.o Funktion.o

prog: $(OBJ)
$(CPP) -o prog $(OBJ)

%.o: %.cpp *.h
$(CPP) $(CPPFLAGS) %.cpp

Tom Servo
2003-11-11, 02:03:41
Original geschrieben von Vedek Bareil
ich würde das Makefile folgendermaßen machen:

CPP = g++
CPPFLAGS = -I. -c
OBJ = main.o Funktion.o

prog: $(OBJ)
$(CPP) -o prog $(OBJ)

%.o: %.cpp *.h
$(CPP) $(CPPFLAGS) %.cpp



CPP und CPPFLAGS werden in GNU-Make in Implicite Rules für den C-Preprocessor benutzt. Für den C++-Compiler gibt es CXX und CXXFLAGS.



Variables Used by Implicit Rules
================================

`CXX'
Program for compiling C++ programs; default `g++'.

`CPP'
Program for running the C preprocessor, with results to standard
output; default `$(CC) -E'.

`CXXFLAGS'
Extra flags to give to the C++ compiler.

`CPPFLAGS'
Extra flags to give to the C preprocessor and programs that use it
(the C and Fortran compilers).



Als Makefile reicht folgendes aus:

main.o : main.cpp funktion.hpp
funktion.o : funktion.cpp funktion.hpp

app.exe : main.o funktion.o
$(CXX) $^ -o $@


Wenn man Optionen an den Compiler übergeben will, dann kann man im Makefile der Variable CXXFLAGS diese Optionen zuweisen (mit =, :=, ?= oder +=) oder in der Kommandozeile.

Man könnte es dann so aufrufen:

$ make app.exe
oder
$ make CXXFLAGS="-Wall -O2" app.exe

Es gibt für GNU Make eine gute Dokumentation als GNU-Info Datei. Kann man am komfortabelsten mit Emacs lesen.

edit: irgendwie vergisst man das nur leider sehr schnell alles wieder.

Diese Abhängigkeiten kann man übrigens von GCC erzeugen lassen:

$ g++ -E -M main.cpp funktion.cpp
main.o: main.cpp funktion.hpp
funktion.o: funktion.cpp funktion.hpp

man würde dann im Makefile z.B. schreiben:


sources=main.cpp funktion.cpp

app.exe : main.o funktion.o
$(CXX) $^ -o $@


Makefile.deps :
$(CXX) -E -M $(sources) >Makefile.deps

include Makefile.deps



Damit die Datei Makefile.deps dann auch automatisch neu erzeugt wird, müsste man da noch was ändern (mittels sed). Man müsste sie momentan per Hand löschen wenn sie veraltet ist. Gab da glaube ich auch noch jede Menge andere Probleme.

Die Liste der *.o Dateien kann man übrigens auch automatische erzeugen.


sources=main.cpp funktion.cpp

objects=$(addsuffix .o,$(basename $(sources)))

app.exe : $(objects)
$(CXX) $(LDFLAGS) $^ $(LDLIBS) -o $@


Makefile.deps :
$(CXX) -E -M $(sources) | sed 's/:/ Makefile.deps :/' >Makefile.deps

include Makefile.deps



Manchmal ist das auch alles recht nützlich, aber man kommt auch leicht in Versuchung zuviel automatisieren zu wollen.