Live data from Hacker News

Writing C software without the standard library

weeb.ddns.net

161–170 of 188 posts

Re: Writing C software without the standard library

#161

If the asm was written a little more cleverly, the syscalls would avoid almost all moves, because the compiler'd put everything in place: _syscall5: mov %r9, %r10 _syscall3: mov %rcx, %rax syscall ret And then: extern unsigned long _syscall3( unsigned long, unsigned long, unsigned long, unsigned long); extern unsigned long _syscall5( unsigned long, unsigned long, unsigned long, unsigned long, unsigned long, unsigned…

Is that going to be inlined properly?

Re: Writing C software without the standard library

#162

Earlier quoted context omitted.

> At least on Linux the first few (i.e. the oldest, most common and useful) syscalls have not really moved around over the years IIRC raw syscalls are an officially supported kernel API, that's why you can have alternate libc implementations (e.g. musl), and Linux is an oddity in that, on most systems even if the syscalls are fairly stable there are no actual guarantees with respect to them, and the only officially s…

IIRC raw syscalls are an officially supported kernel API The term you are looking for is We don't break userland . Once a systemcall goes live it is engraved in stone. This is why if you look though the syscall table you see call , call2 , call_ext .

You're implying that exposing syscalls as the ABI instead of a higher level ABI is the only way to get backwards compatibility but that's not true. OS X can change its syscalls without breaking userland in exactly the way GP says: don't allow static linking of e.g. libSystem and its ABI is the ABI you're expected to use instead of directly calling syscalls.

Sure, Linux makes promises about its syscalls. But that's not the only way to get robust backwards compatibility and as GP says it's the oddity in that respect.

Re: Writing C software without the standard library

#163
post #114

Earlier quoted context omitted.

> wasting resources I don't consider variable-width fonts, nice margins and dynamic word wrapping to be a waste of resources. There's a sane middle ground between Electron and the 70s.

That ground is mostly unexplored. IMHO, the sane middle ground between Electron and the 70s isn't shoving an entire browser inside a desktop app. It's using modern programming and compiler techniques to get close to the metal without having to deal with all of the shortcomings of C/C++. It's also using UI libraries that make creating desktop UIs as easy as making a website, but with better performance. Sadly, neither…

You would be well-served to use Qt and Cocoa enough to get familiar with them. You may or may not like the language, but the organization and consistency of both are excellent. In particular, QT's signals/slots, naming consistency, and layouts make writing UIs easy without looking at the documentation (once you've used it a bit). Cocoa is well thought out, but a bit dated. (UIKit is not so well thought out, and forces MVC on you, which is often unnecessary and unhelpful, and then it completely botches UITableView by muddling up the M, V, and C. Qt's QTableView is much saner.) Unfortunately, it's also hard to write a UI in code in Cocoa, since you have to write the positioning system yourself due to no layouts. How they handle different screen resolutions is brilliant, though.

You might also want your UI library to be cross-platform, which will limit your choice of languages, especially if you ever want to port to mobile.

Re: Writing C software without the standard library

#164
I've tried this myself. What you'll run into is that you tend to need a few things that are non-trivial:

An implementation of malloc/free

Functions to parse and print floats (somewhat system dependent)

Assembly implementations of any trigonometric functions used

While there is code that goes to that effort (The Go runtime comes to mind), it's quite a pain for "normal" code.

Re: Writing C software without the standard library

#165

A value in the range between -4095 and -1 indicates an error, it is -errno. The syscall/errno stuff has always seemed unusual, inelegant, and inefficient --- instead of just returning a negative error code directly, the function returns the vague "an error has occurred" -1, and you have to then check errno separately after that. It only adds insult to injury when you realise that the kernel itself isn't doing it, but…

I didn't see it mentioned at a cursory glance of the comments, but syscalls != standard library. Standard Libraray for C is that which is defined by the ISO C standard. syscalls are whatever *nix decides them to be. Additionally Standard Library != posix.

Sure, there may be some overlap or interleaving between Standard Libraries, syscalls, and posix, but they are definitely not the same.

Re: Writing C software without the standard library

#166
post #3

> Executables are incredibly small (the http mirror server for my gopherspace is powered by a 10kb executable). Is this ever an real issue, even on any embedded system in the last 20 years?

Yes, executable size is still a modern day problem. Imagine you're shipping an OS; do you want the hundreds of thousands of executables in your system to all be a few percentage larger when you're trying to ship to a customer who may only have 16 or 32 GB of storage space? Of course, removing libc won't be your first (or second or third or ...) step for removing bloat from a mature codebase.

If you'r trying to reduce bloat in this OS would be dynamically linking anyway wouldn't you? I've installed those 300MB printer drivers so I'm all for reducing bloat. This just seems to be on the extreme end of things.

Re: Writing C software without the standard library

#167
post #46

Earlier quoted context omitted.

I find myself agreeing with him 100%. Not just on Gopher (although i did write a Gopher client some time ago - http://runtimeterror.com/tools/gopher/ ) but on the entire rant about wasting resources, UIs that waste screen real estate and become unusable in smaller resolutions, fonts that only look good with anti-aliasing and have weird misplaced pixels with antialiased disabled (which i also do), websites that make r…

> I've even seen functionality regression for the sake of modern and > "responsive" design in many cases. > > For example, you used to be able to browse youtube favorites in > pages. Whoops, not anymore! Now you need to scroll down and > painfully wait for the site to make its stupid animation and load > the next page. > What's worse is, you can't just skip through pages. You have to > load EVERYTHING and it all stay…

> Plus, often search doesn't look in the "scrollable" content, i.e. you need to load (=scroll) all these "pages" and rely on the browser search function.

That's a feature. They want all this valuable search data so the make you search via the site instead of the browser.

Re: Writing C software without the standard library

#168
post #115
post #94

Earlier quoted context omitted.

Fine, but you get this thanks to already being linked with these megagodzillas like kernel32.dll. Try without them.

On windows nt, ntdll gets loaded in all processes, it's hardcoded in the kernel ( http://gate.upm.ro/os/LABs/Windows_OS_Internals_Curriculum_R... ). That's where the loader resides, so you can't go without it. And that's where all the Zw/Nt* functions/ i.e. where the syscalls are. So you can't really go without it.

Except the new picoprocesses, which don't get ntdll (or anything really, I guess).

Re: Writing C software without the standard library

#169
post #147

Earlier quoted context omitted.

I use stdint.h too, but I'm honestly curious if there is any common platform around today where one of the following asserts fails: int main() { assert(sizeof(signed char) == 1); assert(sizeof(short) == 2); assert(sizeof(int) == 4); assert(sizeof(long long) == 8); return 0; } I'm not interested in the language lawyering, because yes I know the standard provides more freedom to compilers. I just think those definition…

I think int is 8 bytes on the PS4. (I just got bitten by this...)

How does the PS4 declare a 4 byte int? If that's "short", is there a way for a 2 byte int?

Re: Writing C software without the standard library

#170
post #113

Earlier quoted context omitted.

> The only bit i'd disagree would be with games, at least on AAA games since i have some experience there and -at least at the engine level- there is still a lot of low level wizardry being done there. He explicitly calls out people "writing poorly performing code on top of pre-made engines". I'm pretty sure he's aiming most of his ire at non-indies using Unity3D or Unreal.

Personally, I don't mind this at all, mostly because they wouldn't be writing anything if it weren't for Unity and Unreal.

They could be decomposable libraries rather than singular massive engines.
Post reply on HN