11 ms·
How to Write Portable C Without Complicating Your Build
- stinos 10y agoThe nice alternative is MinGW(-w64) with MSYS or Cygwin supplying the unix utilities, though it has the problem of linking against msvcrt.dll You can use MingW's make but compile with cl instead of gcc and that problem is not present. You will have to check if all compiler flags are supported though. Or use a wrapper around cl which translates compiler flags, pretty sure that exists already. My preferred approach lately is an amalgamation build That won't scale well though. For large projects you'll really want to use something as mentioned above, or have seperate Makefiles and VS projects, or use a build file generator like CMake. edit another thing I wanted to add: instead of using #if defined(_WIN32) void foo() { //win implementation } #else void foo() { //unix implementation } #endif all over the place another option is to split the implementations over source files like impl/win32.c and impl/unix.c then have the build system deal with selecting the correct one. Especially when there are more than a couple of platforms this is much cleaner and more convenient.
- petters 10y agoCompletely agree about Cmake. The language may be quirky, but it has solved all the hard problems. Often the build files will be trivially short as well.
- exDM69 10y agoYes, I've been using CMake again after a few years of something else. I came back to CMake because it seems to be the build system that sucks the least. I use CMake with Ninja, which is faster and nicer than plain old Make. CMake completely falls apart if you're trying to do anything else than normal user space applications that link with commonly used libraries. Some years ago I tried building a bare metal project with CMake and the job got done but the experience was atrocious. But CMake really shines in building "normal" applications and in particular cross compiling them.
- pjmlp 10y agoAlso at least for those of us that care about C++, Google and Microsoft's adoption of it pretty much settles the question which build system to use.
- Keyframe 10y ago#if defined(PLATFORM) I would recommend against that as well. I have a directory structure where there's a module/hello.c (stub or generic, platform independent code) and then there's module/platform/hello.c which my Makefile selects over the generic one based on the platform. Primary issue is that there's some code duplication, and it's hard to keep .h files general across platforms all the time (not possible all the time), but in general it does make life a lot easier - especially if platform number grows over time or certain are removed.
- logicchains 10y agoThat's quite similar to how Go does it (Go would have something like hello_platform.c), which in my opinion works quite well. I actually find it to be one of the nicest things about Go, compared even to Haskell, which for some bizarre reason uses the C preprocessor (!!) to pepper code with #ifdefs.
- ycmbntrthrwaway 10y agoGo took this idea from Plan 9. In fact, Go shares a lot with Plan 9 C compiler structure. Related: http://doc.cat-v.org/henry_spencer/ifdef_considered_harmful http://doc.cat-v.org/henry_spencer/ifdef_considered_harmful
- Keyframe 10y agoInteresting! I never went in-depth into Go, apart from spending an afternoon with it to have a go at it.
- marcosdumay 10y ago> for some bizarre reason uses the C preprocessor (!!) to pepper code with #ifdefs. I always thought that was funny. You are using the most powerful (of the widely used) language available for transforming text, but must fall back to the C preprocessor.
- pjmlp 10y agoThat has always been my approach as well, much easier than #ifdef spaghetti.
- kodfodrasz 10y agoWhy are people still so obsessed by C? There are better alternatives for almost any usecase. Where there are none, there portability is a non-issue (eg. embedded stuff. although C is also not especially suitable for that use, yet still prevails, because of the existing tooling.)
- exDM69 10y agoObsessed? I don't think so. It's just that C has been around for 40 years, it'll stay around for at least another 40 years. You can still compile source code from 20-30 years ago with little or no modifications. C gets the job done fast and efficient. That said, I'm waiting for a good excuse to learn Rust. Prior to that there have been very few alternatives to C.
- kodfodrasz 10y agoI've seen C used for safety critical software. I've used it for that. I've seen the insane amount of tooling and support and processes and rules to make it suitable for the task it is utterly not suitable for. It does not get the job done fast and efficient, if you consider the develpment costs. The fast code the compiler generates could be genrated from other sources just as well, it's not especially the merit of that language.
- exDM69 10y agoI write security critical software with C and I know exactly what you're talking about. It's written in C because the tooling to analyze and certify it for security in embedded/automotive/aerospace is targetting C (or C++). For some industries, Ada might be an alternative. It may be paradoxical, but you're not going to be able to write safety critical software in Rust because the tooling to certify it for security doesn't exist and it'll still take years to get there. Do I like this situation? No, I do not. Do I think it's a big problem? No, not big enough that it can't be solved by pouring money and engineering resources on it.
- 10y ago
- vbernat 10y agoThe article downplays a little bit the value of the autotools. It's not just about building for 30-year ago hosts, it's also about providing a lot of convenient tools and providing the user with a known interface. If you write your own configure, people may expect to be able to pass additional CFLAGS (not overriding the existing one), setting the path to various elements, installing in a different root, having certain targets for the generated makefiles (like dist, clean, install, uninstall), having cross-compilation work (including crosscompiling to Windows with mingw64 which solves the toolchain problem of releasing binaries for Windows), ... It's a tedious task and easy to get wrong. On the other hand, using the autotools in a modern way is dead easy. You don't need to add many many tests if you don't intend to support old stuff. You get access to automake which is a fantastic tool on its own. Don't read old tutorials, don't look at how big established projects are doing, look at the Autotools Mythbuster instead (autotools.io) and start with a minimal configure.ac.
- Sir_Cmpwn 10y agoIf you use a simpler configure solution, then the user probably won't have much trouble figuring out how to deal with niche edge cases that they expect to work with autotools. On the other hand, when something goes wrong with autotools, the first step is to pour yourself a drink.
- pksadiq 10y agoThe m4 macros (autoreconf) and ./configure is too slow when it can be not. Of course './configure -C' may speed up running it a bit. Still not fast enough. I really feel a need for something like AC_TRUST_HOST or AC_TRUST_DISTRO or something like that would not check for each header or each function in some library. Instead it will check the version of GCC, libc, and compile some (hopefully just one) program and test. Of course the common tests shall be skipped only in popular distributions. Say, Debian, Arch, Fedora (and their derivatives), to name a few.
- zbuf 10y ago> If you write your own configure, people may expect to be able to [...] It's a tedious task and easy to get wrong. The author's point is to do <em>without</em> a configure step entirely, not write your own. A standard Makefile is perfectly capable of implementing these things in a manner which is clean, reasonably portable and without the level of indirection that makes things hard to debug. My own experience is that autotools has evolved to feel less standard than the modern platforms it purports to smooth over the differences in; I seem to frequently find that I can't generate a 'configure' for a 3rd party software or libraries from Git repositories because I of the wrong autotools versions. Efforts to investigate this by unpicking the various macros etc. provided in the build have almost always been unsuccessful, leaving me building from distributed .tar.gz files. With something that feels like such a moving target after 30 years, I'm glad to see people realising the benefit of a simple Makefile that's easy to customise for the edge cases.
- metafunctor 10y agoIn the past, I've wrangled with autotools more than I'd care to admit. I spent very many hours indeed perfecting my automake files, configure scripts, libtool and gettext integrations, making sure you can pass in CPPFLAGS and LDFLAGS as one might expect, cross compiling, with all the .in and .in.in and .h.in files, with all the m4 macros, publishing some of my own autoconf macros, all the while figuring out how to make it all work on Linux, mingw, the various bsd derivatives, AIX, Solaris, HP-UX, OS X, Visual C compilers, and what have you. From the developer's perspective, it is an absolute nightmare – and I consider myself a seasoned professional with the various unix-like systems and their shells. Even with this deliberate care, dedication, and time spent preparing the scripts beforehand, the build usually doesn't work properly out of the box on a new platform. My mistake was in thinking that the value in autotools system is that you can build a system that will compile for anything. I imagined all you need to do is use (or create) feature tests to sniff out any differences between platforms, and your code will build and work anywhere. The real value of autotools is for the user. The user can run "./configure && make && make check && make install". Don't make the mistake I did, thinking that autotools will save you time. It absolutely will not.
- ufo 10y agoI really wish there were an alternative to the autotools that offered that configure make install interface without the nightmarish developer experiente. Perhaps by compromising on the portability, if that is the only way.
- contras1970 10y agohttps://github.com/roman-neuhauser/motoconf https://github.com/roman-neuhauser/motoconf aims for that space, sadly it's very incomplete and dormant.
- Sir_Cmpwn 10y agoI wrote a simple PoC of a simpler way to achieve something similar: https://github.com/SirCmpwn/scmake https://github.com/SirCmpwn/scmake It's definitely not something you should use, but I hope new things take a similar approach. It's small, give it a read. Here's a somewhat more complicated project that uses it: https://github.com/SirCmpwn/libccmd https://github.com/SirCmpwn/libccmd I think from a usability standpoint (both for devs and users) it's really great but the internals could use some work.
- ryanpepper 10y agoI don't mind Make for small projects, but it can be a bit of a nightmare for large ones - there are some very unwieldy Makefiles out there. It gets more complicated when you want to have several libraries and/or executables in a project, each of which link against different external libraries. I've always found CMake makes this much easier IMO, and while the language is a bit janky, it does the job.
- Keyframe 10y agoMake is kind of write only. I have this library of mine for image and graphics processing that I wrote and which I'm rolling through years. In the beginning I wrote in all this functionality I thought I needed, basically an architecture of project and it does what I need - I can make a static or dynamic library, it works on Windows, MacOS, and Linux, it has "modules" which can be overriden based on if there's a certain subdirectory within a module with OS/Platform name (basically a compile time multiplatform specialisation) and several other things.. It works and is rock solid. However, if you've asked me now what's in there and why and how... Just by glancing at that spaghetti code in there I know in general what's where, but damnit if I would be brave enough to make significant changes to it. It works though and it works great. I must also point out that I use gcc (different versions) and clang here and there on all three platforms and I change compiler/tools in the environment itself, not within Makefile. If I would to do it again (I'm not active developer anymore though - it's more for personal use now) I would do it again with make. There's certain straightforwardness to it when writing it, and from my experience there's zero to almost none maintenance, but ymmv of course. I would probably look into CMake if I were using vastly different compiler tools (configuration-wise), but I'm not.
- newhouseb 10y agoOne of the nice (perhaps underappreciated) side effects of Rust has been first-class support for all major platforms. If you're linking against other Windows bits you still need to think about mingw vs. msvc, but beyond that you don't need to customize/hack up a build system to get things compiling sanely on both. I recently switched back to C for a particular project and there's been quite a bit of reverse-culture shock when it comes to setting up builds & projects.
- pjmlp 10y agoRust still doesn't have first class support for UWP.
- sqeaky 10y agoUWP? EDIT - I should clarify. I google UWP and it could be Universal Windows Platform. If so, how is that different than the windows builds that Rust does support?
- pjmlp 10y agoThink of UWP as Windows Longhorn reborn. It is the same programming model as .NET, but built on top of COM and just the minimal set of Win32 APIs to support COM based APIs. It is also sandboxed, just like on OS X. For Rust to support UWP, it needs to be able to consume COM with the new UWP semantics and at the same time expose traits as COM interfaces accessible to any programming language able to consume UWP libraries.
- sqeaky 10y agoThat doesn't sound like a good idea at all, any of it. Why is microsoft doing this?
- inferiorhuman 10y agoRust is pretty nice if a bit rough with the cross compilation stuff. The big problem is speed. Compiling rust projects takes a while and some operations like reading a file line b line are absolutely glacial.
- antirez 10y agoJust to reality-proof that: Redis managed to survive 7 years without autotools so far, using similar techniques, you just type "make" regardless of the unix system you want to compile Redis with. No major complexity was faced in order to do so, however Redis depends on nothing externally if not libc. For software projects with many external deps it may be worth to use autotools instead.
- qznc 10y agoD is a tiny bit more with two options: `make -f posix.mak` or `make -f win32.mak`.
- antirez 10y agoRedis does not have a win32 target, otherwise probably that would be needed as well for simplicity.
- sporkenfang 10y agoPeople can really easily abuse the Makefile ecosystem and write complicated, difficult to read, spaghetti builds. If something fails somewhere in the m4 macro forest while building on your specific machine, you're hosed for potentially hours or days while trying to figure it out so you can just build the damn project. The whole point of this kind of tooling should be to take some of the heavy lifting out of getting source code built, not add extra complexity to a project. I've been working on and off with Make, C, autoconf, etc for ten years. Maybe if I were a full-time C or C++ person I'd have less trouble with this, but it seems rather excessive. The thing that most discourages me from contributing to certain open source projects is when I clone the repository and the build fails and I can't get the project to build in an hour or less of build-debugging time (and I'm not on a weird setup, I'm on a modern Lenovo with Debian).
- cperciva 10y agoTo add another example: spiped is 6 years old and should build on any POSIX system with the Software Development Utilities option, /dev/urandom, and OpenSSL. Over the years I've collected a handful of workarounds for not-quite-POSIX systems, but the "check if we need any workarounds" step of the spiped build is still much faster and simpler than using autotools.
- ape4 10y agoFor the thorny Windows platform: how about using cygwin so you have the shell and regular Linux commands but use the Visual C++ compiler for a more native exe.
- coldtea 10y ago>The man page also documents secure_getenv(), which is a GNU extension: to be avoided in anything intended to be portable. GCC itself is portable, so?
- ptman 10y agosecure_getenv() first appeared in glibc 2.17. not GCC
- andreareina 10y agoThat's like arguing that because cows are vegetarian, so is beef. Portable means that any system implementing the standard interface can use it; secure_getenv isn't part of the standard, therefore it's not portable.
- coldtea 10y agoNo, it's more like arguing that if one is OK with the (lots) of platforms GCC covers, then it shouldn't matter if some extension is GCC specific. Your program is still portable for your use cases.
- andreareina 10y agoNot everyone uses GCC, or glibc.
- liveoneggs 10y agoI will write this rant here because it will get a bigger audience than any blog I would have. Speaking of someone who occasionally ports software to less common environments I have a few things to say that I was hoping to find in this article. First the case for and against autotools: autotools are well supported by every packaging system out there (freebsd ports, pkgsrc, apt, rpm, etc) where it's really just a line or three to do the build in the package definition. The main things that are not usually thought about by developers but are loved by porters and packagers (and come out of the box with autotools) are: DESTDIR support (installing into an intermediate path, so DESTDIR/PREFIX/usr/bin/myapp) and cross-compilation, which is one of the biggest strengths of using C in the first place! The biggest argument against autotools is that feature discovery at build time is complete bullshit. I hate it. It reduces determinism and ties execution environment to the build environment. So if you can support DESTDIR and cross compilation without autotools, go for it! It's not that much extra work! --- The second point I was hoping this article would address is project layout. In the last few years a nice, standard-ish layout for portable projects has emerged but package maintainers end up having to teach it to every new (big) project. Here are examples where it has gone okay: https://github.com/nodejs/node/tree/63243bcb330408d511b3945c53719425d8b7abb8/deps/uv/src/unix https://github.com/nodejs/node/tree/63243bcb330408d511b3945c... https://github.com/golang/go/tree/964639cc338db650ccadeafb7424bc8ebb2c0f6c/src/net https://github.com/golang/go/tree/964639cc338db650ccadeafb74... Where you are writing to non-portable parts of unix you can build non-portable stuff into their own files named simply after the platforms and include the correct stuff higher up in a .h and an exmaple of it not going so well: https://github.com/dart-lang/sdk/issues/10260 https://github.com/dart-lang/sdk/issues/10260 (fwiw you can find https://github.com/mulander https://github.com/mulander all over this "teaching" effort, so points to him)
- iainmerrick 10y agoTotally agreed about autotools. It's fine (if very slow) when it works, when it doesn't work right away it's a massive pain. Sometimes I need to build C code for iOS and Android. Libraries with a nice clean setup like the author of this article describes are great. Libraries that use autotools are more trouble than they're worth.
- 0xfeba 10y agoIn my experience, I've found that the configuration management aspect of writing software is the least understood and hardest to learn. All the books on learning languages I've read just skip it entirely, or provide a default setup with no explanation. Which is somewhat understandable. But then the documentation for various projects also do the same thing. I'm left reading the man pages for various tools that some project says I need and going "Why do I need this?" on a higher level than the man pages. And in my professional life it's much the same. On one project we have one or two people who understand how the system is setup, reject any kind of change or improvement, and just keep throwing bailing wire at it to keep the process going. On my own, partly as a result to institutional intertia, we have an ancient TFS build controller running a powershell script running gulp running webpack running babel, and then deploy it using robocopy to network share in IIS (using iisnode). It's amazing it all works. It often breaks, rarely with the same error. It also takes 10 times as long as building on my local machine.
- _RPM 10y agoThis guy has some really interesting blog posts about system level programming.
- kyberias 10y agoSlightly off-topic or meta but still: the layout, CSS styles, fonts, colors and contrast on this blog (article) are just PERFECT. Rare occurrence.
- mschuster91 10y agoautotools sucks for developers, but as a user I highly value "make uninstall". Every time I see software without a make uninstall, I have to manually create a textfile with the stuff the application installed so I can do a clean upgrade... it sucks. Hell, most Windows software ships (mostly crappy) uninstallers, and most OS X software can be uninstalled by dragging the app folder into the Trash, but there are loads of .nix programs without uninstall support? What the f..k? Oh, and .nix software using standard build systems has another advantage: it's usually trivial to package them in .deb/.rpm if you want to distribute e.g. custom ffmpeg/vlc builds on a fleet of servers.
- wahern 10y agoIf you’re coding to POSIX, you must define the _POSIX_C_SOURCE feature test macro to the standard you intend to use prior to any system header includes: Nooooooo..... Take it from someone who religiously keeps many of his very complex libraries portable across many systems (AIX, FreeBSD, Linux/glibc, Linux/musl, NetBSD, OpenBSD, Solaris, and others), the _last_ thing you want to do is define the POSIX portability macros. Once you do that, you're in for a _world_ of hurt trying to use any extension, including routines from newer POSIX standards that are almost universally supported, but because some systems don't claim 100% compliance to the latest POSIX release, become hidden once you start using the user-definable feature macros. Most systems (particularly the BSDs) make _everything_ available by default. On Linux, however, you should define _GNU_SOURCE; on Solaris, define __EXTENSIONS__ and _POSIX_PTHREAD_SEMANTICS; on AIX define _ALL_SOURCE; on Minix define _MINIX. This way, anything you might possibly want to use is available. There are a few cases where a native extension conflicts with a POSIX routine. The classic case is strerror_r on glibc. Dealing with these cases is easier to deal with than fumbling with feature macros. Remember, you'll _always_ want to use extensions unless you're writing pure ANSI C (C90) code. Portable is not the same thing as POSIX compliant. And many POSIX routines are effectively extensions on systems not yet certified for the latest POSIX standard.
- wahern 10y agoI don't like plugging my projects on HN, but in this case I'll make an exception. Though I've recently warmed to autoconf, I still agree it's overkill for most cases. For many years I have, like many others, maintained an ad hoc library of purely preprocessor-based feature checks that I would copy from project to project. When recently writing a comprehensive Unix system API module for Lua I basically ended up with a ridiculous number of feature checks. I broke those out into a separate project I called autoguess. https://github.com/wahern/autoguess Autoguess is a config.h file that exclusively uses preprocessor-based feature checks. It's very comprehensive. Much more comprehensive than most need, but compared to autoconf, or relative to modern C++ projects, all those preprocessor conditionals are effectively free. autoguess doesn't provide any compatibility routines--just the detection. (For compatibility routines see my Lua module, lunix. Compatibility interfaces can be tricky, and context matters, so I've learned to stay away from trying to comprehensively "solve" that problem. One needn't look further than Gnulib to understand the pitfalls of trying to maintain such a beast.) Most of the code in the autoguess repository is a framework for running autoconf checks and comparing feature detection results with the autoguess header. The library is just the single file "config.h.guess". Autoguess uses the same naming conventions as autoconf, though in the autoconf universe consistency can be poor and HAVE_FOO names can differ from project-to-project. However, autoguess recommends to use "#if HAVE_FOO" arithmetic conditionals, not "#ifdef HAVE_FOO". That makes it possible to override feature detection macros from CPPFLAGS. Autoconf's rule about using #ifdef is an archaic solution to a mostly non-existent problem these days--broken C preprocessors not evaluating an undefined macro as 0 in arithmetic expressions. (There's a 6 line preamble at the top of configure.ac in my repository that will fix how autoconf generates config.h files.) Of course, as new operating system releases are made, the autoguess checks can become outdated. Though many autoguess checks aren't directly reliant on version numbers, autoconf feature checking is, I agree, a more robust method. But autoconf checks aren't immune to regressions, either. There's no substitute for regularly building your software on various platforms. Autoguess does much more than detect system and compiler versions, but I've benefitted greatly over the years from this project: https://sourceforge.net/p/predef/wiki/Home/