为 Windows 编写纤薄的 C++ 程序(如 uTorrent)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1021210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Programming slim C++ programs (like uTorrent) for Windows
提问by Andrioid
I've always admired the original uTorrent program. It looked great, was less than 64kb, was extremely fast and had all the features I needed. Unfortunately the program is closed source (and becoming more bloated by the day) so I come to Stackoverflow for inspiration.
我一直很欣赏最初的 uTorrent 程序。它看起来很棒,不到 64kb,速度非常快,并且具有我需要的所有功能。不幸的是,该程序是闭源的(而且一天比一天变得臃肿)所以我来到 Stackoverflow 寻求灵感。
What methods do you recommend in writing fast, memory efficient and elegant programs on Windows?
在 Windows 上编写快速、内存高效且优雅的程序时,您推荐哪些方法?
While C# (and the whole .NET concept) are cool ideas I am more interested in 'purist' answers and the challenge of writing efficient, fast software for the Windows platform, much like the original uTorrent client. I don't mind allocating my own memory, doing my own garbage collection and creating my own data structures.
虽然 C#(以及整个 .NET 概念)是很酷的想法,但我对“纯粹”的答案以及为 Windows 平台编写高效、快速软件的挑战更感兴趣,就像最初的 uTorrent 客户端一样。我不介意分配自己的内存,进行自己的垃圾收集和创建自己的数据结构。
Recommendations on books, articles, libraries, IDEs (even efficient ways of getting more caffeine into my system) welcome.
欢迎对书籍、文章、图书馆、IDE(甚至是让更多咖啡因进入我的系统的有效方法)提出建议。
采纳答案by Eric
The Windows Template Library is geared towards what you want to do. It's a light-weight, template-based C++ wrapper for the Win32 API. With it, you don't have to go through the pain of direct Win32 coding, but it doesn't add a lot of overhead like MFC.
Windows 模板库面向您想要做的事情。它是用于 Win32 API 的轻量级、基于模板的 C++ 包装器。有了它,您就不必经历直接 Win32 编码的痛苦,但也不会像 MFC 那样增加很多开销。
回答by StackedCrooked
uTorrent is written in C++ and uses the old fashioned Win32 API. Google Chrome is also written this way, so why not download the source code and learn from their code?
uTorrent 是用 C++ 编写的,并使用老式的 Win32 API。谷歌浏览器也是这样写的,那么为什么不下载源代码并学习他们的代码呢?
回答by Crashworks
If you want to optimize for the smallest possible memory footprint and you don't mind jumping through a bunch of hoops that the .NET CLR was invented to take care of for you, then writing a direct Win32API app and hooking to GDI+is the way to go. Petzoldwas the definitive reference.
如果您想优化尽可能小的内存占用,并且不介意跳过 .NET CLR 发明来为您处理的一堆麻烦,那么编写直接的 Win32API 应用程序并连接到GDI+是一种方法去。Petzold是最终的参考。
Really, though, it's sort of a fool's errand, since the .NET runtime is going to be loaded into the OS's memory whether your app uses it or not, so you may as well link to it.
但实际上,这有点像傻瓜的差事,因为无论您的应用程序是否使用 .NET 运行时都将被加载到操作系统的内存中,因此您不妨链接到它。
回答by Tom Leys
The Demo Sceneis a group of people who spend their free time trying to make impressive and very small executables, which usually render something in 3d to music. Often the entire demo (code, music, 3d data) compiles into a single executable that is compressed to 64k or an impressively small size for the content.
该演示场景是一组谁花自己的空闲时间试图让令人印象深刻和非常小的可执行文件,通常呈现东西在3D的音乐人。通常,整个演示(代码、音乐、3d 数据)会编译为单个可执行文件,该文件被压缩为 64k 或非常小的内容大小。
You might draw some inspiration from the demos and learning about how they are made will inform your obsession to create small executables.
您可能会从演示中获得一些灵感,并了解它们的制作方式,这会让您痴迷于创建小型可执行文件。
Often, the key is to leverage as many 3rd party DLLs as possible that are installed with windows. Also, low level, custom coding of everything else is required.
通常,关键是尽可能多地利用 Windows 安装的第 3 方 DLL。此外,还需要对其他所有内容进行低级别的自定义编码。
回答by MSalters
The old "LIBCTiny"trick still works. With modern VC++ releases, you might need to turn of a few features.
旧的“LIBCTiny”技巧仍然有效。使用现代 VC++ 版本,您可能需要关闭一些功能。
Another good trick to know is the lstr*
collection of functions in Kernel32. That's already in memory, so those functions might be a leaner choice.
另一个要知道的好技巧是lstr*
Kernel32 中的函数集合。这已经在内存中,所以这些功能可能是一个更精简的选择。
回答by Robert Fraser
General: For smaller executables, #define WIN32_LEAN_AND_MEAN and VC_EXTRALEAN (assuming VS). Don't compile with debug symbols (you probably knew this). Use fewer libraries, and be user to only link the parts of libraries you need (VC's linker is pretty good about this, but don't touch optlink if you can help it).
一般:对于较小的可执行文件,#define WIN32_LEAN_AND_MEAN 和 VC_EXTRALEAN(假设 VS)。不要用调试符号编译(你可能知道这一点)。使用较少的库,并成为用户仅链接您需要的库部分(VC 的链接器在这方面做得很好,但如果可以帮助,请不要触摸 optlink)。
Strip the relocation headers : Go to http://www.paehl.de/cms/oldtoolsand search for "ReduceEXE" (direct download link: http://www.paehl.de/reduce.zip).
去除重定位标头:转到http://www.paehl.de/cms/oldtools并搜索“ReduceEXE”(直接下载链接:http: //www.paehl.de/reduce.zip)。
Run an executable packer: http://upx.sourceforge.net/... It uses up more memory at runtime and starts a bit slower, but the file is MUCH smaller.
运行一个可执行的打包程序:http: //upx.sourceforge.net/...它在运行时使用更多的内存并且启动速度有点慢,但文件要小得多。
If you care about file size more than speed, VC has an option to "optimize for size", which turns off some things like loop unrolling and function inling.
如果您更关心文件大小而不是速度,那么 VC 可以选择“优化大小”,这会关闭循环展开和函数内联等某些功能。
If you want to go hardcore (and don't care about all the software engineering advantages), try using fewer classes, preferring POS types without virtual functions instead. Wikipedia suggests that 6-13% of a program's execution time is spent doing virtual calls. Further, the vtables themselves take up (a LITTLE) memory, and size_t worth of memory at the beginning of every class instance (that has a virtual function) is allocated for the vtable pointer. IOW, "pure C" can end up being slightly faster (though if you find yourself emulating classes with function pointers, go back to C++).
如果您想成为核心(并且不关心所有软件工程优势),请尝试使用更少的类,而更喜欢没有虚函数的 POS 类型。维基百科建议程序执行时间的 6-13% 用于进行虚拟调用。此外,vtable 本身占用(一小部分)内存,并且在每个类实例(具有虚函数)开始时为 vtable 指针分配了 size_t 的内存。IOW,“纯 C”最终可能会稍微快一点(尽管如果您发现自己使用函数指针模拟类,请返回 C++)。
回答by AndreasT
Notepad++is also a very fast, highly optimized and very useful os program that could spark your inspiration. It's philosophy is similar to that of uTorrent. It uses the good old Win32 api which should still be the fastest you can go on Windows.
Notepad++也是一个非常快速、高度优化且非常有用的操作系统程序,可以激发您的灵感。它的理念类似于 uTorrent 的理念。它使用旧的 Win32 api,它应该仍然是您在 Windows 上可以运行的最快的。
If you want to go really artistic, the demo scene is the perfect place to go. Although their code is not always open.
如果你想要真正的艺术化,演示场景是一个完美的去处。虽然他们的代码并不总是开放的。