multiple definition



  • Hallo,

    ich bekomme die Fehlermeldung "multiple definition", kann mir aber nicht erklären warum. Hat jemand eine Idee?
    Ich habe folgende Dateien:

    //main.cpp
    
    #include <windows.h>
    #include "fenster1.cpp"
    
    int APIENTRY WinMain(   HINSTANCE   hInstance,
                            HINSTANCE   hPrevInstance,
                            LPSTR       lpCmdLine,
                            int         nCmdShow)
    {
    	MSG msg;
    	MyRegisterClass(hInstance);
    	if (!InitInstance (hInstance, nCmdShow))
    	{
    		return false;
    	}
    	while (GetMessage(&msg, NULL, 0, 0)!=0)
    	{
    		DispatchMessage(&msg);
    	}
    	return msg.wParam;
    }
    
    //fenster1.h
    
    #include <windows.h>
    
    bool	InitInstance(HINSTANCE, int);
    LRESULT	CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    int	MyRegisterClass(HINSTANCE);
    void	NewPaint(HWND);
    
    //fenster1.cpp
    
    #include "fenster1.h"
    
    int MyRegisterClass(HINSTANCE hInstance)
    {
    	WNDCLASSEX wcex;
    	wcex.cbSize =			sizeof(WNDCLASSEX);
    	wcex.style = 			CS_HREDRAW | CS_VREDRAW;
    	wcex.lpfnWndProc = 		(WNDPROC)WndProc;
    	wcex.cbClsExtra = 		0;
    	wcex.cbWndExtra = 		0;
    	wcex.hInstance = 		hInstance;
    	wcex.hIcon = 			0;
    	wcex.hCursor = 			LoadCursor(NULL, IDC_ARROW);
    	wcex.hbrBackground = 	(HBRUSH)(COLOR_BTNSHADOW);
    	wcex.lpszMenuName = 	NULL;
    	wcex.lpszClassName = 	"Einfach";
    	wcex.hIconSm = 			0;
    	return RegisterClassEx(&wcex);
    }
    
    bool InitInstance(HINSTANCE hInstance, int nCmdShow)
    {
    	MessageBox(NULL, "Blubb", "Titel", MB_OK + MB_ICONINFORMATION);
    	HWND hWnd;
    	hWnd = CreateWindowEx(0, "Einfach", "MiniX", WS_VISIBLE + WS_OVERLAPPED + WS_MINIMIZEBOX + WS_CAPTION + WS_SYSMENU, CW_USEDEFAULT, 0, 300, 300, NULL, NULL, hInstance, NULL);
    	if (!hWnd)
    	{
    			return false;
    	}
    	ShowWindow(hWnd, nCmdShow);
    	return true;
    }
    
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	switch (message)
    	{
    		case WM_PAINT:
    			NewPaint(hWnd);
    			break;
    		case WM_DESTROY:
    			MessageBox(NULL, "Beenden", "Titel", MB_OK + MB_ICONINFORMATION);
    			PostQuitMessage(0);
    			break;
    		default:
    			return DefWindowProc(hWnd, message, wParam, lParam);
    	}
    	return 0;
    }
    
    void NewPaint(HWND hWnd)
    {
    	PAINTSTRUCT ps;
    	HDC hDC;
    	hDC = BeginPaint(hWnd, &ps);
    	{
    		TextOut(hDC, 10, 10, "test", sizeof("test") - 1);
    	}
    	EndPaint(hWnd, &ps);
    }
    

    Der genau Build-Log lautet wie folgt:

    Project : Win32 Application
    Compiler : GNU GCC Compiler (called directly)
    --------------------------------------------------------------------------------
    Switching to target: default
    Compiling: main.cpp
    Compiling: fenster1.cpp
    Linking executable: Win32GUI.exe
    .objs\fenster1.o:fenster1.cpp:(.text+0x0): multiple definition of MyRegisterClass(HINSTANCE__*)' .objs\\main.o:main.cpp:(.text+0x0): first defined here .objs\\fenster1.o:fenster1.cpp:(.text+0x80): multiple definition ofInitInstance(HINSTANCE__, int)'
    .objs\main.o:main.cpp:(.text+0x80): first defined here
    .objs\fenster1.o:fenster1.cpp:(.text+0x148): multiple definition of \_Z7WndProcP6HWND\_\_jjl@16' .objs\\main.o:main.cpp:(.text+0x148): first defined here .objs\\fenster1.o:fenster1.cpp:(.text+0x1de): multiple definition ofNewPaint(HWND__
    )'
    .objs\main.o:main.cpp:(.text+0x1de): first defined here
    collect2: ld returned 1 exit status
    Process terminated with status 1 (0 minutes, 1 seconds)

    VIELEN DANK FÜR HILFE!!!



  • Man inkludiert keine Quellcodedateien:

    #include "fenster1.cpp"
    

    ➡

    #include "fenster1.h"
    


  • Super! Vielen Dank! (Ich bitta an dieser Stelle nochmals meine Signatur zu beachten)

    Eine allgemeinere Frage hätte ich aber doch noch. Wie findet der Linker die fenster1.cpp? Nur anhand der selbigen Namen?

    Vielen Dank!!!



  • ichbinanfaenger schrieb:

    Eine allgemeinere Frage hätte ich aber doch noch. Wie findet der Linker die fenster1.cpp? Nur anhand der selbigen Namen?

    Nö, die befindet sich ja mit in deinem Projekt 😉 . Daher weißer das sie dazu gehört 😉 .



  • außerdem solltest du Include Guards verwenden:

    #ifndef MeineHeaderdatei
    #define MeineHeaderdatei
    
    // das hier wird nur einmal inkludiert
    
    #endif
    

Anmelden zum Antworten