2016-07-19 16 views
0

Ich bin neu in Makefile und versuche, ein cpp-Projekt damit zu erstellen. Jetzt habe ich nur "Hallo Welt" Programm (nur main.cpp Datei). Ich kann nicht aufhören diesen Fehler, wenn sie mit machen, um zu kompilieren versuchen:Fehler machen: "g ++: Fehler: main.o: Keine solche Datei oder Verzeichnis"

g++ -std=c++0x -g -Wall -o sub_game main.o 
g++: error: main.o: No such file or directory 
g++: fatal error: no input files 
compilation terminated. 
Makefile:38: recipe for target 'sub_game' failed 
make: *** [sub_game] Error 4 

Ich verstehe nicht, was ich falsch mache, würde Ihre Hilfe zu schätzen wissen.

Dies ist das Makefile:

# the compiler: gcc for C program, define as g++ for C++ 
CC = g++ 

# compiler flags: 
# -g adds debugging information to the executable file 
# -Wall turns on most, but not all, compiler warnings 
CXXLAGS = -std=c++0x -g -Wall 

# the build target executable: 
TARGET = sub_game 

# define any libraries to link into executable: 
LIBS = -lpthread -lgtest 

# define the C source files 
SRCS = ../src 

# define the C object files 
# 
# This uses Suffix Replacement within a macro: 
# $(name:string1=string2) 
#   For each word in 'name' replace 'string1' with 'string2' 
# Below we are replacing the suffix .cc of all words in the macro SRCS 
# with the .o suffix 
# 
#OBJ = $(SRCS)/main.cc 
OBJ = main.o 

# define any directories containing header files other than /usr/include 
# 
INCLUDES = -I../include 

all : $(TARGET) 

$(TARGET) : $(OBJ) 
      $(CC) $(CXXLAGS) -o $(TARGET) $(OBJ) 

main.o : $(SRCS)/main.cpp 

.PHONY : clean 
clean : 
    rm $(TARGET) $(OBJ) 

Sie in fortgeschrittenen Dank.

+0

Es scheint, um in Ordnung zu sein. Versuchen Sie, einen Befehl für die Kompilierung unter main.o-Regel zu schreiben. – EFenix

+0

Ich habe die Regel hinzugefügt und es funktioniert: g ++ -c $ (SRCS) /main.cpp Ist das alles, was ich hinzufügen muss? – dorash

+0

Ich nehme an, es funktioniert jetzt ... Übrigens, verwenden Sie CXXFLAGS (nicht CXXLAGS) – EFenix

Antwort

1

Makefile verwendet wird, Programme zu kompilieren, ohne die Befehlszeile jedes Mal eingeben zu müssen und neu kompilieren zu vermeiden, was nicht benötigt.

Hier mit einem kleinen Projekt von einer Datei, kompilieren Sie Ihre Datei jedes Mal neu, aber mit einem größeren Projekt sparen Sie viel Zeit, wenn Sie nicht jedes Mal alles neu kompilieren (zum Beispiel wenn Sie arbeiten mit einigen großen Bibliotheksquellen).

So müssen Sie ein wenig Ihre Makefile ändern zu vermeiden Wieder kompilieren, was man nicht braucht:

SRCS = ../src/main.cpp\ #Put here the relative path to your .cpp 
     ../src/exemple_second_file.cpp 

OBJS = $(SRCS:.cpp=.o) # Here you get the .o of every .cpp 

TARGET = sub_game # The executable name 

CC = g++ 

CXXFLAGS = std=c++0x -g -Wall 

LIBS = -lpthread -lgtest 

all: $(TARGET) 

$(TARGET): $(OBJS) # This line will compile to .o every .cpp which need to be (which have been modified) 
      $(CC) -o $(TARGET) $(OBJS) $(LIBS) # Linking (no need to CXXFLAGS here, it's used when compiling on previous line 

ETC... # And so on... 

Wie, dass Ihr Make-Datei automatisch $ (OBJS) kompiliert mit $ (CXXFLAGS) (main.o Regel ist zumindest auf linux implizit, ich weiß nicht, für winodws)

Herzlichst, JM445

(Sorry für mein Englisch, ich bin französisch)

+0

Vielen Dank, dass die Antwort viel erklärt hat. – dorash

1

Es braucht einen Befehl Gebrüll der main.o Regel:

main.o : $(SRCS)/main.cpp 
    $(CC) $(CXXFLAGS) -c -o [email protected] $^ 
+0

Vielen Dank für Ihre Hilfe, aber ich entschied mich, die Antwort oben zu verwenden. – dorash

Verwandte Themen