Getting bug: 'first defined here'

while using codeblocks to compile, I get the errors
1
2
3
Multiple definition of main
and 
first defined here


These are in the area where
 
int main()

is located. here is the code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <cmath>
using namespace std;
int f1 = 0;
int f2 = 1;
int fRESULT = 0;
int amount = 20;

int main()
{

	std::cout << f1 << ", ";
	std::cout << f2 << ", ";
	for (int i = 0; i < amount; i++)
	{
		fRESULT = f1 + f2;
		std::cout << fRESULT << ", ";
		f1 = f2;
		f2 = fRESULT;
	}
	return 0;
}

please help me fix it.
You have not listed all the information we need.

The reason you are getting that complaint is probably due to one of the libraries you are linking with, or another source file you are #including.
THIS IS ALL THE CODE. SERIOUSLY.
it appears listed as 'multiple definitions of main' on line 9.

on line 10 appears 'first defined here'.
that is all that is going on in this relatively small program.
You probably have some other .cpp file with a main() function in the same project.
I had another file that I deleted, but that didn't work.
could it be because of the stuff between line
15 and 20?
closed account (DEUX92yv)
I don't think lines 15-20 would cause that, but if that is "ALL THE CODE. SERIOUSLY.", why make the variables global? If this is all your program does, you ought to put them in main().

You don't need to include cmath for what this function does.
Also, you don't need to put std:: in front of cout. That's what line 3 does for you.

I'll also note that g++ compiles your exact code without warning or error. I doubt CodeBlocks is playing tricks on you. Can you confirm once again that this is the only file with a main() function in your project?
Last edited on
> You probably have some other .cpp file with a main() function in the same project.
Show the build command
Better yet, start a new, clean project. Make sure it is a "command line" or "console" application. Copy the code you showed us into the editor and save your project. Compile.

There is otherwise nothing wrong with the code you have shown us. If you still get the multiple definition error, then C::B is misconfigured.
Topic archived. No new replies allowed.