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
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Dll and global variables.

Dll and global variables.

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++json
7 Posts 4 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.
  • D Offline
    D Offline
    Dimitris Vikeloudas
    wrote on last edited by
    #1

    How do I declare initialise a global variable, exported from one dll and use it from various files? Let me be more specific. Assume that I have two libraries (i.e. dlls) in my application A & B. The A library consists of the A_1.cpp & the A_2.cpp file (among others). The B library consists of a B_1.cpp source. The requirment is that both use a global variable A_X which is stored in the A dll. In UNIX using gcc I declare the code in a common header A_X.h Thus the A_X.h looks like extern int A_X; Then in library A my two sources look like A_1.cpp source #include "A_X.h" /* initialise A_X variable */ int A_X = 10; A_2.cpp source #include "A_X.h" .... code using A_X; and in library B the source code of B_1.cpp is looking like #include "A_X.h" .... code of B_1 using A_X; I have tried the following in windows A_X.h header : #ifdef LIB_IS_A __dllspec(dllexport) extern int A_X; #else __dllspec(dllimport) extern int A_X; #endif and the A_1.cpp source in A dll #include "A_X.h" /* initialise A_X variable */ __dllspec(dllexport) int A_X = 10; The rest of the sources are the same. Although the solution worked I take the following warning in Win32 when I try to build the A.dll A_1.obj : warning LNK4197: export "?A_X@@3IA" specified multiple times; using first specification How can I declare my A_X to be a global variable. I.e. delcare it in a header, use it in both A.dll and B.dll, store it in A.dll once, and be able to use it by any source that I include that header?

    J M 2 Replies Last reply
    0
    • D Dimitris Vikeloudas

      How do I declare initialise a global variable, exported from one dll and use it from various files? Let me be more specific. Assume that I have two libraries (i.e. dlls) in my application A & B. The A library consists of the A_1.cpp & the A_2.cpp file (among others). The B library consists of a B_1.cpp source. The requirment is that both use a global variable A_X which is stored in the A dll. In UNIX using gcc I declare the code in a common header A_X.h Thus the A_X.h looks like extern int A_X; Then in library A my two sources look like A_1.cpp source #include "A_X.h" /* initialise A_X variable */ int A_X = 10; A_2.cpp source #include "A_X.h" .... code using A_X; and in library B the source code of B_1.cpp is looking like #include "A_X.h" .... code of B_1 using A_X; I have tried the following in windows A_X.h header : #ifdef LIB_IS_A __dllspec(dllexport) extern int A_X; #else __dllspec(dllimport) extern int A_X; #endif and the A_1.cpp source in A dll #include "A_X.h" /* initialise A_X variable */ __dllspec(dllexport) int A_X = 10; The rest of the sources are the same. Although the solution worked I take the following warning in Win32 when I try to build the A.dll A_1.obj : warning LNK4197: export "?A_X@@3IA" specified multiple times; using first specification How can I declare my A_X to be a global variable. I.e. delcare it in a header, use it in both A.dll and B.dll, store it in A.dll once, and be able to use it by any source that I include that header?

      J Offline
      J Offline
      Joseph Marzbani
      wrote on last edited by
      #2

      In DLL: __declspec(dllexport) int g_iYourVariable; In EXE files: __declspec(dllimport) int g_iYourVariable;

      D 1 Reply Last reply
      0
      • J Joseph Marzbani

        In DLL: __declspec(dllexport) int g_iYourVariable; In EXE files: __declspec(dllimport) int g_iYourVariable;

        D Offline
        D Offline
        Dimitris Vikeloudas
        wrote on last edited by
        #3

        Appologies for the typo in the original post. Where __dllspec read <code>_declspec</code>. However, I am not sure that I understand since the only thing I am building is two dlls (exes come much further down the line and they do not really know the variable). Thus, how I create the g_iYourVariable initialise in one of source files of the first dll, use it in all other sources of the same dll, export it and be able to imported (assuming using __declspec(dllimport)) in any other dll file)?

        C 1 Reply Last reply
        0
        • D Dimitris Vikeloudas

          How do I declare initialise a global variable, exported from one dll and use it from various files? Let me be more specific. Assume that I have two libraries (i.e. dlls) in my application A & B. The A library consists of the A_1.cpp & the A_2.cpp file (among others). The B library consists of a B_1.cpp source. The requirment is that both use a global variable A_X which is stored in the A dll. In UNIX using gcc I declare the code in a common header A_X.h Thus the A_X.h looks like extern int A_X; Then in library A my two sources look like A_1.cpp source #include "A_X.h" /* initialise A_X variable */ int A_X = 10; A_2.cpp source #include "A_X.h" .... code using A_X; and in library B the source code of B_1.cpp is looking like #include "A_X.h" .... code of B_1 using A_X; I have tried the following in windows A_X.h header : #ifdef LIB_IS_A __dllspec(dllexport) extern int A_X; #else __dllspec(dllimport) extern int A_X; #endif and the A_1.cpp source in A dll #include "A_X.h" /* initialise A_X variable */ __dllspec(dllexport) int A_X = 10; The rest of the sources are the same. Although the solution worked I take the following warning in Win32 when I try to build the A.dll A_1.obj : warning LNK4197: export "?A_X@@3IA" specified multiple times; using first specification How can I declare my A_X to be a global variable. I.e. delcare it in a header, use it in both A.dll and B.dll, store it in A.dll once, and be able to use it by any source that I include that header?

          M Offline
          M Offline
          malaugh
          wrote on last edited by
          #4

          There may be a easier way to do this, but one way is to create global shared memory is to use CreateFileMapping on one DLL, and OpenFileMapping in the other. Pseudo-code is something like: DLL #1 ------ int *pMemory; hMem = CreateFileMapping(....."MySharedMemory"); pMemory = (int *)MapViewOfFile(hMem); pMemory = 3; DLL #2 ------- int *pMemory; int Value; hMem = OpenFileMapping(....."MySharedMemory"); pMemory = (int *)MapViewOfFile(hMem); Value = *pMemory; Note sure if this is 100% correct, its been a while since I used it. grep in codeproject for "Shared Memory with IPC with threads" to get more details.

          D 1 Reply Last reply
          0
          • M malaugh

            There may be a easier way to do this, but one way is to create global shared memory is to use CreateFileMapping on one DLL, and OpenFileMapping in the other. Pseudo-code is something like: DLL #1 ------ int *pMemory; hMem = CreateFileMapping(....."MySharedMemory"); pMemory = (int *)MapViewOfFile(hMem); pMemory = 3; DLL #2 ------- int *pMemory; int Value; hMem = OpenFileMapping(....."MySharedMemory"); pMemory = (int *)MapViewOfFile(hMem); Value = *pMemory; Note sure if this is 100% correct, its been a while since I used it. grep in codeproject for "Shared Memory with IPC with threads" to get more details.

            D Offline
            D Offline
            Dimitris Vikeloudas
            wrote on last edited by
            #5

            Thanks but I think shared memory will complicate my code immencelly. I gave it a thought but then it is too much of a complication. In the worse case I 'll keep it as it is and will have to ignore the compiler warnings. After all despite the warnings the existing code seems to work (not sure though if it is by accident though).

            1 Reply Last reply
            0
            • D Dimitris Vikeloudas

              Appologies for the typo in the original post. Where __dllspec read <code>_declspec</code>. However, I am not sure that I understand since the only thing I am building is two dlls (exes come much further down the line and they do not really know the variable). Thus, how I create the g_iYourVariable initialise in one of source files of the first dll, use it in all other sources of the same dll, export it and be able to imported (assuming using __declspec(dllimport)) in any other dll file)?

              C Offline
              C Offline
              cyrfer
              wrote on last edited by
              #6

              Thus, how I create the g_iYourVariable initialise in one of source files of the first dll, use it in all other sources of the same dll, export it and be able to imported (assuming using __declspec(dllimport)) in any other dll file)? 1 - define a handy macro to make it easy for your DLL to use dllexport and allow clients to use dllimport. Put it in a file, call it "MyDLLExports.h". #ifdef BUILDING_MYDLL # define MyDLLExport __declspec(dllexport) #else # define MyDLLExport __declspec(dllimport) #endif 2 - compile all sources of your DLL with the preprocessor definition 'BUILDING_MYDLL' added - when building only your DLL. 3 - define a header file where your variable will exist. e.g. #ifndef __MyDLL_MyVar_h__ #define __MyDLL_MyVar_h__ #include "MyDLLExports.h" MyDLLExport int MyDLLmyVar; #endif // __MyDLL_MyVar_h__

              D 1 Reply Last reply
              0
              • C cyrfer

                Thus, how I create the g_iYourVariable initialise in one of source files of the first dll, use it in all other sources of the same dll, export it and be able to imported (assuming using __declspec(dllimport)) in any other dll file)? 1 - define a handy macro to make it easy for your DLL to use dllexport and allow clients to use dllimport. Put it in a file, call it "MyDLLExports.h". #ifdef BUILDING_MYDLL # define MyDLLExport __declspec(dllexport) #else # define MyDLLExport __declspec(dllimport) #endif 2 - compile all sources of your DLL with the preprocessor definition 'BUILDING_MYDLL' added - when building only your DLL. 3 - define a header file where your variable will exist. e.g. #ifndef __MyDLL_MyVar_h__ #define __MyDLL_MyVar_h__ #include "MyDLLExports.h" MyDLLExport int MyDLLmyVar; #endif // __MyDLL_MyVar_h__

                D Offline
                D Offline
                Dimitris Vikeloudas
                wrote on last edited by
                #7

                That's almost exactly how I did it. It seems to work but still get a linking warning of multiple definitions. I think the problem comes from the difference between the declaration and the definition with initialisation. To be more specific my code originates from Unix. What I use to have there was File export.h #ifndef _my_variable_decl #define _my_variable_decl extern int my_var; #endif which was included by all the clients. However, because the variable is external I have to redeclare it in a local file in one of my libraries. The declaration & its initialisation is File globals.cpp #include int my_var = 10; Now migrating to Win32 I transform it File export.h #ifndef _my_variable_decl #define _my_variable_decl #ifdef _WIN32 #ifdef _MY_DLL #define MY_EXTERN __declspec(dllexport) extern #else #define MY_EXTERN __declspec(dllimport) extern #endif #else #define MY_EXTERN extern #endif MY_EXTERN int my_var; #endif and modified the initialisation as follows File globals.cpp #include #ifdef _WIN32 #define MY_GLOBAL _declspec(dllexport) #else #define MY_GLOBAL #endif MY_GLOBAL int my_var = 10; As you said the _MY_DLL is set only when I compile the main dll part of it is globals.cpp. Other dlls include export.h. This code works and my_var behaves as a global variable inside the main dll and any other dll (note also that it is still compatible with UNIX). However, when I build the main DLL I get a warning of globals.obj : warning LNK4197: export "?my_var@@3IA" specified multiple times; using first specification which is due to the fact that _WIN32 think that the variable is declared twice (at least that's what my Visual C++ 6.0 compiler says). I tried to remove the extern keyword in the windows part of the export.h but then it does not compile. I tried to add a __declspec(selectany) and again I get the same warning. It is an annoying warning and nothing more so I am incline to endure it since my_var is a real global variable. I thought to reshape the code as following File export.h #ifndef _my_variable_decl #define _my_variable_decl #ifdef _WIN32 #ifdef _MY_DLL #define MY_EXTERN(_type, _var, _val) __declspec(dllexport) _type _var = _val #else #define MY_EXTERN(_type, _var, _val) __declspec(dllimport) _type _var #endif #else

                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