2016-07-19 37 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

相关问题