C++ 目标“Project1.exe”的配方失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23084708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:18:44  来源:igfitidea点击:

recipe for target 'Project1.exe' failed

c++cmakefile

提问by Varun Ajay Gupta

I try to compile a simple c file in Dev-C++and it shows an error on line 25 C:\Users\varun\Desktop\cprog\Makefile.win recipe for target 'Project1.exe' failed.

我尝试编译一个简单的 c 文件Dev-C++,它在第 25 行显示错误 C:\Users\varun\Desktop\cprog\Makefile.win recipe for target 'Project1.exe' failed.

Makefile.win

生成文件.win

# Project: Project1
# Makefile created by Dev-C++ 5.6.2

CPP      = g++.exe
CC       = gcc.exe
WINDRES  = windres.exe
OBJ      = main.o Untitled2.o
LINKOBJ  = main.o Untitled2.o
LIBS     = -L"C:/Program Files (x86)/Dev-Cpp/MinGW64/lib" -L"C:/Program Files (x86)/Dev-Cpp/MinGW64/x86_64-w64-mingw32/lib" -static-libgcc
INCS     = -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/include" -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include" -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.8.1/include"
CXXINCS  = -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/include" -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include" -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.8.1/include" -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.8.1/include/c++"
BIN      = Project1.exe
CXXFLAGS = $(CXXINCS) 
CFLAGS   = $(INCS) 
RM       = rm.exe -f

.PHONY: all all-before all-after clean clean-custom

all: all-before $(BIN) all-after

clean: clean-custom
    ${RM} $(OBJ) $(BIN)

$(BIN): $(OBJ)
    **$(CC) $(LINKOBJ) -o $(BIN) $(LIBS)**

main.o: main.c
    $(CC) -c main.c -o main.o $(CFLAGS)

Untitled2.o: Untitled2.c
    $(CC) -c Untitled2.c -o Untitled2.o $(CFLAGS)

Errors

错误

C:\Users\varun\Desktop\cprog\Untitled2.o    Untitled2.c:(.text+0x0): multiple definition of `main'
C:\Users\varun\Desktop\cprog\main.o main.c:(.text+0x0): first defined here
C:\Users\varun\Desktop\cprog\collect2.exe   [Error] ld returned 1 exit status
25      C:\Users\varun\Desktop\cprog\Makefile.win   recipe for target 'Project1.exe' failed

回答by Jan Hudec

The important bit of the output is:

输出的重要部分是:

C:\Users\varun\Desktop\cprog\Untitled2.o    Untitled2.c:(.text+0x0): multiple definition of `main'

It tells you, that you have two definitions of function main. You need exactly one such definition in an executable. The next line of the error tells you, where that definition is:

它告诉你,你有两个函数定义main。在可执行文件中您只需要一个这样的定义。错误的下一行告诉您,该定义在哪里:

C:\Users\varun\Desktop\cprog\main.o main.c:(.text+0x0): first defined here

So you have function maindefined in Untitled2.cand you have another function maindefined in main.c. Delete one of them. From the names perhaps the main.cis unnecessary altogether, but I can't tell without seeing the files.

所以你main定义了函数Untitled2.c,你main定义了另一个函数main.c。删除其中之一。从名称main.c上看,可能完全没有必要,但如果没有看到文件,我就无法判断。