12 ms·
Some sanity for C and C++ development on Windows
- habibur 5y agoIf I were to develop native Windows app now, I would still go for C-Win32-GDI, instead of sorting through all those jungle of stacks that had been coming and going in the last 20 years.
- zenlot 5y agoI'd choose Delphi instead for native Windows apps. This is where it still shines.
- Koshkin 5y agoAnd with Wine you have some hope for portability.
- spaetzleesser 5y agoqt would be a good candidate too.
- oneplane 5y agoUnless you're writing something low-level like a driver or representation engine (game engines or CAD/CAM or something like that), why would you still have to write native Windows applications? I do see how a native application generally behaves and performs better, but it's a whole lot of extra work vs. a less-than-native application. I'd even opt for a generic backend (say, Rust or Go based) and simply having the 'interface' be OS-native.
- gefhfffh 5y agoYou can develop native windows apps in Rust these days - even through the new old win32 APIs
- no_time 5y agoPride in one's craft. It starts to make a lot of sense when you are optimizing for anything other than profit.
- pjmlp 5y agoLife sciences laboratory devices, DAW, factory automation, air gaped dashboards, for some examples.
- oneplane 5y agoSo essentially the representation engines (specialised science on Windows, DAW on Windows), but factory automation or air gapped dashboards, I doubt either needs Windows or 'native' applications on a desktop? Maybe a Windows-based HMI or something, but other than that I haven't seen any implemented like that. Factory automation at TSMC and the likes is mostly just Java, web and webbased interfaces all in a closed network. Same for most modern logistics factory automations. Classic PLC-based HMI interfaces from Siemens and the likes do still come with Windows as a dependency, but those have normal HTTP interfaces as well. Besides, this is such a niche with so much money going around they hardly depend on the ability to work inside of Microsofts limits. Calls into private APIs galore, hence the limits on OS patches and upgrades that might break those undocumented interfaces...
- pjmlp 5y agoMost drivers for such hardware are based on DLLs or COM, and they aren't going to change any time now.
- oneplane 5y agoAs far as I can see most are using the same protocols they always have over the buses they have always supported, no special driver applies. But even then, why limit the end-user application to a Windows desktop native application in C or C++ when you can also just have it run a communication server and do the client-side things in a more universal implementation. Makes for better compartmentalisation anyway, like the example of the closed networks of chip manufacturers. The chip machines and control systems run linux, but the desktops can be anything with a TCP/IP stack and a browser.
- pjmlp 5y ago
- userbinator 5y agoIt's definitely not "a whole lot of extra work". The same attitude as yours has spawned an entire culture of repulsive inefficiency and egregious waste. Hundreds or even gigabytes of memory for a bloody chat client which still manages to lag noticeably on 2019-era hardware? How is that even acceptable? ...all because of some misguided and incredibly selfish notion that "developer experience" somehow trumps that of however many users there are (and because a lot of this is corporate shit, we have no choice but are forced to use it.)
- jcelerier 5y agoIf were to develop a native Windows app 20 years ago, I'd have picked Qt, in these 20 years I'd have relatively simple changes to make and in exchange I'd get portability to many more places and great performance https://www.qt.io/blog/2018/05/24/porting-from-qt-1-0 https://www.qt.io/blog/2018/05/24/porting-from-qt-1-0 I'd do the same today, definitely !
- tored 5y agoI can recommended PureBasic. You can call win32 API directly from PureBasic if you need to. Easy to both build and call static and dynamic libraries. https://www.purebasic.com/ https://www.purebasic.com/
- topspin 5y agoThe classic Win32 GDI API isn't the worst thing imaginable. It has been the basis of untold amounts of successful GUI software and it still works, although much of the latest capability of Windows is hard to reach using it. The best GUI development experience I had on Windows aside from that was WTL/ATL. I kept hoping for a long time that this framework would reemerge as a first class GUI platform on Windows. I think Microsoft missed a tremendous opportunity by neglecting it. It actually made writing COM components enjoyable and appealing.
- pornel 5y agoWindows is what annoys me about C's portability story. C prides itself on supporting every platform, including exotic and dead ones, but only hypothetically. The spec is all about the potential. All the portability promises end the moment you try to compile anything. It doesn't work, but that is no longer C's problem. It's simultaneously the most portable language in the world, and one that barely works on the most popular desktop OS.
- ziml77 5y agoI wonder if people would still call C portable if there were more non-POSIX-compliant operating systems
- flohofwoe 5y agoThe ANSI/ISO C standard library and the POSIX standard are two different things though that just happen to overlap here and there.
- ziml77 5y agoMy point is that a lot of people treat the POSIX standard as if it was the C standard library. It's understandable because of how limited the actual standard library is, but it leads to people having a view that an OS is doing things wrong if it's not following POSIX.
- dundarious 5y agoI think the proliferation of POSIX-like OSes is mostly down to OS projects wanting there to be useful software that can run on their OS, so they minimize/eliminate the work of porting. It has led to the phenomenon you describe though -- instead of thinking an OS is just too much work to port to, it might be thought of as being "wrong".
- pjmlp 5y agoWorse is when the OS does follow POSIX but people haven't read the fine print that certain POSIX APIs are implementation dependent, e.g. signal handling and threads.
- gecko 5y agoThe title of this article had me do a double-take: C and C++ development on Windows is great. No sanity is needed. But that's not what the article is about. What the article is about is that the C runtime shim that ships with Visual Studio defaults to using the ANSI API calls without supporting UTF-8, goes on to identify this as "almost certainly political, originally motivated by vendor lock-in" (which it's transparently not), and then talks how Windows makes it impossible to port Unix programs without doing something special. I half empathize. I'd empathize more if (as the author notes) you couldn't just use MinGW for ports, which has the benefit that you can just use GCC the whole way down and not deal with VC++ differences, but I get that, when porting very small console programs from Unix, this can be annoying. But when it comes to VC++, the accusations of incompetence and whatnot are just odd to me. Microsoft robustly caters to backwards compatibility. This is why the app binaries I wrote for Windows 95 still run on my 2018 laptop. There are heavy trade-offs with that approach which in general have been endlessly debated, one of which is definitely how encodings work, but they're trade-offs. (Just like how Windows won't allow you to delete or move open files by default, which on the one hand often necessitates rebooting on upgrades, and on the other hand avoids entire classes of security issues that the Unix approach has.) But on the proprietary interface discussion that comes up multiple times in this article? Windows supports file system transactions, supports opting in to a file being accessed by multiple processes rather than advisory opt-out, has different ideas on what's a valid filename than *nix, supports multiple data streams per file, has an entirely different permission model based around ACLs, etc., and that's to say nothing of how the Windows Console is a fundamentally different beast than a terminal. Of course those need APIs different from the Unix-centric C runtime, and it's entirely reasonable that you might need to look at them if you're targeting Windows.
- jcelerier 5y ago> The title of this article had me do a double-take: C and C++ development on Windows is great. No sanity is needed. we certainly have a different opinion on what "great" means. It takes less time to rebuild my whole toolchain from scratch on Linux (~15 minutes) than it takes to MSVC to download those friggin debug symbols it seems to require whenever I have to debug something (I sometimes have to wait 30-40 minutes and I'm on friggin 2GB fiber ! and that's seemingly every time I have to do something with that wretched MSVC !) Thankfully now the clang / lld / libc++ / lldb ... toolchain works pretty well on Windows and allows a lot more sanity but still, it's pretty slow compared to Linux.
- feldrim 5y ago> On Windows, C and C++ is so poorly hooked up to operating system interfaces that most portable or mostly-portable software — programs which work perfectly elsewhere — are subtly broken on Windows. I believe that the problem is about the definition of "elsewhere". The elsewhere is called POSIX. The portability is based on POSIX. So the article can be summed as "Windows is not POSIX compliant".
- feldrim 5y ago> The UTF-8 encoding was invented in 1992 and standardized by January 1993. UTF-8 was adopted by the unix world over the following years due to its backwards-compatibility I'd like to add some historical background the UTF-8 problem mentioned there. UTF-8 was standardized in 1992. But the first Linux using UTF-8 as the default encoding was RedHat in 2002 [1]. 10 years later! On the other hand, Windows started using Unicode before it was standardized, and back then there was no UTF-8. Or as Raymond Chen put it, "Windows adopted Unicode before the C language did" [2]. [1] https://www.cl.cam.ac.uk/~mgk25/unicode.html#linux https://www.cl.cam.ac.uk/~mgk25/unicode.html#linux [2] https://devblogs.microsoft.com/oldnewthing/20190830-00/?p=102823 https://devblogs.microsoft.com/oldnewthing/20190830-00/?p=10...
- flohofwoe 5y agoThere was a time window of a few months between the invention of UTF-8 and the release of Windows NT with UNICODE support. If the two involved parties would have known of each other (and assuming the Windows devs would have understood what a great deal UTF-8 was), I guess Windows could have used UTF-8 right from the start.
- jcranmer 5y agoThe addition of Unicode support to Windows NT was probably done a great deal in advance of its actual release--it's not the sort of thing you can throw in the last minute. It's also worth noting that dealing with UTF-8 correctly is more than just declaring "the encoding of this set of characters is UTF-8"--it does require you rethink how string APIs are designed to work well. If the alternative is a 16-bit fixed-with character format, UTF-8's variable-width format doesn't necessarily look like a wiser idea. It's not until 1996, when Unicode moves from 16-bits to 25-bits, that UTF-8 actually looks like a good thing.
- bullen 5y agoThe only flaw Windows has is that you are not allowed to redistribute the cl.exe compiler. For my game engine it's a 35MB zip! https://developercommunity.visualstudio.com/t/allow-redistribution-of-the-clexe-compiler/975508 https://developercommunity.visualstudio.com/t/allow-redistri...
- jcelerier 5y agoI'm doing it with clang. As a bonus I get better performance than I did with msvc.
- bullen 5y agoI tried looking at all other compilers, the only that fit my size constraints was tcc... that would mean dropping std::string and only use ASCII char*... not a big deal for my game because my .ttf fonts dont have anything else but for other projects throwing UTF-8 out of the window might be a show stopper!? Also considered Rust, but that is so bloated/slow! Can't stop anyone from using Rust for the game in the future though, because any .so/.dll will be able to hot-deploy.
- jcelerier 5y agoI don't understand, shipping 50mb of cl.exe is fine but shipping clang isn't ?
- bullen 5y agoShipping cl.exe is illegal, I tried looking at clang but it was alot bigger than 50MB, and it was unclear how to untangle it from installers and other dependencies: https://stackoverflow.com/questions/65807034/clang-llvm-zip-file-installer-for-windows https://stackoverflow.com/questions/65807034/clang-llvm-zip-...
- jcelerier 5y agoI'm shipping it as part of https://ossia.io https://ossia.io (statically linked to the app) ; my installers are between 50 and 100mb for something that ships clang+llvm, boost, qt, ffmpeg and a lot of other things (in addition to its own code): https://github.com/ossia/score/releases/tag/v3.0.0-rc7 https://github.com/ossia/score/releases/tag/v3.0.0-rc7 you just need to build llvm/clang statically and target_link_libraries(<couple stuff>) in cmake (and ship the headers if you want to do useful things, this actually takes much more space uncompressed but it'd be the same whatever the compiler)
- pjmlp 5y agoSanity only required for those that fight against the platform and insist in carrying their UNIX habits to every platform they touch. Embrace C++ Builder, Visual C++ or Qt. No sanity medicine required.
- ColonelPhantom 5y agoWith the exception of Qt, none of those platforms are, in any way, shape or form, cross-platform. Visual C++ is Windows-only, and C++ Builder supports only Windows and iOS (??). So you don't solve the problem, merely invert it. Instead of everything-except-Windows, you now support only-Windows. I also don't see how using VC++ for example circumvents API issues, since it's just a compiler. I can imagine that Qt might solve some issues, since it offers a very wide set of abstractions. As such, it can probably do things like convert string formats on-the-fly between getting it from the OS and passing it to your application.
- pjmlp 5y agoThis thread is about developing on Windows....
- asveikau 5y agoI've done a lot of Win32 development in C and C++. People here are saying don't expect it to be Unix, and that's fine. When I write for Windows, everything is PWSTR and that's fine. But I have seen a lot of people get tripped up on this. People who are not well versed in the issue have no idea that using fopen() or CreateFileA() will make their program unable to access the full range of possible filenames, varying with the language setting. I've absolutely seen bugs arise with this in the 21st century. The old "ACP" is meant for compatibility with pre-unicode software, but that hasn't been a big concern for something like 20 years. It is a much bigger issue that people are unaware of this history and expect char* functions to just work.
- lazyjeff 5y agoI have done a lot of Win32 as well, and recently started a new project after a long haitus. The problem is there are some very occasional things that use char*, and so you have to keep that in mind: for example, some parameters in wWinMain, many cross-platform libraries that I want to link to, or some of the C++ library functions mentioned in that article. Converting between char* and wchar_t requires being a bit careful not to cause a buffer overflow. It's a lot of mental overhead to deal with it all, but you're right that if you're mainly just calling Win32, then sticking with the wide version of functions and strings will mostly be fine. Microsoft could certainly make this easier by offering a "wWin32" project that only has the wide version of libraries and documentation to prevent mistakes.
- q-big 5y ago> Microsoft could certainly make this easier by offering a "wWin32" project that only has the wide version of libraries Using API functions ending with ...A (for the ANSI version) like CreateFileA is actually rather lowlevel style and means that you know what you do (the same holds for the ...W versions) and explicitly want it this way. Normally, you use CreateFile and Visual C++ by default sets the proper preprocessor symbols such that CreateFileW will be used. If you pass a char*, you will get compile errors. In other words: the infrastructure is there to avoid this kind of errors - and it is even used by default. But if you make it explicit in your code that the ANSI version of an API function (CreateFileA) should be used instead of using CreateFile, don't complain that the compiler does obey and this turns out to be a bad idea.
- gavinray 5y agoI want to point out that this article doesn't acknowledge this major change, regarding ANSI/Unicode API variants in Windows: > "Until recently, Windows has emphasized "Unicode" -W variants over -A APIs. However, recent releases have used the ANSI code page and -A APIs as a means to introduce UTF-8 support to apps. If the ANSI code page is configured for UTF-8, -A APIs operate in UTF-8. This model has the benefit of supporting existing code built with -A APIs without any code changes." https://docs.microsoft.com/en-us/windows/apps/design/globalizing/use-utf8-code-page https://docs.microsoft.com/en-us/windows/apps/design/globali... It seems not everyone is aware of this, too
- garaetjjte 5y ago>I want to point out that this article doesn't acknowledge this major change But it does? In "How to mostly fix Unicode support" section.
- kazinator 5y ago> I’m excluding Cygwin and its major fork, MSYS2, despite not inheriting any of these flaws. They change so much that they’re effectively whole new platforms, not truly “native” to Windows. Ah, but Cygwin doesn't actually change so much that we can't scale some of it back, to have sane C and C++ development on Windows. https://www.kylheku.com/cygnal/ https://www.kylheku.com/cygnal/ Build a Cygwin program, then do a switcheroo on the cygwin1.dll to deploy as a Windows program. I developed Cygnal to have an easy and sane way of porting the TXR language to Windows. You can control the Windows console with <termios.h> and ANSI/VT100 escapes. Literally not a line of code has to change from Linux! In Cygnal, I scaled back various POSIXy things in Cygwin. For instance: - normal paths with drive letter names work. - the chdir() function understands the DOS/Windows concept of a logged drive: that chdir("D:") will change to the current directory remembered for the D drive. - drive relative paths like "d:doc.txt" work (using the working directory associated with the D drive). - PATH is semicolon separated, not colon. - The HOME variable isn't /home/username, but is rewritten from the value of USERPROFILE. (I had problems with native Windows Vim launched from a Cygwin program, and then looking for a nonexistent /home due to the frobbed variable, so I was sure to address this issue). - popen and system do not look for /bin/sh. They use cmd.exe! - when exec detects that "/bin/sh" "-c" "<arg>" is being spawned, it rewrites this to cmd.exe. The benefit is that interpreter programs which expose command invocation functions which they implement from scratch by forking and execing /bin/sh -c command are thereby retargetted to windows. - the /cygdrive directory is gone - /dev is available as dev:/ so you can access devices.
- 6581 5y ago> https://www.kylheku.com/cygnal/ https://www.kylheku.com/cygnal/ Your SSL certificate is invalid and your server uses TLS 1.0 which is insecure and disabled by default in recent browsers.
- kazinator 5y ago> your server uses TLS 1.0 That is correct. Basically, the OS upgrades kept breaking my highly customized setup, resulting in downtime and wasted time, so eventually I dropped out of the program. I need to rebuild a new server and migrate everything over. With two small kids now and full-time job, if I have any block of free time (basically late at night or early morning), I prioritize putting it into working on code. It's not just a web server setup and custom things around it, but Postgress databases, e-mail setup and other stuff. Every item could have some issue chewing up hours. > which is insecure Secure/insecure isn't a simple Boolean. It's less secure than newer versions of the protocol, but it's not "insecure" like, say, a plain password sent over telnet. > Your SSL certificate is invalid I don't believe that is the case. Firstly, I've never had an problem connecting to the site using https from any browser. Secondly, most online SSL checking tools do not report a problem other than remarking on it being self-signed. For instance geocerts.com says "The hostname (www.kylheku.com) matches the certificate and the certificate is valid." I found one which doesn't understand wildcard names: it reports that www.kylheku.com doesn't match *.kylheku.com. That's a bug in the checker. One other flags some unspecified issue with the Alternative Names property (which I didn't even specify when generating the cert).
- geofft 5y agoShould new programming languages built around internal use of UTF-8 be opting their binaries into this behavior by default? (I'm mostly thinking about Rust, which I think has to do some contortions on Windows. Probably also Go, to the extent it uses native APIs; arguably Python too.)
- ChrisSD 5y agoThe Rust standard library has contortions in order to support invalid UTF-16. To do this it uses an encoding called "WTF-8"[0], which is an extension to UTF-8. If the standard library dropped this requirement and only supported valid Unicode then it could simply use normal UTF-8 instead of WTF-8. [0]: https://simonsapin.github.io/wtf-8/ https://simonsapin.github.io/wtf-8/
- int_19h 5y agoThe problem right now is that forcing the codepage to UTF-8 only works on Windows 10 build 1903+. So quietly forcing this opt-in may result in situations where everything works fine for the devs, but some of their users see breakage for no clear reason. Long-term, though? Absolutely.
- gigel82 5y agoYou can set UTF-8 system-wide for 3+ years; I've been doing it on all my boxes and never had issues. I suspect in a few years this will just be the default: https://en.wikipedia.org/wiki/Unicode_in_Microsoft_Windows#UTF-8 https://en.wikipedia.org/wiki/Unicode_in_Microsoft_Windows#U...
- WalterBright 5y agoD was initially designed (around 2000) to be agnostic about UTF8, UTF16, and UTF32. The language has full support for all three. But it gradually became clear that UTF8 predominates. D best practice now is to do all processing in UTF8. When encountering UTF16 or UTF32, promptly convert it to UTF8, do the processing, and then convert to UTF16 or UTF32 on output. In practice, the only time this is needed is to interface with Windows. All the D standard library code that interfaces with Windows converts to/from UTF8 to UTF16. If I was doing a do-over for D, there wouldn't be core language support for UTF16 or UTF32, only library support.
- ChrisSD 5y agoUTF-32 was an interesting choice. Was that done for the sake of completeness or was there a specific use case for it?
- WalterBright 5y agoIt was done for completeness. I was accused of being an anglophile at one point for not doing everything in UTF-32 :-)
- layer8 5y agoCan anyone elucidate whether the new UTF-8 codepage allows working with filenames that aren’t valid UTF-16 (i.e. containing mismatched surrogate code points)? In general I think it’s best to stick with the native "W" functions and/or wchar_t on Windows.
- ChrisSD 5y agoIt does not. Invalid UTF-16 (aka unpaired surrogates) are replaced by the Unicode REPLACEMENT_CHARACTER (�) However, in practice such filenames are only produced by malicious software and many large products won't work with them anyway (e.g. Microsoft's own VSCode).
- tsujp 5y agoI've recently started dabbling in some C development on Windows, particularly drivers, and a lot of the newer APIs Microsoft provides are C++ only headers so unless I add `extern "C"` to everything myself (and thus now also need to maintain that with any updates Microsoft issues) how can I interact with those APIs or use those headers in pure C? I don't see how and it's quite frustrating.
- leecommamichael 5y agoI understand that _not_ recommending an alternative to the C-family keeps the post focused, but it does feel odd not to name one language that works well. Perhaps that's an indication of something...