|
-
August 7th, 2003, 11:31 AM
#1
Problem on Linking extern variables
Hi,
I have problem on linking an extern variable.
My code is look like as follows.
My dll contains the following code
DllTest.h
-----------
#include <iostream>
namespace Test
{
__declspec(dllexport) extern int* i;
}
DllTest.cpp
--------------
#include "DllTest.h"
namespace Test
{
__declspec(dllexport) int *i = NULL;
}
If i make a dll with above code it is done.
When i am linking this dll with my Test program it gives the error
error LNK2001: unresolved external symbol "int * Test::i" (?i@Test@@3PAHA)
My Test Program for this dll is
#include <DllTest.h>
void main()
{
if (Test::i == NULL)
{
printf("Null");
}
else
{
printf("Not Null");
}
}
If anybody gives solution i am very much happy......
thanks in advance
Regards
Manikandan
-
August 7th, 2003, 11:52 AM
#2
-
August 7th, 2003, 11:55 AM
#3
The way ur using ur variable for that u need to have ur DLL should be implicitaly link.
How r u linking ur DLL? Is it implicit linking?
See MSDN article here
If ur linking ur DLL implicitaly then name mangling might be issue.
when ur trying to export from a DLL use extern "C" so that u can use it with diff. kind of name mangling schemes.
rgds,
Vinod
-
August 8th, 2003, 12:29 AM
#4
Hi Richard,
I didn't understand how it will solve my problem from the thread given by you. Please can you explain.
-
August 8th, 2003, 12:32 AM
#5
Hi Vinod,
I tried with extern "C", still i am getting the same error.
Yes, I am using implicit linking.
Can u solve this problem?
thanks
Regards
Manikandan
-
August 8th, 2003, 06:01 AM
#6
The problem is that the variable is always defined as dllexport even when it should be dllimport or no modifier at all in the app that uses the dll. It's often better to add macros that will make your life easier
Code:
DllTest.h
#ifdef _MY_DLL_BUILDING
#define DLLEXPORT __declspec(dllexport)
#else
#define DLLEXPORT __declspec(dllimport)
#endif
#include <iostream>
namespace Test
{
extern DLLEXPORT extern int* i;
}
DllTest.cpp
#ifndef _MY_DLL_BUILDING
#define _MY_DLL_BUILDING
#endif
#include "DllTest.h"
namespace Test
{
DLLEXPORT int *i = NULL;
}
// test app
#include <DllTest.h>
#pragma comment (lib, "DllTest.lib")
void main()
{
if (Test::i == NULL) printf("Null"); else printf("Not Null");
}
Hope it will help you
-
August 8th, 2003, 07:39 AM
#7
Hi,
Thanks for all.........
The problem is solved. It is actually because of export and import.
Thanks for all ur help.
Regards
Manikandan
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|