• C

Problems compiling multiple cpp files

I'm going to be working on a big project so instead of putting everything in one big file i thought it would be better if i split it all up.  I have the following files:

winproc.cpp - contains the winproc function
winmain.cpp - contains winmain and similar functions
globals.h - contains global variables and function definitions.

When i goto compile i get the following errors:

Compiling...
winmain.cpp
winproc.cpp
Linking...
winproc.obj : error LNK2005: "struct HINSTANCE__ *  hInstance" (?hInstance@@3PAUHINSTANCE__@@A) already defined in winmain.obj
Debug/MASTER.exe : fatal error LNK1169: one or more multiply defined symbols found
Error executing link.exe.

How would i fix this?


MarkensteinAsked:
Who is Participating?
 
WynConnect With a Mentor Commented:
It means you dont inclue the file which define that struct...
So include it...
0
 
WynCommented:
It means you have already defined struct HINSTANCE__ *  hInstance in winmain.cpp but you redine it again in winproc.cpp again.

Ways to solve...
0
 
WynCommented:
Dont define it again...
0
Worried about phishing attacks?

90% of attacks start with a phish. It’s critical that IT admins and MSSPs have the right security in place to protect their end users from these phishing attacks. Check out our latest feature brief for tips and tricks to keep your employees off a hackers line!

 
MarkensteinAuthor Commented:
I'm sorry the errors i get are:

Compiling...
winmain.cpp
winproc.cpp
Linking...
winmain.obj : error LNK2001: unresolved external symbol "struct HINSTANCE__ *  hInstance" (?hInstance@@3PAUHINSTANCE__@@A)
Debug/MASTER.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

And not what i said before.
0
 
WynCommented:
You can simply declare as

struct instance_;
0
 
WynCommented:
Or define it in your .cpp which use it and declare it in other .cpps...
0
 
WynCommented:
Like in winmain:


struct instance
{....

};


in other ones...
struct instance;





0
 
MarkensteinAuthor Commented:
I included windows.h, which is where HINSTANCE is (right?)

globals.h

#include <windows.h>

//-------------------------------------------------------------------------
// Product identifier string defines

#define APPNAME       Master

//-------------------------------------------------------------------------
// Functions for initialization

BOOL Register(HINSTANCE hInst);
HWND Create(HINSTANCE hInst, int nCmdShow);

//-------------------------------------------------------------------------
// Functions for Dialogs' WinProcs

LRESULT CALLBACK WinProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam);

//-------------------------------------------------------------------------
// Global Variables

extern HINSTANCE hInstance; // The current instance handle





winproc.cpp

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

//-------------------------------------------------------------------------
// Window Procedure, where it all happens son

LRESULT CALLBACK WinProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
      switch(message)
      {
      case WM_DESTROY:
            
            PostQuitMessage(0);
            return FALSE;
      
      }

      return FALSE;
}






winmain.cpp

#include <windows.h>
#include <commctrl.h>
#include <richedit.h>
#include "resource.h"
#include "globals.h"

//-------------------------------------------------------------------------
// WinMain

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR lpszCmdParam, int nCmdShow)
{
      MSG  Msg;
      HWND MainWindow;

      
      if (!hPrevInstance)
            if (!Register(hInst))
                  return FALSE;
      
      MainWindow = Create(hInst, nCmdShow);

      if (!MainWindow)
            return FALSE;

      while (GetMessage(&Msg, NULL, 0, 0))
      {
            {
                  TranslateMessage(&Msg);
                  DispatchMessage(&Msg);
            }
      }

      return Msg.wParam;
}

//-------------------------------------------------------------------------
// Register the Window

BOOL Register(HINSTANCE hInst)
{
      WNDCLASSEX WndClassEx;
      TCHAR szAppName[20];

      LoadString(hInstance, IDS_APPNAME, szAppName, sizeof(szAppName));

      WndClassEx.cbSize        = sizeof(WNDCLASSEX);
      WndClassEx.style         = CS_HREDRAW | CS_VREDRAW;
      WndClassEx.lpfnWndProc   = WinProc;
      WndClassEx.cbClsExtra    = 0;
      WndClassEx.cbWndExtra    = DLGWINDOWEXTRA;
      WndClassEx.hInstance     = hInst;
      WndClassEx.hIcon         = LoadIcon(hInst, "ICON");
      WndClassEx.hCursor       = LoadCursor(NULL, IDC_ARROW);
      WndClassEx.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1);
      WndClassEx.lpszMenuName      = NULL;
      WndClassEx.lpszClassName = szAppName;
      WndClassEx.hIconSm               = LoadIcon(hInst, "ICON");
      return RegisterClassEx(&WndClassEx);
}

//-------------------------------------------------------------------------
// Create the Window

HWND Create(HINSTANCE hInst, int nCmdShow)
{
      HWND hwnd;
      hInstance = hInst;
      TCHAR szAppName[20];

      LoadString(hInstance, IDS_APPNAME, szAppName, sizeof(szAppName));

      hwnd = CreateDialog(hInst, szAppName, 0, NULL);

      if (hwnd == NULL)
      {
            MessageBox(0, "Error: main window could not be created.",
                  szAppName, MB_OK | MB_ICONEXCLAMATION);
            return hwnd;
      }

  ShowWindow(hwnd, nCmdShow);
  return hwnd;
}
0
 
WynCommented:
What's the problem ? it's okay...

Btw ,you can remove include windows.h in your globals.h since you include windows.h prior than globals.h...
0
 
MarkensteinAuthor Commented:
I get the following errors when i try to compile the project in VC++ 6:

Compiling...
winmain.cpp
winproc.cpp
Linking...
winmain.obj : error LNK2001: unresolved external symbol "struct HINSTANCE__ *  hInstance" (?hInstance@@3PAUHINSTANCE__@@A)
Debug/MASTER.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
0
 
WynCommented:
Btw: You' also define hinstance,otherwise all declaration:

HINSTANCE hInstance ...
0
 
WynCommented:
Btw: You' also define hinstance,otherwise all declaration:

HINSTANCE hInstance ...

i.e:include global.h in every .cpp which use it and define global variables in the winmain.cpp
0
 
WynCommented:
Btw: You' also define hinstance,otherwise all declarations:

HINSTANCE hInstance ...

i.e:include global.h in every .cpp which use it and define global variables in the winmain.cpp
0
 
MarkensteinAuthor Commented:
I did include global.h in every .cpp, why can't i define global veriables in globals.h and declare them as extern, won't that work?
0
 
WynCommented:
Here is  a scenario:

Make a global.h which contain all global declaration ...

Define them in the winmain.cpp and include the global.h in other cpps which use them...

Regards
Eirnava...
0
 
WynCommented:
->I did include global.h in every .cpp, why can't i define global veriables in globals.h and declare them as extern, won't that work?
=======================
extern means declaration ,but every varaibles in c++ must have a definition , otherwise link error...

YOu'd define it in your main cpp file ,following last comment...

Sorry for many same comments,my connection is bad...
Regards
Eirnava..
0
 
WynCommented:
->I did include global.h in every .cpp, why can't i define global veriables in globals.h and declare them as extern, won't that work?
=======================
extern means declaration ,but every varaibles in c++ must have a definition , otherwise link error...

YOu'd define it in your main cpp file ,following last comment...

Sorry for many same comments,my connection is bad...
Regards
Eirnava..
0
 
WynCommented:
Are you okay?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.