Solved

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

Posted on 2010-09-01
8
693 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

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 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 SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to monitor remote shell execution on linux 9 100
maxBlock challenge 30 129
Unix Command -- Challenging  question 7 91
Detect file exist or not 3 158
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

809 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