7 ms·
Cosmopolitan Libc author here. When I saw this article earlier today, it put a big smile on my face, because I would have thought there'd be so many more issues
by jart 6y ago
Cosmopolitan Libc author here. When I saw this article earlier today, it put a big smile on my face, because I would have thought there'd be so many more issues than there turned out to be! For additional context, we've got a GitHub issue that's tracking progress on the changes that need to be made based on what we learned: https://github.com/jart/cosmopolitan/issues/61 https://github.com/jart/cosmopolitan/issues/61
- ducktective 6y agoAny recommendations to somebody who wants to learn C "properly" in $CURRENT_YEAR?
- unkulunkulu 6y ago$CURRENT_BEST_BOOK Sorry :)
- jtvjan 6y agoI really liked "The C Programming Language (second edition)" by Kernighan & Ritchie. It doesn't teach C99 and later features, but if you want to write portable code you're not using those anyway.
- sillysaurusx 6y agoK&R is a horrible way to learn C. It's the best way to make someone run in the other direction, though. It's like telling someone that SICP is the best way to learn Scheme. Maybe, but lots of people learn differently. Unfortunately I don't have good suggestions myself, but at least they won't feel bad if K&R isn't their cup of tea.
- e12e 6y agoFrom various discussions on Hn, I think the consensus is most people really like k&r as a technical book (it's concise, clear and a fun read) - but everyone working with C says its a terrible book for learning modern C. I tried to refresh my memory from previous threads, and I think the general trend has been to recommend (as seen in sibling comments): https://modernc.gforge.inria.fr/ https://modernc.gforge.inria.fr/ https://nostarch.com/Effective_C https://nostarch.com/Effective_C I've also seen the older: https://www.oreilly.com/library/view/21st-century-c/9781449344382/ https://www.oreilly.com/library/view/21st-century-c/97814493... Mentioned. There's also Architecture of Open Source Applications, which can help with starting to read some larger code bases, some of which are in C: http://aosabook.org/en/index.html http://aosabook.org/en/index.html And there's a general recommendation to go read the source code of the Redis cache/db. Finally i came across a mention of this short article on gdb (nb: mention of TUI text ui should probably have been in the top, not a foot note): https://www.recurse.com/blog/5-learning-c-with-gdb https://www.recurse.com/blog/5-learning-c-with-gdb I feel I'm missing a book that has come up often, but can't think of which one. I did like zed Shaws learn c the hard way, but I'm afraid it's getting a bit long in the tooth: https://learncodethehardway.org/c/ https://learncodethehardway.org/c/
- vkazanov 6y agoTry new books: effective c and the free modern c one. They actually teach the language and all the concepts.
- dj_mc_merlin 6y agoK&R -> do some random programming problems -> small project -> learn ASM -> learn how to do a buffer overflow (read "Smashing the stack for fun and profit") -> from here find your own path. The first three are just useful for getting the basics. The next two are for starting to learn low level stuff. You don't really understand C until you understand how it relates to lower level code.
- CraigJPerry 6y agohttps://www.oreilly.com/library/view/21st-century-c/9781491904428/ https://www.oreilly.com/library/view/21st-century-c/97814919... I thought 21st Century C was good, i've still kept my copy. I'd happily recommend it. I like the K&R book too - it feels reeeeeeally old but it's really short. There's a few others that have helped me in various ways but these are a little older - Love C by Tim Love (free online, my copy is something i just printed out, it's not that long). Programming from the Ground Up (x86 assembly) - this is available freely online but i bought the book and that helped me a lot with C even though it's a book with only assembly... (to be fair, it does go through calling conventions). Finally there's another book i love, Advanced Programming in the Unix Environment by Stevens, i have the 6th edition updated by Rago after Stevens' passing. Fascinating book - but huge.
- himujjal 6y agoAPE makes it viable for a cross platform C library with a cross platform C compiler to make cross platform binaries. Besides I was in the same boat as you. I come from the world of JS/Python/Go. I even wrote https://github.com/Himujjal/ekon https://github.com/Himujjal/ekon in pure C recently. The reason I thought C would be good is performance and portability. But I found it to be better to invest time in Rust rather than in C. C is a fantastic language but cross platform dependency management is difficult. Unit Testing is also difficult. There are solutions but not as efficient as Rust's ecosystem. BTW I wish there could be a Cargo for every language.
- moonchild 6y ago> C is a fantastic language Odd...I find that a very strange sentiment. I thoroughly enjoy C, and have even written a compiler for it, but I don't think it's a well-designed language by any account.
- CodeArtisan 6y agoC Primer Plus by Stephen Prata is the most complete i have seen, it does cover everything up to C11 and goes in details about stuff like array vs pointer. I don't like 21st Century C, it may have changed since then but it has a chapter titled "Object-Oriented Programming in C" which is confusing object with Abstract data type.
- edgyquant 6y agoI assume object oriented C means using structs and function pointers?
- CodeArtisan 6y agoNo. For 21st Century C, an "object" is an incomplete type and procedures. struct Object; void Object_init(struct Object *o); That's ADT.
- ktr 6y agoI really enjoyed Expert C Programming (https://www.amazon.com/Expert-Programming-Peter-van-Linden/dp/0131774298 https://www.amazon.com/Expert-Programming-Peter-van-Linden/d...).
- alisonatwork 6y agoI can't remember where I learned the basics, but if you aren't new to programming in general then I think there are only three important things you need to know: what is a pointer, what is a macro, and always free what you malloced. Reading a book or tutorial will be helpful for this. In my day it was K&R, no doubt there are more up-to-date ones now. But the next step, which I think is far more important, is to look at the source code of tools that you actually use in real life. Things like cp, or wc, or head. You've probably used them for years without thinking about it, but they're all written in C. Don't look at the modern GNU versions just yet, since they can be packed full of complex functionality, start with micro implementations like Busybox or Toybox. Then look at some OSes that are known for super clean code like NetBSD or OpenBSD. In those OSes you have the benefit of being able to go back to the version that existed 25+ years ago, so you can read through the diffs to see how they adopted new features and found new ways to address C's biggest flaw/risk - memory exploits. If you're interested in kernel programming, check the Linux and BSD sources, and lurk on the mailing lists for a while to see how people talk about the code. The review process tends to be a bit more brusque than you might be used to on Github, but it's often detailed and results in code that is of a relatively high quality, or at least a consistent standard. It's a great place to learn.
- kamranjon 6y agoI am just trying to learn myself so take this with a grain of salt but I have found the github of Antirez:(https://github.com/antirez https://github.com/antirez) - the author of Redis - to be an amazing resource. The code is incredibly well documented and to my eyes pretty well written. A good place to start I think is Kilo:(https://github.com/antirez/kilo/blob/master/kilo.c https://github.com/antirez/kilo/blob/master/kilo.c) - a 1000 line text editor with no dependencies.
- mikewarot 6y agoI'm an old Delphi/Pascal application programmer here, trying to get the lay of the land in the C/C++ world. Why is there a forest of files in Cosmopolitan Libc? I tried looking at it on Github to see how things were done, and there were a lot of .h and .S files, I couldn't actually find the C source, though I'm sure somewhere in there is a .c file. Doesn't fragmenting the source into thousands of files make compilation far slower than it needs to be? Also, I wonder if/how functions that aren't called in a program get trimmed away by the linker, and thus don't make the executable larger.
- jart 6y agoIt takes about 18 seconds to build the entire Cosmopolitan repo and run all its unit tests on my PC. During that time the make command builds 14,376 .o files, 66 .a archives, and 421 .com executables. It's an exceedingly fast build config. Having lots of objects is a good thing because it helps static linking work better. When the Unix linker loads a symbol from a static archive, it pulls in the whole module that defines that symbol. For example, if you define memcpy() and memset() in the same .c file and then your app only needs one of them, they'll both go towards bloating your binary. Workarounds exist like -ffunction-sections and -Wl,--gc-sections but a C library should make assumptions about the fewest flags feasible. What's the end result? We're able to build executables that are 12kb in size which run on seven different operating systems. The big codebase is what made tiny binaries possible: https://justine.lol/cosmopolitan/howfat.html https://justine.lol/cosmopolitan/howfat.html
- krab 6y agoMay I ask - what was the reason for creating APE/Cosmopolitan? I read your post about actually portable executable format but I wonder if it's something that you found immediately helpful for some project or if you work on it just out of curiosity.
- matheusmoreira 6y agoDo you build on tmpfs? I started doing that and it makes builds ridiculously fast. $XDG_RUNTIME_DIR is perfect for this, I just make a directory there and symlink the build directory to it. Even added some makefile logic to ensure the link target exists: build_directory_link := $(shell readlink $(build_directory)) $(if $(build_directory_link),$(shell mkdir -p $(build_directory_link)))
- s_Hogg 6y agoCongratulations on all you've done so far, it's amazing.
- egeozcan 6y agoOnly your own creativity seems to match your intelligence; excellent, excellent work! I was wondering if we can accompany runtimes written in C (like lua) with some scripts? So we have cross-platform, double-click-to-run scripting? For us poor souls who can't write reliable C code, that would be a great thing!
- jart 6y agoYes we can do that. There's a real opportunity here to define a novel API / framework on top of Cosmopolitan that does just that. It's something I myself have personally refrained from, mostly because fiddling with the low level bits is what I'm best at, and novel APIs require a certain kind of artist. But I'm certain that someone if not myself at some point is going to come along and use Cosmo for that purpose and it'll be pretty cool.
- HexDecOctBin 6y agoOne question: is it possible to load .so files (through dlopen/dlsym) on Linux when compiling to APE? I was working on something similar (though much smaller in scope), but had to stop when I realised that `ld-linux.so` has some internal APIs that Glibc uses to setup dlopen()/dlsym(); essentially meaning that it is very hard -if not impossible - to load any shared libraries if one does not link to Glibc. What I wouldn't give for a liblinux, similar to NTDLL.
- surajrmal 6y agoIt doesn't appear to be supported in their libc implementation: https://github.com/jart/cosmopolitan/blob/d7ac16a9ed56ebdc70481f4a802265ea15619894/libc/runtime/ldso.c https://github.com/jart/cosmopolitan/blob/d7ac16a9ed56ebdc70...
- surajrmal 6y agoLooks like you're also giving up position independent code as that also relies on the dynamic linker.
- HexDecOctBin 6y agoYeah, as I feared. Some one needs to wrest the control of ld-linux.so from Glibc's hands and give it to the Kernel developers (who "will not break userspace"). Otherwise, Linux will always be a all-work-for-no-reward platform.
- matheusmoreira 6y ago> What I wouldn't give for a liblinux, similar to NTDLL. Hey I actually tried to make such a thing. https://github.com/matheusmoreira/liblinux https://github.com/matheusmoreira/liblinux It provides access to Linux system calls and process start up code that gets all the arguments, environment and auxiliary values. I have several examples of applications written in 100% freestanding C with zero dependencies other than this library: https://github.com/matheusmoreira/liblinux/tree/master/examples https://github.com/matheusmoreira/liblinux/tree/master/examp... It's a bit too low level but I actually planned to make my own ld-liblinux eventually. It currently works with static and dynamic linking. The ld-linux.so is able to link even though nothing else links against glibc. I didn't test dynamic loading though. I stopped working on it because I found better solution for system calls on the Linux kernel repository: https://github.com/torvalds/linux/blob/master/tools/include/nolibc/nolibc.h https://github.com/torvalds/linux/blob/master/tools/include/... Do you think liblinux could have a future?
- stephc_int13 6y agoIn my opinion, the most important drawback to your work and its adoption is the use of fancy unicode characters in the name. I think your work is outstanding but it looks a bit childish/bizarre with this name, and thus it might prevent some people to trust its reliability.
- metalliqaz 6y agoyou must mean ape. But the package itself doesn't seem to use any such characters.
- lordofgibbons 6y agoThe future is now, old man
- deleted 6y ago[deleted]
- chacha2 6y agoNaming Your Child | David Mitchell's Soapbox https://www.youtube.com/watch?v=Xblh12XgQ4o https://www.youtube.com/watch?v=Xblh12XgQ4o The first half is relevant.
- shawxe 6y agoSo first of all, I just want to echo the general sentiment here and say that all of this is beyond awesome. Cosmopolitan libc seems to have the potential to literally re-define what constitutes portable code. With that out of the way, am I understanding correctly that the way this works on Linux/Unix is that the modifies itself (by overwriting the EXE file header with an ELF header)? This seems to have the consequence of making that specific file no longer portable. If I'm understanding things correctly, it also looks like the QEMU hack for non-x86_64 architectures will only work once per file, since after the first time running the file it will no longer run as a shell script on Unix so the QEMU invocation will be unreachable. Have you considered adding workarounds for this?
- p10jkle 6y agoOn unix its a Thompson shell script that pipes an ELF binary to 'exec', I believe
- RodgerTheGreat 6y agoHave you considered writing an index for Cosmopolitan's API by topic? The current reference documentation is rather daunting, and it is very difficult to determine at a glance what sorts of functionality are available, and thus what kinds of applications could be written or ported. What would it take to create an analog of SDL- some kind of lowest-common-denominator interface for mouse/keyboard io, audio output, and software-rendered graphics?
- jart 6y agoCosmopolitan is a C library that makes an effort to conform to POSIX and ANSI standards. So in addition to its documentation page, you can dust off any old textbook on C development and use that as a reference or tutorial. Something like SDL could be ported to run on top of Cosmopolitan but we'd need to port all its dependencies too. That would be a massive undertaking. If Cosmopolitan ends up being the next big thing and attracts a large community of contributors then something like that is bound to happen. Right now it's just a scrappy ambitious project being built for fun. So GUIs aren't something we're able to do quite yet. Although we've got TUIs working great! You can have the best most portable console / terminal apps in the world, today. Think about it this way. How many years did it take Microsoft and Linux before they could offer a polished GUI? That should give you a rough estimate of how long it takes to develop these sorts of things from first-principles.
- lostmsu 6y agoCopy-pasted from thread about libc: I missed an opportunity to ask in the previous thread: what would it take to link an app in a different language (say, Rust) with this library? Is it enough to just build an object file, that has libc functions assuming LP64 ABI as unresolved exports?
- e12e 6y agoNot sure if it's been mentioned, but the logical next step would seem to be extending the zip hack[1] to allow embedding lua source with the binary? [1] https://news.ycombinator.com/item?id=26271117 https://news.ycombinator.com/item?id=26271117
- ahgamut 6y agoI'm the author of the blog post! As someone who has never messed around with Libc-level programming, it was surprisingly straightforward (and exciting!) to compile Lua all the way. Cosmopolitan is incredible, thank you so much.
- jll29 6y agoAmazing accomplishment, a great hack!