error LNK2001: unresolved external symbol
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: error LNK2001: unresolved external symbol

  1. #1
    Join Date
    Jun 2011
    Posts
    2

    error LNK2001: unresolved external symbol

    I am included C headers file in my VC8 project. In one of my C header file , we have few definistions of the variables. When i compile my project it gives me linker error for all the global variables.
    My global hearder file "intdefs.h" has below data:
    long rfl_xref_id;

    I have included intdefs.h file in my main cpp file say myfile.cpp:
    #include intdefs.h
    .........
    extern long rfl_xref_id
    .....

    getting linker error :error LNK2001: unresolved external symbol "long rfl_xref_id" (?rfl_xref_id@@3JA)

  2. #2
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,526

    Re: error LNK2001: unresolved external symbol

    Quote Originally Posted by annieshaw View Post
    I am included C headers file in my VC8 project. In one of my C header file , we have few definistions of the variables. When i compile my project it gives me linker error for all the global variables.
    My global hearder file "intdefs.h" has below data:
    long rfl_xref_id;

    I have included intdefs.h file in my main cpp file say myfile.cpp:
    #include intdefs.h
    .........
    extern long rfl_xref_id
    .....

    getting linker error :error LNK2001: unresolved external symbol "long rfl_xref_id" (?rfl_xref_id@@3JA)
    Could you please post your intdefs.h file?
    Are your variables defined in a global namespace?
    Are there conditional preprocessor directives?
    Also, it is a bad idea to have variable definition in the header file, you should only have declarations there.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinViewer - an integrated GDI objects viewer for Visual C++ Debugger, and more...

  3. #3
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,634

    Re: error LNK2001: unresolved external symbol

    As Vladimir say you have to have a long rfl_xref_id; in one of your source files.
    extern long rfl_xref_id tell the compiler that no space has to be allocated for this variable since it's located in some other source file.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

  4. #4
    Join Date
    Jun 2011
    Posts
    2

    Re: error LNK2001: unresolved external symbol

    Its solved. I was declared and define in the same file(as per C programing concept). In C++, declartion goes to header file and definitions should be in CPP file.

    Thanks for you help!

  5. #5
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,970

    Re: error LNK2001: unresolved external symbol

    I was declared and define in the same file(as per C programing concept). In C++, declartion goes to header file and definitions should be in CPP file.
    Actually, C and C++ share the same concept. As per design, C++ was made a successor to C.

    Header files contain declarations with external linkage (functions are automatically given the one), while compile units (.c or .cpp) contain definitions. Without external declaration a variable is limited to unit scope, compiler generates allocation code for the variable, and its references appear resolved immediately in the unit scope. External declaration instructs to resolve references in the other units (link them to variable real location) on a later stage by linker. That's why linker is called linker.
    Last edited by Igor Vartanov; July 8th, 2011 at 12:04 AM.
    Best regards,
    Igor

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center