gpt4 book ai didi

c++ - 使错误 : "g++: error: main.o: No such file or directory"

转载 作者:行者123 更新时间:2023-11-30 02:30:34 27 4
gpt4 key购买 nike

我是 Makefile 的新手,正在尝试用它创建一个 cpp 项目。现在我只有“hello world”程序(只有 main.cpp 文件)。尝试使用 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

我不明白我做错了什么,非常感谢您的帮助。

这是生成文件:

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

先谢谢你。

最佳答案

Makefile 用于编译程序,无需每次都输入命令行,避免重新编译不需要的内容。

这里有一个文件的小项目,你每次都会重新编译你的文件,但是对于更大的项目,如果你不每次都重新编译所有东西,你会节省很多时间(例如,如果你与一些大型图书馆资源合作)。

因此您需要稍微更改 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...

像那样,您的 makefile 将使用 $(CXXFLAGS) 自动编译 $(OBJS)(main.o 规则至少在 linux 上是隐式的,我不知道 winodws)

亲切地,JM445

(对不起我的英语,我是法国人)

关于c++ - 使错误 : "g++: error: main.o: No such file or directory",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38453518/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com