I have three files:

main.cpp

#include "CMain.h"
int WINAPI WinMain(HINSTANCE hIn...)
{
	return 0;
}

CMain.cpp

#include "CMain.h"
using namespace Program;

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch(msg)
	{
	case WM_DESTROY:
		PostQuitMessage(0);
		break;

	default: return DefWindowProc(hwnd, msg, wParam, lParam);
	}

	return 0;
}

CMain::CMain(void)
{
	hInstance = GetModuleHandle(NULL);
}

And CMain.h

#pragma once
#include <windows.h>

namespace Program
{
	class CMain
	{
	private:
		HINSTANCE hInstance;
		HWND hwnd;
	public:
		CMain(void);
	};
	CMain *g_App;
}

And my problem is at CMain *g_App; Where the compiler says:

error LNK2005: "class Program::CMain * Program::g_App" (?g_App@Programle@@3PAVCMain@1@A) already defined in CMain.obj
fatal error LNK1169: one or more multiply defined symbols found

But, when having everything in only one file everything works like a charm. How should I do to make it work with the current files?

Recommended Answers

All 2 Replies

line 14 of CMain.h -- global objects can not be declared in header files because the compiler will try to create them in each *.cpp file in which the header file is included. Instead, use the extern keyword in the header file then in one, and only one *.cpp file declare the object without the extern keyword.

Oh I see, got it working now. Thank you very much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.