[英]make error: “g++: error: main.o: No such file or directory”
I am new to Makefile and trying to create a cpp project with it. 我是Makefile的新手,并试图用它创建一个cpp项目。 Now I have only "hello world" program (only main.cpp file).
现在我只有“hello world”程序(只有main.cpp文件)。 I can't stop getting this error when trying to compile with make:
尝试使用make编译时,我无法停止收到此错误:
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
I don't understand what I am doing wrong, would appreciate your help. 我不明白我做错了什么,谢谢你的帮助。
This is the Makefile: 这是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)
Thank you in advanced. 先谢谢你。
Makefile is used to compile programs without having to enter the command line each time AND to avoid re-compiling what doesn't need to. Makefile用于编译程序,而不必每次都输入命令行,以避免重新编译不需要的内容。
Here with a little project of one file, you will re-compile your file each time, but with larger project, you will save a lot of time if you doesn't re-compile everything every time (for example if you work with some big library sources). 这里有一个文件的小项目,你每次都会重新编译你的文件,但是对于更大的项目,如果你不是每次都重新编译所有内容,你将节省大量的时间(例如,如果你使用一些文件)大图书馆来源)。
So you need to change a little your Makefile to avoid re-compiling what doesn't need to: 因此,您需要更改Makefile以避免重新编译不需要的内容:
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...
Like that, your makefile will compile $(OBJS) automatically with $(CXXFLAGS) (main.o rule is implicit at least on linux, I don't know for winodws) 像这样,你的makefile将自动编译$(OBJS)$(CXXFLAGS)(main.o规则至少在linux上隐含,我不知道winodws)
Cordially, JM445 亲切的,JM445
(Sorry for my English, I'm french) (抱歉我的英文,我是法国人)
It needs a command bellow the main.o rule: 它需要一个命令,然后是main.o规则:
main.o : $(SRCS)/main.cpp
$(CC) $(CXXFLAGS) -c -o $@ $^
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.