Undefined reference to main collect2 error ld returned 1 exit status


What is the undefined reference to the main collect2 error ld returned 1 exit status?

The “undefined reference to main” error is a linker error that occurs when the linker cannot find a definition for the main() function. This can happen for a few reasons:

-The main() function is not defined in any of the source files being compiled.
-The main() function is defined in one or more source files, but it is not declared as extern “C” in any of them.
-The compiler options being used do not include the -lm (link with the math library) option.

How do you fix the undefined reference to main collect2 error ld returned 1 exit status?

The “undefined reference to main” error is a linker error that occurs when the linker cannot find a definition for main() in your program. This can happen for many reasons, but the most common is simply forgetting to include a definition for main(), or forgetting to tell the linker where to find it.

What are some common causes of the undefined reference to main collect2 error ld returned 1 exit status?

There are a few different things that can cause the undefined reference to main collect2 error ld returned 1 exit status. The most common cause is that you have not included the main function in your program. The main function is what tells the program where to start executing code, and without it, the program will not run. Other causes can include forgetting to link in the object files for your program or forgetting to include important libraries.

How can you prevent the undefined reference to main collect2 error ld returned 1 exit status?


The “undefined reference to main” error can be caused by a few different things. The most common reason is that there is no main() function defined in your code. Another possibility is that your main() function is defined but it is not in the same file as your other code. For example, if you have a file called “main.cpp” with your main function and a file called “util.cpp” with utility functions, you will need to compile them both together:

g++ main.cpp util.cpp -o program

If you forget to include util.cpp on the command line, you will get the “undefined reference to main” error.


Leave a Reply

Your email address will not be published.