dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: [RESOLVED] Link error after upgrade to MSVC 2017

  1. #1
    Join Date
    Aug 2002
    Posts
    681

    [RESOLVED] Link error after upgrade to MSVC 2017

    Hi,
    I recently upgraded my compiler/IDE to MSVC 2017 and now started getting a link error as such:

    Code:
    odbccp32.lib(dllload.obj) : error LNK2019: unresolved external symbol __vsnwprintf_s referenced in function _StringCchPrintfW
    Googling revealed that MS changed the function __vsnwprintf_s() to be inline and I should either link to some library (legacy_stdio_definitions.lib) or include the appropriate header. Both solutions are described here.

    I did it the second way, adding to one of the .cpp files:

    Code:
    #if _MSC_VER >= 1900
    #include <stdio.h>
    #include <stdarg.h>
    #endif
    but the error still there.

    Should I still link in the library, or just include that piece of code in every single source code file? The code is in DLL which contains just 2 cpp files - one to initialize the DLL and for the exported class definition.

    Or there is something else I should do?

    Thank you.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,460

    Re: Link error after upgrade to MSVC 2017

    We had this type of pain a few years ago when we upgraded to the version of VS that had these breaking changes. You shouldn't need the version test. The includes go at the top (after any include guards) in all the source files (.c/.cpp/.h/.hpp etc) that use any of the stdio.h functions (eg printf(), put() etc). If I remember right, at the time we ended up linking with the stated legacy lib to get a successful compile/link until we sorted out all of the source - as some wasn't developed by us. Getting a clean build with no warnings/errors I think took us a few months. Note that for c++ code, you should use the c++ version of the headers (cstdio, cstdarg etc) which has the functions in the std:: name space - rather than the global name space. The change is that you add c to the front and drop the .h
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.8.7)

  3. #3
    Join Date
    Aug 2002
    Posts
    681

    Re: Link error after upgrade to MSVC 2017

    Hi,
    Quote Originally Posted by 2kaud View Post
    We had this type of pain a few years ago when we upgraded to the version of VS that had these breaking changes. You shouldn't need the version test. The includes go at the top (after any include guards) in all the source files (.c/.cpp/.h/.hpp etc) that use any of the stdio.h functions (eg printf(), put() etc). If I remember right, at the time we ended up linking with the stated legacy lib to get a successful compile/link until we sorted out all of the source - as some wasn't developed by us. Getting a clean build with no warnings/errors I think took us a few months. Note that for c++ code, you should use the c++ version of the headers (cstdio, cstdarg etc) which has the functions in the std:: name space - rather than the global name space. The change is that you add c to the front and drop the .h
    Thank you, for the reply.
    Now my library is for the C++ and so I think I should modify the code to read:

    Code:
    #if _MSC_VER >= 1900
    #include <cstdio>
    #include <cstdarg>
    #endif
    right?
    Also, if I read the reply right, I should put this code in both files of the library, right?

    Now the version guard is here for the earlier versions of the MSVC, because I don't know who will compile my software and what compiler will be used.

    Thank you.

    P.S.: For reference here is the code I have right now and it still produces the link error:

    Code:
    #ifdef WIN32
    #include <windows.h>
    #endif
    #ifndef WIN32
    #include <sstream>
    #endif
    
    #if defined __WXMSW__ && defined __MEMORYLEAKS__
    #include <vld.h>
    #endif
    
    #include <map>
    #include <set>
    #include <vector>
    #include <string>
    #include <algorithm>
    #include <sstream>
    #if _MSC_VER >= 1900
    #include <cstdio>
    #include <cstdarg>
    #endif
    #ifdef _IODBCUNIX_FRAMEWORK
    #include "iODBC/sql.h"
    #include "iODBC/sqlext.h"
    #include "iODBCinst/odbcinst.h"
    #else
    #include <sql.h>
    #include <sqlext.h>
    #include <odbcinst.h>
    #endif
    Last edited by OneEyeMan; August 1st, 2018 at 06:48 PM.

  4. #4
    Join Date
    Aug 2002
    Posts
    681

    Re: Link error after upgrade to MSVC 2017

    2kaud,
    Any idea how to make it work cross-platform/cross-compiler?
    I need this to work with gcc in C++11 mode and in Xcode (also with C++11) and with earlier compilers of MSVC.

    Thank you.

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,460

    Re: Link error after upgrade to MSVC 2017

    Quote Originally Posted by OneEyeMan View Post
    2kaud,
    Any idea how to make it work cross-platform/cross-compiler?
    I need this to work with gcc in C++11 mode and in Xcode (also with C++11) and with earlier compilers of MSVC.

    Thank you.
    Sorry, I now only use VS2017. All of our code has been updated for this. I can't help with x-compiler issues. Have you tried linking with legacy_stdio_definitions.lib?

    IMO, I would put the c++ headers first - before windows.h. The order we do this (unless good reason why not), is

    c++headers
    c headers
    windows.h and other window headers
    3rd party headers
    local headers
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.8.7)

  6. #6
    Join Date
    Aug 2002
    Posts
    681

    Re: Link error after upgrade to MSVC 2017

    2kaud,
    If you are interested, or for anybody else who is the solution is:

    Code:
    #if _MSC_VER >= 1900
    #pragma comment(lib, "legacy_stdio_definitions.lib");
    #endif
    Thank you.

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,460

    Re: Link error after upgrade to MSVC 2017

    Quote Originally Posted by OneEyeMan View Post
    2kaud,
    If you are interested, or for anybody else who is the solution is:

    Code:
    #if _MSC_VER >= 1900
    #pragma comment(lib, "legacy_stdio_definitions.lib");
    #endif
    Thank you.
    Yes, but this should be considered only a temporary 'fix' to get the solution to compile. The underlying issue should be identified and addressed. As I said in post #2, we initially did what you're done and linked to the legacy lib until we sorted out the source and got a clean compile without requiring it. This took us a few months.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.8.7)

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




On-Demand Webinars (sponsored)