Why is this error message appearing?
From the last few days, I was trying to run a C++ program but the error is showing. I don’t understand why. I changed the compiler settings after seeing a YouTube video. It was working until the file name was Main.cpp
and when in the next program I gave another name it started showing the error. I do not understand how to read the error messages.
#include <iostream> //iostream allow us to load pre-built libraries.
int main() // main function. Function in c++ is a block of code that does many things.
{
std::cout << "Hello World ! This is my first C++ project" << std:: endl;
std::cout << "This is Anish Sharma";
return 0;
}
Terminal -> Run Task… -> GCC compiler.
Then comes the error message:
* Executing task: Build with GCC compiler 14.2, released [1.8.2024]
Starting build...
cmd /c chcp 65001>nul && C:mingw64bing++.exe -g -std=c++2b C:UsersANISHDocumentsCPP*.cpp -o C:UsersANISHDocumentsCPProoster.exe
C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:UsersANISHAppDataLocalTempccME9uEU.o: in function `main':
C:/Users/ANISH/Documents/CPP/new.cpp:3: multiple definition of `main'; C:UsersANISHAppDataLocalTempcce2cCLP.o:C:/Users/ANISH/Documents/CPP/main.cpp:2: first defined here
C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:UsersANISHAppDataLocalTempccup70G8.o: in function `main':
C:/Users/ANISH/Documents/CPP/numbersprinting.cpp:4: multiple definition of `main'; C:UsersANISHAppDataLocalTempcce2cCLP.o:C:/Users/ANISH/Documents/CPP/main.cpp:2: first defined here
collect2.exe: error: ld returned 1 exit status
Build finished with error(s).
* The terminal process terminated with exit code: -1.
* Terminal will be reused by tasks, press any key to close it.
5
Your compile command:
C:mingw64bing++.exe -g -std=c++2b C:UsersANISHDocumentsCPP*.cpp
Is globbing your directory for C++ files to build. This will attempt to build all files ending with .cpp
in the current directory as a single program. It is up to you to ensure that all of these files together make a valid C++ program.
multiple definition of 'main'
:
This means that the linker encountered an error with multiple definitions of the same function, in this case main
. This is likely happening because you have multiple .cpp
files in your directory with main
functions defined, which isn’t allowed. Functions must be unique across your entire program (this is one of the validity rules discussed above).
I suggest either restructuring your project directory such that only .cpp
files you want combined together are in the folder, or modifying your compile command to stop globbing the .cpp
files, and instead just compile a single file at a time.
It seems that you have multiple .cpp
files in your current directory and you are including them one inside another, but every single one have a main()
function, which is failing the compilation. Your program should have one and only main()
function!
The compiler is clearly saying: “multiple definition of `main’“.
- You have the
main()
function first defined inmain.cpp
on line2
. - You have also defined
main()
function innew.cpp
on line3
. - Then, you have also defined
main()
function innumbersprinting.cpp
on line4
.
And technically, the “compilation” is NOT failing, but the “linking” is failing. Why? Here:
- Notice all the
.o
files in your output (i.e.:ccME9uEU.o
,ccup70G8.o
, etc.). - It means, the “assembling” stage is passing, hence getting the assembler output files. But (pay attention!) when the compiler tries to “link” (the final stage) together all the different assembly outputs into a single executable binary, it finds out that each individual assembly output file has a
main()
function. - And as you may know that in C family languages, this
main()
function is the entry point of your program and as I said earlier, one program should have only onemain()
function. - That is why the compiler doesn’t know which is the “real”
main()
function, which is the entry point of your program, like do you understand!? And the compiler gives up and “gives” the errors.
I hope all that makes sense. Also, you didn’t provided the structure of your project, so I am not sure if you have multiple .cpp
source files. So, I suggested all that from your compiler’s output. It may be helpful if you can use commands like tree
to provide what the structure of your project folder is.
Edited: Also, as the @DXPower said above/below, the cause of this error is the command you are running to compile your program. You are compiling all the .cpp
files as one program, even though they don’t have anything to do with each other.