?
Solved

Problems compiling multiple cpp files

Posted on 2000-05-06
18
Medium Priority
?
575 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
ID: 2783520
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
ID: 2783528
Dont define it again...
0
 

Author Comment

by:Markenstein
ID: 2783531
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 5

Expert Comment

by:Wyn
ID: 2783533
You can simply declare as

struct instance_;
0
 
LVL 5

Accepted Solution

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

Expert Comment

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

Expert Comment

by:Wyn
ID: 2783549
Like in winmain:


struct instance
{....

};


in other ones...
struct instance;





0
 

Author Comment

by:Markenstein
ID: 2783551
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
ID: 2783604
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
 

Author Comment

by:Markenstein
ID: 2783611
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
ID: 2783621
Btw: You' also define hinstance,otherwise all declaration:

HINSTANCE hInstance ...
0
 
LVL 5

Expert Comment

by:Wyn
ID: 2783626
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
ID: 2783627
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
ID: 2783629
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
ID: 2783643
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
ID: 2783651
->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
ID: 2783699
->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
ID: 2783704
Are you okay?
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
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.
Suggested Courses

840 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