Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linker error: _main already defined in *.obj

The following code structure:

ArrayStack.h

#ifndef ARRAY_STACK_H
#define ARRAY_STACK_H
#include "Array.h"
// class ArrayStack
#endif

ArrayStack.cpp

#include "ArrayStack.h"
// ArrayStack's methods

Array.h

#ifndef ARRAY_HEADER
#define ARRAY_HEADER
#include <iostream>
// class Array
#endif

Array.cpp

#include "Array.h"
// Array's methods

main.cpp

#include "ArrayStack.h"
int main() {
    return 0;
}

generates these errors:

LNK1169 one or more multiply defined symbols found

LNK2005 _main already defined in Array.obj

What's the problem here? Please do note that Array.cpp did have int main() defined in itself when it was included in the project for the first time, but no longer has it (neither does the ArrayStack.cpp). Also, the code compiles just fine when the int main() in main.cpp is omitted...

like image 954
Stefan Stanković Avatar asked Sep 27 '22 01:09

Stefan Stanković


1 Answers

The error message means that in all the compiled code, the *.obj files, the linker finds more than one main() function. One is obviously in main.cpp.

The first solution that comes to mind, as mentioned in comments, is to (enforce) re-compile by somehow deleting the *.obj files.

When this doesn't change anything try to rebuild your solution separately from scratch. Start with main.cpp without the include. Then successively add files where you are confident that you won't get errors. Maybe you have to comment out some lines in some cases to make compilation possible.

like image 130
TobiMcNamobi Avatar answered Oct 19 '22 00:10

TobiMcNamobi