My C++ linker is failing and I am unable to understand the error: “multiple definition of `main'” [closed]

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 in main.cpp on line 2.
  • You have also defined main() function in new.cpp on line 3.
  • Then, you have also defined main() function in numbersprinting.cpp on line 4.

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 one main() 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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật