You are here:

C++/Use of a variable in different projects

Advertisement


Question
QUESTION: Hi!

I need to solve a certain problem. I have 2 different projects in Visual .net 2003. One will be the .exe and the other will be the library(.dll) and they are working together will the program is executing.

I need to use a variable from the library project to the other. I'm trying to use "extern int" in the .h of the library so I can use it on the other project. I have the next error: error LNK2201: unresolved external symbol "int IEMTLIB::acknow". I don't know what to do, I've tried lot of different paths, but I didn't fixed the error.

Thanks for reading and I hope you can help me. Thanks a lot!

ANSWER: Hello Miguel.

I think I can help you. In your dll header file you should declare the variable being exported, like this sample:

/* BEGIN mydll.h */
#ifdef MYDLL_EXPORTS
#define MYDLL_API __declspec(dllexport)
#else
#define MYDLL_API __declspec(dllimport)
#endif

MYDLL_API extern int myVariable;
/* END mydll.h */

In your dll source file you should have:

/* BEGIN mydll.c */
#include "mydll.h"
MYDLL_API int myVariable;
/* END mydll.c */

In your dll project, make sure that MYDLL_EXPORTS is defined for the pre-processor. It will be if you made a dll project with the wizard.

In your exe source code, make sure you include #include "mydll.h".
You will need to specify the location of the header. You can do so either in the project property pages under C/C++ / General, or globally under Tools/Options/Projects and Solutions/ VC++ Directories (choose Show directories for Include files)

You will need to link in the lib file. You can set that in the project property pages under Linker/Input/Additional Dependencies.

That should remove the linker error. Finally, make sure your dll is in the same folder as your exe, or somewhere on the path so that it is found at runtime.

If you have trouble with the project settings, send me your email in a private question and I'll send you screen shots.

Best regards
Zlatko

---------- FOLLOW-UP ----------

QUESTION: First of all, thanks for the answer :). I did those steps you said some days ago, but the error still there. I tell a bit more of what I've done.

The dll file is called iemtlib. The header, called iemtlib.h had already:

#ifdef IEMTLIB_EXPORTS
#define IEMTLIB_API __declspec(dllexport)
#else
#define IEMTLIB_API __declspec(dllimport)
#endif

In the same file IEMTLIB is defined as a namespace. I've put there (inside the namespace) this:

IEMTLIB_API extern int acknow;

I didn't insert it in dll's source file, because it gave me an error. Just with the definition in the header is fine and compiles well.

The problem is in the exe. On the header files, there's already a file called IEMTLib.h, and the code is exactly the same as iemtlib.h in the dll. I put the same as the first one (IEMTLIB_API extern int acknow;). There's a source code in the exe called MPMManager, where the principal tasks are done and also, where I need to use my variable with the value obtained in the dll. There was already an #include "IEMTLib.h", that is the header I've said already. I've also wrote #include "iemtlib.h". The error that appears is the following:

error LNK2001:unresolved external symbol"__declspec(dllimport) int IEMTLIB::acknow
(acknow is my variable)
fatal error LNK1120:1 unresolved externals

I've tried lot of different combinations but always appears the same error. Where do you think it can be the problem?

Thanks!

Answer
Hi Miguel.

Make sure the namespace is in both the dll header file and the dll source file

In the header:

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

namespace asdf
{
MYDLL_API extern int acknow;
}


In the source:

#include "stdafx.h"
#include "mydll.h"


namespace asdf
{
MYDLL_API int acknow = 10;
}

And of course in your exe

#include <stdio.h>
#include "mydll.h"

int main(void)
{
   printf("acknow is %d\n", asdf::acknow);
}

If you cannot get your project to work, try to reproduce the problem as simply as possible by creating a dll and exe with just the 3 files I showed above. If you can get that to work then make sure your real project settings match the tests. If you cannot, then zip up the test project and send it to me at zlatko.c.help at gmail

Best regards
Zlatko

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2016 About.com. All rights reserved.