Solved

Problems compiling multiple cpp files

Posted on 2000-05-06
18
543 Views
Last Modified: 2010-05-18
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?


0
Comment
Question by:Markenstein
  • 14
  • 4
18 Comments
 
LVL 5

Expert Comment

by:Wyn
Comment Utility
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
 
LVL 5

Expert Comment

by:Wyn
Comment Utility
Dont define it again...
0
 

Author Comment

by:Markenstein
Comment Utility
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
 
LVL 5

Expert Comment

by:Wyn
Comment Utility
You can simply declare as

struct instance_;
0
 
LVL 5

Accepted Solution

by:
Wyn earned 50 total points
Comment Utility
It means you dont inclue the file which define that struct...
So include it...
0
 
LVL 5

Expert Comment

by:Wyn
Comment Utility
Or define it in your .cpp which use it and declare it in other .cpps...
0
 
LVL 5

Expert Comment

by:Wyn
Comment Utility
Like in winmain:


struct instance
{....

};


in other ones...
struct instance;





0
 

Author Comment

by:Markenstein
Comment Utility
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
 
LVL 5

Expert Comment

by:Wyn
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:Markenstein
Comment Utility
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
 
LVL 5

Expert Comment

by:Wyn
Comment Utility
Btw: You' also define hinstance,otherwise all declaration:

HINSTANCE hInstance ...
0
 
LVL 5

Expert Comment

by:Wyn
Comment Utility
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
 
LVL 5

Expert Comment

by:Wyn
Comment Utility
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
 

Author Comment

by:Markenstein
Comment Utility
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
 
LVL 5

Expert Comment

by:Wyn
Comment Utility
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
 
LVL 5

Expert Comment

by:Wyn
Comment Utility
->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
 
LVL 5

Expert Comment

by:Wyn
Comment Utility
->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
 
LVL 5

Expert Comment

by:Wyn
Comment Utility
Are you okay?
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now