Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Linking global variables multiple times

Linking global variables multiple times

Scheduled Pinned Locked Moved C / C++ / MFC
helpcomquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Vaclav_
    wrote on last edited by
    #1

    I really need some advice with this „problem“. I am rebuilding a class and according to the original its function is to act as „COM lookalike”Basically y a callback for DirectShow frame capture. The original uses global variables and there is the “problem”. No matter what I do the linker will complain about multiple definitions, but only on some variables. I have made sure the variables are unique and compiled only once, same as the header file, but it does not help. Here is the code: #ifndef _CALLBACK_GLOBALS #define _CALLBACK_GLOBALS #pragma message( "Compiling " __FILE__ ) #pragma message( "Last modified on " __TIMESTAMP__ ) BOOL g_bOneShot_CB = FALSE; DWORD g_dwGraphRegister_CB=0; // For running object table HWND g_hwnd_CB; // Structures typedef struct _callbackinfo_CB { double dblSampleTime; long lBufferSize; BYTE *pBuffer; BITMAPINFOHEADER bih; }CALLBACKINFO_CB; CALLBACKINFO_CB callback; // = {0}; #endif And here is a small sample of linker errors: _AVIFrame.obj : error LNK2005: "int g_bOneShot_CB" (?g_bOneShot_CB@@3HA) already defined in _Analysis_Contour.obj _AVIFrame.obj : error LNK2005: "struct _callbackinfo_CB callback" (?callback@@3U_callbackinfo_CB@@A) already defined in _Analysis_Contour.obj _Blob.obj : error LNK2005: "int g_bOneShot_CB" (?g_bOneShot_CB@@3HA) already defined in _Analysis_Contour.obj _Blob.obj : error LNK2005: "struct _callbackinfo_CB callback" (?callback@@3U_callbackinfo_CB@@A) already defined in _Analysis_Contour.obj _BlobTracker.obj : error LNK2005: "int g_bOneShot_CB" (?g_bOneShot_CB@@3HA) already defined in _Analysis_Contour.obj _BlobTracker.obj : error LNK2005: "struct _callbackinfo_CB callback" (?callback@@3U_callbackinfo_CB@@A) already defined in _Analysis_Contour.obj _ChildFrame_Tab.obj : error LNK2005: "int g_bOneShot_CB" (?g_bOneShot_CB@@3HA) already defined in _Analysis_Contour.obj _ChildFrame_Tab.obj : error LNK2005: "struct _callbackinfo_CB callback" (?callback@@3U_callbackinfo_CB@@A) already defined in _Analysis_Contour.obj _DocManager.obj : error LNK2005: "int g_bOneShot_CB" (?g_bOneShot_CB@@3HA) already defined in _Analysis_Contour.obj _DocManager.obj : error LNK2005: "struct _callbackinfo_CB callback" (?callback@@3U_callbackinfo_CB@@A) already defined in _Analysis_Contour.obj _DrawBitmap.obj : error LNK2005: "int g_bOneShot_CB" (?g_bOneShot_CB@@3HA) already defined in _Analysis_Contour.obj And here is sample usage STDMETHODIMP BufferCB( double dblSampleTime, BYTE * pBuffer, long lBufferSize

    D 1 Reply Last reply
    0
    • V Vaclav_

      I really need some advice with this „problem“. I am rebuilding a class and according to the original its function is to act as „COM lookalike”Basically y a callback for DirectShow frame capture. The original uses global variables and there is the “problem”. No matter what I do the linker will complain about multiple definitions, but only on some variables. I have made sure the variables are unique and compiled only once, same as the header file, but it does not help. Here is the code: #ifndef _CALLBACK_GLOBALS #define _CALLBACK_GLOBALS #pragma message( "Compiling " __FILE__ ) #pragma message( "Last modified on " __TIMESTAMP__ ) BOOL g_bOneShot_CB = FALSE; DWORD g_dwGraphRegister_CB=0; // For running object table HWND g_hwnd_CB; // Structures typedef struct _callbackinfo_CB { double dblSampleTime; long lBufferSize; BYTE *pBuffer; BITMAPINFOHEADER bih; }CALLBACKINFO_CB; CALLBACKINFO_CB callback; // = {0}; #endif And here is a small sample of linker errors: _AVIFrame.obj : error LNK2005: "int g_bOneShot_CB" (?g_bOneShot_CB@@3HA) already defined in _Analysis_Contour.obj _AVIFrame.obj : error LNK2005: "struct _callbackinfo_CB callback" (?callback@@3U_callbackinfo_CB@@A) already defined in _Analysis_Contour.obj _Blob.obj : error LNK2005: "int g_bOneShot_CB" (?g_bOneShot_CB@@3HA) already defined in _Analysis_Contour.obj _Blob.obj : error LNK2005: "struct _callbackinfo_CB callback" (?callback@@3U_callbackinfo_CB@@A) already defined in _Analysis_Contour.obj _BlobTracker.obj : error LNK2005: "int g_bOneShot_CB" (?g_bOneShot_CB@@3HA) already defined in _Analysis_Contour.obj _BlobTracker.obj : error LNK2005: "struct _callbackinfo_CB callback" (?callback@@3U_callbackinfo_CB@@A) already defined in _Analysis_Contour.obj _ChildFrame_Tab.obj : error LNK2005: "int g_bOneShot_CB" (?g_bOneShot_CB@@3HA) already defined in _Analysis_Contour.obj _ChildFrame_Tab.obj : error LNK2005: "struct _callbackinfo_CB callback" (?callback@@3U_callbackinfo_CB@@A) already defined in _Analysis_Contour.obj _DocManager.obj : error LNK2005: "int g_bOneShot_CB" (?g_bOneShot_CB@@3HA) already defined in _Analysis_Contour.obj _DocManager.obj : error LNK2005: "struct _callbackinfo_CB callback" (?callback@@3U_callbackinfo_CB@@A) already defined in _Analysis_Contour.obj _DrawBitmap.obj : error LNK2005: "int g_bOneShot_CB" (?g_bOneShot_CB@@3HA) already defined in _Analysis_Contour.obj And here is sample usage STDMETHODIMP BufferCB( double dblSampleTime, BYTE * pBuffer, long lBufferSize

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      Declare the global variables in the .h file like:

      extern BOOL g_bOneShot_CB;
      extern DWORD g_dwGraphRegister;

      and then in the corresponding .cpp file, define them like:

      g_bOneShot_CB = FALSE;
      g_dwGraphRegister_CB = 0;

      "One man's wage rise is another man's price increase." - Harold Wilson

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

      V 1 Reply Last reply
      0
      • D David Crow

        Declare the global variables in the .h file like:

        extern BOOL g_bOneShot_CB;
        extern DWORD g_dwGraphRegister;

        and then in the corresponding .cpp file, define them like:

        g_bOneShot_CB = FALSE;
        g_dwGraphRegister_CB = 0;

        "One man's wage rise is another man's price increase." - Harold Wilson

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

        V Offline
        V Offline
        Vaclav_
        wrote on last edited by
        #3

        Thanks David, but I just found my stupid "problem". Or better yet, stupid found the problem! THe orignal has two classes in one cpp file, I really do not understand why people do that - it just makes things convoluted and does not save anything. I had the globals in my header file - mistake #1. After fixin that I had the header file in StdAfx and than BEFORE the globals - mistake #2 and #3. Now both compiler and linker are OK. Not the best solution, but now I can code! Cheers Vaclav

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups