Standalone VS 2010 C++ Program

后端 未结 5 597
迷失自我
迷失自我 2021-01-24 09:50

it\'s been a long while since I\'ve used VS 2010 and C++, and as I\'m getting back to using it, I\'m running into the same problems that plagued me last year: the exe\'s that I

相关标签:
5条回答
  • 2021-01-24 10:32

    Firstly, before I actually give you the detail:


    Warning

    If you do this, things will be bad for two reasons:

    1. If there are security or other bugs in the MSVC runtimes, and you take this approach, they're baked into your app which means you need to re-distribute. DLLs are preferred because theoretically people use system update which means any errors get fixed.
    2. Everything else you compile into your exe also needs to do this. If you don't, you end up with two versions of the code and whatever you're using won't link.

    One possible solution is to bake the MSVC runtime into your application, by using the cl.exe option (C/C++ compiler settings) /MT which means multi-threaded version of the C/C++ runtime linked statically. As I said, if you try to link against something that is linked itself dynamically to the runtime, you're going to end up in a mess. Also, as I said, this represents an additional security risk factor, so bear that in mind.

    The other options are to write an installer that can either download the appropriate runtime, or include the DLL needed.

    If you're using some feature of the runtime that exceeds a certain version of Windows (generic statement, but it does happen) then you should be able to use the Windows SDK to target various versions of Windows using appropriate C runtimes.

    0 讨论(0)
  • 2021-01-24 10:36

    http://www.microsoft.com/downloads/en/details.aspx?FamilyID=a7b7a05e-6de6-4d3a-a423-37bf0912db84

    google text: visual studio c++ redist

    Do not statically link to the runtime; specifically don't do so if you're using any kind of dll for other purposes. It introduces all kinds of bogus problems wrt heap management that you probably don't want to mess with.

    0 讨论(0)
  • 2021-01-24 10:42

    This previous answer should hold true for VS2010. I still build with VS2005, but all my apps use the static CRT for the sole reason of being able to run across old and newer machines alike.

    0 讨论(0)
  • 2021-01-24 10:44

    The easiest thing to do is to just install the VC++ Redistributable Package. It has both x86 and x64 versions.

    0 讨论(0)
  • 2021-01-24 10:50

    Open the properties dialog for your project and select Configuration Properties | C/C++ | Code Generation. The default setting is Multi-threaded DLL. Change that to Multi-threaded and you'll be building and .EXE with the run-time statically linked in. Don't forget to do the same for the debug version.

    If you're using MFC or ATL, you will need to navigate to Configuration Properties | General and set "Use of MFC" or "Use of ATL" to link statically as well.

    NB: If you link the runtime statically, you must make sure that any other library you're linking in also links it in statically. Otherwise you'll wind up with two copies of the runtime in memory, each with its own heap and bad things will happen when code using one runtime tries to free an object allocated by the other runtime.

    0 讨论(0)
提交回复
热议问题