[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2010-09-01
8
Medium Priority
?
728 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 

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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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 summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Suggested Courses
Course of the Month20 days, 8 hours left to enroll

868 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