CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2003
    Location
    India
    Posts
    4

    Unhappy 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

  2. #2
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158

  3. #3
    Join Date
    Oct 2002
    Location
    Tx, US
    Posts
    208
    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

  4. #4
    Join Date
    Aug 2003
    Location
    India
    Posts
    4
    Hi Richard,
    I didn't understand how it will solve my problem from the thread given by you. Please can you explain.

  5. #5
    Join Date
    Aug 2003
    Location
    India
    Posts
    4
    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

  6. #6
    Join Date
    Apr 2003
    Posts
    1,755

    Smile

    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

  7. #7
    Join Date
    Aug 2003
    Location
    India
    Posts
    4

    Smile

    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
  •  





Click Here to Expand Forum to Full Width

Featured