2016-07-19 36 views
0

我是Makefile的新手,試圖用它創建一個cpp項目。 現在我只有「hello world」程序(只有main.cpp文件)。 我不能停止想用make編譯時收到此錯誤:出錯:「g ++:error:main.o:沒有這樣的文件或目錄」

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 

我不明白我做錯了,希望得到您的幫助。

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

謝謝你在先進。

+0

似乎沒問題。試着寫一個編譯main.o規則的命令。 – EFenix

+0

我添加了這個規則,它的工作: g ++ -c $(SRCS)/main.cpp 這就是我需要添加的所有東西嗎? – dorash

+0

我想現在可以...順便說一下,使用CXXFLAGS(不是CXXLAGS) – EFenix

回答

1

Makefile文件用來編譯程序,而不必每次都輸入命令行,以避免重新編譯什麼也不需要。

這裏有一個文件的小項目,每次你都會重新編譯你的文件,但是對於大項目,如果你沒有每次重新編譯所有東西,你會節省很多時間(例如if你與一些大型圖書館來源合作)。

所以,你需要改變一點你的Makefile,以避免重新編譯什麼也不需要:與$(CXXFLAGS)(自動

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... 

這樣,你的Makefile會編譯$(OBJS) main.o規則是隱含的,至少在Linux上,我不知道你的Winodws)

親切, JM445

(對不起,我的英語,我是法國人)

+0

謝謝你的回答解釋了很多。 – dorash

1

它需要一個命令波紋管的main.o規則:

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

感謝您的幫助,但我選擇了使用上面的答案。 – dorash

相關問題