Solved

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

Posted on 2010-09-01
8
672 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 30

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 30

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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 30

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

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…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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 video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

758 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

20 Experts available now in Live!

Get 1:1 Help Now