Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Problems compiling multiple cpp files

Posted on 2000-05-06
18
552 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 5

Expert Comment

by:Wyn
ID: 2783533
You can simply declare as

struct instance_;
0
 
LVL 5

Accepted Solution

by:
Wyn earned 50 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Using unmanaged DLL from managed application 14 294
negation in C function 14 161
Want to delete all my personal data 13 146
Secondary drive is failing... D Volume 11 13
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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

839 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