?
Solved

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

Posted on 2010-09-01
8
Medium Priority
?
713 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
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.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

765 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