Click here to Skip to main content
16,007,472 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: memory could not be "read" Pin
Mark Salsbery3-Apr-08 10:15
Mark Salsbery3-Apr-08 10:15 
GeneralRe: memory could not be "read" Pin
led mike3-Apr-08 10:42
led mike3-Apr-08 10:42 
GeneralRe: memory could not be "read" Pin
Mark Salsbery3-Apr-08 10:45
Mark Salsbery3-Apr-08 10:45 
GeneralDll and global variables. Pin
Dimitris Vikeloudas3-Apr-08 5:55
Dimitris Vikeloudas3-Apr-08 5:55 
GeneralRe: Dll and global variables. Pin
Joseph Marzbani3-Apr-08 6:03
Joseph Marzbani3-Apr-08 6:03 
GeneralRe: Dll and global variables. Pin
Dimitris Vikeloudas3-Apr-08 6:20
Dimitris Vikeloudas3-Apr-08 6:20 
AnswerRe: Dll and global variables. Pin
cyrfer9-Apr-08 15:49
cyrfer9-Apr-08 15:49 
GeneralRe: Dll and global variables. [modified] Pin
Dimitris Vikeloudas10-Apr-08 3:27
Dimitris Vikeloudas10-Apr-08 3:27 
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
<br />
#ifndef _my_variable_decl<br />
#define _my_variable_decl<br />
<br />
extern int my_var;<br />
<br />
#endif<br />


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
<br />
#include <export.h><br />
<br />
int my_var = 10;<br />
</export.h>


Now migrating to Win32 I transform it

File export.h
<br />
#ifndef _my_variable_decl<br />
#define _my_variable_decl<br />
<br />
#ifdef _WIN32<br />
#ifdef _MY_DLL <br />
#define MY_EXTERN __declspec(dllexport) extern<br />
#else <br />
#define MY_EXTERN __declspec(dllimport) extern<br />
#endif<br />
#else<br />
#define MY_EXTERN extern<br />
#endif<br />
<br />
MY_EXTERN int my_var;<br />
#endif<br />


and modified the initialisation as follows

File globals.cpp
<br />
#include <export.h><br />
<br />
#ifdef _WIN32<br />
#define MY_GLOBAL _declspec(dllexport)<br />
#else<br />
#define MY_GLOBAL <br />
#endif<br />
<br />
MY_GLOBAL int my_var = 10;<br />
</export.h>


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
<br />
#ifndef _my_variable_decl<br />
#define _my_variable_decl<br />
<br />
#ifdef _WIN32<br />
#ifdef _MY_DLL <br />
#define MY_EXTERN(_type, _var, _val) __declspec(dllexport) _type _var = _val<br />
#else <br />
#define MY_EXTERN(_type, _var, _val) __declspec(dllimport) _type _var<br />
#endif<br />
#else<br />
#define MY_EXTERN extern _type _var<br />
#endif<br />
<br />
MY_EXTERN(int, my_var, 10); <br />
<br />
#endif<br />


File globals.cpp
<br />
#include <export.h><br />
<br />
#ifdef _WIN32<br />
#define MY_GLOBAL(_type, _var, _val)<br />
#else<br />
#define MY_GLOBAL(_type, _var, _val)  _type _var = val<br />
<br />
#endif<br />
<br />
MY_GLOBAL(int, my_var, 10);<br />
</export.h>


but by doing that I am not sure how many instance of my_var do I have. Would be one or more than one as my export.h will be include by more than one sources in the main dll? Also it introduces the danger that the initial value is specified in two different places (one for Unix and one for Win32) and this may lead to bugs in future (forget to update one of the two).

modified on Thursday, April 10, 2008 9:33 AM

GeneralRe: Dll and global variables. Pin
malaugh3-Apr-08 10:33
malaugh3-Apr-08 10:33 
GeneralRe: Dll and global variables. Pin
Dimitris Vikeloudas3-Apr-08 22:39
Dimitris Vikeloudas3-Apr-08 22:39 
GeneralCComPtr Pin
George_George3-Apr-08 4:47
George_George3-Apr-08 4:47 
GeneralRe: CComPtr Pin
led mike3-Apr-08 5:14
led mike3-Apr-08 5:14 
GeneralRe: CComPtr Pin
George_George3-Apr-08 21:16
George_George3-Apr-08 21:16 
GeneralA2WBSTR Pin
George_George3-Apr-08 4:40
George_George3-Apr-08 4:40 
GeneralRe: A2WBSTR Pin
James R. Twine3-Apr-08 4:54
James R. Twine3-Apr-08 4:54 
GeneralRe: A2WBSTR Pin
George_George3-Apr-08 21:29
George_George3-Apr-08 21:29 
GeneralRe: A2WBSTR Pin
CPallini3-Apr-08 5:47
mveCPallini3-Apr-08 5:47 
GeneralRe: A2WBSTR Pin
George_George3-Apr-08 21:27
George_George3-Apr-08 21:27 
GeneralRe: A2WBSTR Pin
CPallini3-Apr-08 22:38
mveCPallini3-Apr-08 22:38 
GeneralRe: A2WBSTR Pin
George_George3-Apr-08 23:04
George_George3-Apr-08 23:04 
GeneralRe: A2WBSTR Pin
CPallini3-Apr-08 23:15
mveCPallini3-Apr-08 23:15 
GeneralRe: A2WBSTR Pin
George_George3-Apr-08 23:18
George_George3-Apr-08 23:18 
GeneralRe: A2WBSTR Pin
CPallini3-Apr-08 23:29
mveCPallini3-Apr-08 23:29 
GeneralRe: A2WBSTR Pin
George_George3-Apr-08 23:32
George_George3-Apr-08 23:32 
Generalesoteric use of the "new" keyword Pin
Dave Calkins3-Apr-08 4:12
Dave Calkins3-Apr-08 4:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.