Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's with these g++ "multiple definition" errors?

I'm in the early stages (read:just started yesterday) of a project, and I'm setting up my initial makefile. It's very simplistic. Here's the full contents of the file:

all: main.o resource.o
    g++ -o output.exe main.o resource.o

main.o: main.cpp main.h resource.h
    g++ -mwindows -o main.o main.cpp

resource.o: resource.rc
    windres resource.rc resource.o

clean:
    rm *.o 

At this point I think it's important to mention that I'm working on Windows, so I'm doing all of this in either Powershell with MinGW g++ 4.5.2 or else with Cygwin and g++ 3.4.4 (I've tried both; same errors).

Without printing the contents of all the above-listed files, it suffices to say that main.h contains a reference to windows.h and resource.h, main.cpp contains a reference to main.h and a few functions, most importantly an int WINAPI WinMain(...) function and an LRESULT CALLBACK WndProc(...) function, resource.rc contains resource definition statements for a simple menubar with a file menu and a dummy second menu as well as a reference to resource.h, and resource.h simply contains #define statements for the IDs used in resource.rc.

Now, I can compile both resource.rc and main.h separately from each other and get those two nice .o files. However, when I try to link them and produce output.exe, I get the following scary list of errors, none of which I really understand:

main.o:main.cpp:(.text+0x0): multiple definition of `mainCRTStartup'
c:/mingw/bin/../lib/gcc/mingw32/4.5.2/../../../crt2.o:crt1.c:(.text+0x160): first defined here
main.o:main.cpp:(.text+0x0): multiple definition of `WinMainCRTStartup'
c:/mingw/bin/../lib/gcc/mingw32/4.5.2/../../../crt2.o:crt1.c:(.text+0x140): first defined here
collect2: ld returned 1 exit status
make: *** [all] Error 1

The above is generated in Powershell. Cygwin gives a very similar list of errors, still centering around "multiple definition" statements. The multiple definitions seem to be originating in C:/mingw/lib/crt2.o, and possibly another file named crt1.c, which would seem to be more helpful since obviously C is far more human readable than a .o file, but I can't find anything with that title anywhere within the C:\MinGW folder.

Any direction at all would be helpful, because at the moment I'm pretty lost.

EDIT: The only #include statement in main.cpp:

#include "main.h"

The contents of main.h:

#ifndef _MAIN_H_
    #define _MAIN_H_

    #include <windows.h>
    #include "resource.h"

#endif
like image 237
Ken Bellows Avatar asked Jan 23 '11 19:01

Ken Bellows


People also ask

How do you fix multiple definition main errors?

The main function is the entry point for the C++ program execution. The fix for the error is to scan the source files listed in the build log and remove the unwanted main routines in the respective files. We can have one definition of the main function for the project to run.


1 Answers

You're missing the -c argument to g++ when you're compiling a .cpp file to an .o file . As it is now , you're trying to create one executable from another one.

g++ -mwindows -o main.o main.cpp should be g++ -mwindows -c -o main.o main.cpp

While at it, add warning flags as well, at least -Wall

like image 150
nos Avatar answered Oct 22 '22 21:10

nos