Solved

"unresolved external"  on variable declared with __declspec( dllimport )

Posted on 2010-09-01
8
682 Views
Last Modified: 2013-11-20

I'm working with an executable/dll combination originally written by someone else. I'm trying to add a variable to be shared between the dll and exe.

In the exe, I'm declaring the variable like this -  __declspec( dllimport ) int arc_fail;

It's declared in the .cpp file I'm using it in. When I compile I get error LNK2001: unresolved external symbol "__declspec(dllimport) int arc_fail"

I wrote a small program to test what I'm doing, using the same declaration, and it compiled.
0
Comment
Question by:apostrophe27
  • 5
  • 3
8 Comments
 
LVL 31

Expert Comment

by:Zoppo
ID: 33577232
Hi apostrophe27,

how is the variable in the DLL declared and instantiated?

Easiest is to do it somehow like this:

- Define a Preprocessor macro if not yet exists in your DLL-project, i.e. 'MYDLL_EXPORTS'
- In a header in the DLL project do something like this:
> #ifdef MYDLL_EXPORTS
> #define MYDLL_API __declspec(dllexport)
> #else
> #define MYDLL_API __declspec(dllimport)
> #endif

- Declare your variable in the header like this:
> extern MYDLL_API int arc_fail;

- Now you still have to implement the instance of the variable in a CPP file in the DLL like this:
> MYDLL_API int arc_fail = 0;

Now you can include the header in both your DLL and your EXE project and should be able to access the variable from where you want.

Hope that helps,

ZOPPO
0
 

Author Comment

by:apostrophe27
ID: 33577783

I was originally using defines, but for debugging I've been directly typing the commands since they unfold to the same value, and for me made it easier to read.

When I write it like your example above, I get "error C2491: 'arc_fail' : definition of dllimport data not allowed" at the line  MYDLL_API int arc_fail = 0;


0
 
LVL 31

Expert Comment

by:Zoppo
ID: 33578182
Well, the defines in fact don't 'unfold to the same value' - their value depends on the project they are used with - within the DLL project the MYDLL_API is '__declspec ( dllexport )', in the EXE project it's '__declspec( dllimport )'.

The error you got can IMO with my posted code only happen if either the line 'MYDLL_API int arc_fail = 0;' is written in a CPP file in the EXE project or if the 'MYDLL_EXPORTS' is not defined in the DLL project.

Maybe you could post the code of all related places in both projects? So the declaration in the DLLs header, the instantiation and how it's used in the EXE ...
0
 

Author Comment

by:apostrophe27
ID: 33578880

Ok, the dll compiles now. I had MYDLL_EXPORTS misspelled when I defined it.

Now the exe isn't compiling. I get the error "unresolved external symbol "__declspec(dllimport) int arc_fail" in the cpp file I'm using it in.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:apostrophe27
ID: 33579362
I just noticed that I'm getting the warning "warning C4273: 'arc_fail' : inconsistent dll linkage" at the line in the dll .cpp file where I declare  MYDLL_API int arc_fail = 0;

Here's the file I'm sharing between the exe and dll projects.

//shared.h

#ifdef MYDLL_EXPORTS      
      #define MYDLL_API __declspec(dllexport)
#else      
      #define MYDLL_API __declspec(dllimport)
#endif


extern MYDLL_API int arc_fail;



0
 

Author Comment

by:apostrophe27
ID: 33579420

Sorry, I made a mistake - I'm not getting any errors in the dll project. I am getting the error in the exe project.
0
 

Author Comment

by:apostrophe27
ID: 33580232

One more thing - the exe and dll are not part of the same solution.

I do have the same shared.h file included in both.
0
 
LVL 31

Accepted Solution

by:
Zoppo earned 500 total points
ID: 33584085
ok - if they aren't in the same solution it maybe the EXE project simply doesn't know it has to link the library created with the DLL project. Did you set something in the EXE project's linker settings? IMO the DLL project's LIB has to be added to the EXE projects 'Properties->Linker->Input->Additional Dependencies' ...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

919 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now