10 ms·
An Introduction to C and GUI Programming
- rjammala 8y agoDownload link: https://www.raspberrypi.org/magpi-issues/C_GUI_Programming.pdf https://www.raspberrypi.org/magpi-issues/C_GUI_Programming.p...
- jononor 8y agoC for GUI programming? GTK2 for starting a new project today? I think new learners would be much better off with Python 3 and GTK3 (or Qt5)...
- jvanderbot 8y agoOr any other arbitrary |L|x|F|x|P| combinations of languages, frameworks, and platforms. But, this one didn't have a book yet.
- spectramax 8y agoI disagree. There is always value in learning something simple and master it than learning something complex and barely scratching the surface. It teaches you fundamentals, especially on an embedded system such as R-Pi. The first language I learned was C on an ARM processor. Learning about registers, bit manipulation, seeing how compiler optimizes the code based on GCC flags, understanding pointers, memory management, the whole thing is difficult for a beginner but to me, it was immensely helpful. I also love Python/Julia/Java/JavaScript but man it makes you appreciate neat things higher languages offer. One day I want to build my own 8-bit computer from discrete gates. I’m sure it won’t be a waste of time.
- Allower 8y agoLol you really went all in on the sunk cost fallacy there huh
- sdegutis 8y agoThe embedded environment is what makes this setup make sense. But in the general case, C is less than ideal for GUI programming compared to what's available in other languages, because of lack of first class closures and more complex data types (or at least classes). Otherwise you are basically in the 80s doing Win32 GUI programming all over again.
- terryschiavo22 8y agoWhat do first-class closures have to do with anything?
- sdegutis 8y agoBeing able to pass a closure (lexically scoped anonymous function) as an event handler is invaluable in GUI programming.
- sureaboutthis 8y agoSo you think C can't do that? Of course it can. Anything you want to create, C can handle it just as well if not better.
- icedchai 8y agoC doesn't support closures natively, so have fun building it up yourself. You can pass a function pointer. That's it.
- sureaboutthis 8y agoMy point is, that in C, you can do anything, including creating closures and passing them however you wish.
- icedchai 8y agoWith enough coding, you can "do anything" in any Turing complete language. This is meaningless. It doesn't mean it is advisable (simple, maintainable) to do so. C does not support a syntax for closures, so you'll really just be calling functions with an 'environment' arg or something similar. What's the point?
- kbumsik 8y agoI agree with you if you are talking about Python vs C. But a new GTK2 book sounds like teaching Python 2 for beginners.
- satysin 8y agoThe author does touch on this on page 76 of the book (quoted below) GTK 2 and GTK 3 One thing worth mentioning at this point is that there are two versions of GTK in use. The current version is GTK 3, and this is still under active development – it changes quite a bit between releases. For this reason, many people prefer to use the older version, GTK 2. This offers most of the same functionality, but it is stable code which doesn’t change very much any more. Some say that it is always better to use the latest and greatest version of anything, but more cautious old engineers (like your author…) tend to prefer the older version that has had a lot more testing and with which people have stopped fiddling about! In terms of the examples we are looking at here, there aren’t that many significant differences between the two, but just to be clear, the examples in this book will use GTK 2.
- megous 8y agoGTK+ 3 is stabilized now. GTK 4 sees active development, and hopefully a first release this year.
- badsectoracula 8y agoWhich, like Gtk 3 to Gtk 2, is also backwards incompatible so using Gtk 3 also wont be future proof (and Gtk4 wont be either, for that matter, unless the Gtk developers stop breaking their library every major version and decide to provide a stable API people can target - but considering they cant even decide on a stable way to make themes, i have little hope for that).
- jononor 8y agoGTK2.0.0 was released 17 years ago. GTK3.0.0 was released 7 years ago. GTK2.24 got its last patch release 2.24.32 about 1 year ago. 10 years being the mainline version, and having a supported compatible version for 16 years is pretty darn impressive! If GTK4.0.0 comes out next year, then GTK3 will have had 8 years being the mainline version. Its not quite win32 levels of stability, but it is not like they break this stuff every year or something...
- wrycoder 8y agohttps://www.nand2tetris.org/copy-of-cool-stuff https://www.nand2tetris.org/copy-of-cool-stuff This is definitely not a waste of time. But though building a physical ALU from TTL is feasible, constructing a reasonable amount of memory would be a waste of time and money. Once you have written the nand2tetris HDL for the hardware, you could translate it to Verilog and burn an FPGA quite easily.
- spectramax 8y agoThis is the coolest thing ever, thank you!
- mayankkaizen 8y agoThis is exactly what I wanted to learn. I really wanted to 'see' C in action at lowest level. This aspect of programming is what I am curious about. But I never found a decent beginner friendly resources. Anyone reading this comment, please chime in with your suggestions.
- deaddodo 8y agoIf you want to see C at it's lowest level and understand where it shines, I'd recommend the embedded and bare-metal realms. This Raspberry Pi tutorial set, for instance: https://github.com/bztsrc/raspi3-tutorial https://github.com/bztsrc/raspi3-tutorial It's also fairly popular for emulators, since it's types are orthogonal and it's low level design is well suited to designing a pseudo-machine. It's definitely being displaced in that realm by C++/Rust/Nim/etc though.
- mayankkaizen 8y agoThank you very much!!!
- s_m_t 8y agoIf I may add my two cents... I highly recommend reading through the standard library and then looking at how they are implemented in actual C. The string library is probably the most important, this is where most people mess up! If you can figure out pointers and how to actually get the size of things (sizeof(*char) doesn't get you the size of what it is pointing at... it gets you the size of a pointer which varies depending on the architecture. sizeof(myArray) doesn't give you the number of elements in an array, it gives you the number of bytes your array can hold, to get the number of elements you must do sizeof(myarray)/sizeof(myarray[0]). sizeof() is a compile-time operator, it knows nothing of runtime) you will be pretty much 99% of the way there. https://en.cppreference.com/w/c/string/byte https://en.cppreference.com/w/c/string/byte https://github.com/lattera/glibc/tree/master/string https://github.com/lattera/glibc/tree/master/string
- samt430 8y agoIts possible you may have come across this but if you havent already it speaks to what you just said https://www.evanmiller.org/you-cant-dig-upwards.html https://www.evanmiller.org/you-cant-dig-upwards.html
- teddyh 8y agoHasn’t it always been the case that the current generation skips learning about useless lower layers used by older generations? How could it be that the layers you grew up with and learned are the special ones which are still useful, when all the layers which came before, which people at the time thought essential, have turned out not to be so? I.e. why is it that C and assembler is “fundamental”, but electronics is not? (I have written about this before: https://news.ycombinator.com/item?id=19106922#19113359 https://news.ycombinator.com/item?id=19106922#19113359 )
- jack_h 8y agoI mean when I went to university we absolutely went over the electronics side of things. Certainly not to the same depth as an electrical engineer would have, but it was covered.
- jononor 8y agoI'm an electronics engineer who started to learn programming Assembler on PIC16 micro-controllers. My arguments is not against learning the fundamentals. I just know from teaching that the primary problem people have when learning is that they get stuck and stop. Making GUIs in Python (given good educational resources, like a book) will give results much faster, and more quickly become a practical skillset for GUI programming and other useful tasks. For learning C and starting to poke at low-level embedded stuff I would recommend starting with Arduino (and then into plain AVR) - and not go anywhere near a GUI toolkit. There is a reason we don't actually throw kids into the deep end when teaching them to swim. Most of them would just drown...
- PorterDuff 8y agoI think if I were wanting to bang out a simple embedded design with a GUI, this is exactly the direction I'd go. It isn't like you usually need super complex behaviors and I'd hate to add in extra toolkits or a big framework just to get a few buttons on a screen. I've never dealt with Raspberry Pi world, but I can see the value of stripping the software world on one to the bare minimum. I wonder if someone has put together a FreeRTOS tool chain for it.
- zemo 8y agoyou're ignoring the audience and the platform. The audience is people whose only computing device is the Raspberry Pi. Compiling a Qt app on most of the rpi models would take forever. The lower-end models are pretty resource constrained. Most of the rpi software is written in GTK, so it's a primer on using the desktop software toolkit that is used to make the applications that the user is using.
- vortico 8y agojononor should have recommended Gtk for Python via https://pygobject.readthedocs.io/en/latest/ https://pygobject.readthedocs.io/en/latest/. Not sure why PyQt was suggested instead.
- jononor 8y agoI did not suggest compiling Qt apps (or doing C++). You can build Qt apps in pure Python using the libraries installed via the package manager.
- ChrisRR 8y agoAs a low level developer, i thoroughly disagree. I think it's disappointing that we're given absolutely insane amounts of processing power where intel keep pushing the limits of physics, then developers keep wasting that on inefficient languages like javascript, python, java, etc. They have their uses, but using them to develop every piece of software would be an inefficient waste of resources. For fast, efficient software there will always be low level languages such as C, C++, etc. and I would always urge more people to learn them, even if just to learn to appreciate how much is managed by their high level language and understand the trade-offs they're making with those languages.
- vortico 8y agoRendering a GUI is much more overhead than the business logic to set up the GUI and make it interactive. A GUI is only "slow" if there is a user-triggered operation that takes more than 10ms. Interpreting a Python callback is many orders of magnitude smaller than this, and complex algorithms can be outsourced to other languages if needed. Focus on your bottlenecks first. Don't pre-optimize.
- agentultra 8y ago> Don't pre-optimize. Don't pessimistically de-optimize either. It's harder to remove bottlenecks from mature applications than it is to plan ahead for the target platform and common use cases. By all means, prototype in Python but if your target platform is, at minimum, a R-Pi 2 and you want the experience to be snappy even when their is high IO or CPU work being done... then don't release on Python; use the prototype to drive the implementation in C. Maybe consider even compiling from a higher-level language into a C library to minimize the boiler-plate. Planning goes a long way, even if you don't plan everything up front.
- vortico 8y ago>It's harder to remove bottlenecks from mature applications than it is to plan ahead for the target platform and common use cases. I disagree. It's easier to release first and change what you need later, since there might only be 1-10 sections of code that need to be changed. Python is extremely fast to write, maybe 3x faster than C. This means you can get seed funding and sales faster, and if the idea is good enough to get a decent salary, you can just hire out optimization for a v2 release. It also allows your application to appear to get better over time, which makes long-term customers happy.
- droithomme 8y agoHe explains his reasoning both on page 76 of the free to download book and in the parallel complaint thread on the linked article. His reasoning is that GTK2 is stable and GTK3 changes with each iteration.
- VMG 8y agoIt is impossible to use the GTK3 or GTK2 bindings for any language without understanding the C interface. Usually there is no real documentation on the bindings.
- buzzert 8y agoI would actually pick Vala over anything else for GTK3 applications. It’s really pleasant to work with and compiles to optimized C code, so you get serious performance wins over Python (and type safety to boot)
- axaxs 8y agoVala is nice, but is really niche at the time being. I think people would be better served using Python 3 and QT for maximum ease, documentation/help, and cross-platform abilities.
- bollockitis 8y agoVala really is a remarkable engineering achievement that is highly underrated. I can only imagine what could be possible if Vala had mindshare similar to Python.
- weberc2 8y agoI'm not sure either make sense. So much of GUI programming is learning a particular toolkit. Especially in the case of Qt where it's an entire _runtime_--memory management, string types, threading, async/signaling, etc etc. This isn't even mentioning the hoops you have to jump through to compile a Qt program (if you're using C/C++). These toolkits are inevitably on their way out. The future is the browser stack, loathe though I am to accept it. As a tangent, it wouldn't be horrible if something like a browser was used as a window manager and UI runtime a la ChromeOS except with apps running in containers (and no bastardized Linux kernel and the restrictions that come with). This would have lots of benefits--the whole frontend benefits from all of the investment going into the web (including wasm) and the whole backend benefits from the investment in containers. Notably, packaging / dependency management, process management, log management, etc would all be much simpler/easier.
- billsix 8y agoImmediate mode gui frameworks make for a pleasant programming experience https://github.com/vurtun/nuklear https://github.com/vurtun/nuklear
- plopz 8y agoWhats the benefit of immediate mode libs? I haven't had the opportunity to use one before. I'm hopeful for libui in the future, but maybe I should consider immediate mode libraries? https://github.com/andlabs/libui https://github.com/andlabs/libui
- billsix 8y agoNo callbacks, so no forced heap allocation nor lifetime management . "oh you want to use FooGUI? First let me tell you about the object hierarchy and object lifecycle". No mvc. No custom, required data structures. The programmer just copies and pastes from the demos, makes small modifications, and it just works. (No offense intended towards your framework, I've used gtk, qt, and swing and have never understood them like I understand nuklear and dearimgui)
- cozzyd 8y agoFor a raspberry pi which is unlikely to be plugged into a monitor it seems like a better gui for most usecases would be a simple webserver.
- badsectoracula 8y agoRaspberry Pi was made from the beginning to be used as a cheap desktop computer that people can play around with and students can buy with their own money, pretty much like the BBC micros and ZX Spectrums in the 80s. From day one it came with a full Linux desktop system and was meant to be used by itself as a proper computer.
- soneil 8y agoIt's worth remembering the core mission for the Pi was to be an affordable educational computer. In that context, this publication is entirely on-point for the foundation. There's an almost symbiotic relationship going on where the foundation doesn't exactly ignore the hobbyist or industrial communities - far from it - but they are still holding to their core mission, and these notable, but ultimately ancillary communities provide the budget for it.
- ingve 8y agoThe author speaks highly of the book "C for Yourself". An electronic version of the book from the Microsoft Programmer’s Library 1.3 CD-ROM has been posted [0] on the truly excellent PCjs Machines [1] website, where a lot of other older programming information can also be read [2]. [0] https://www.pcjs.org/pubs/pc/reference/microsoft/mspl13/c/c4yrself/ https://www.pcjs.org/pubs/pc/reference/microsoft/mspl13/c/c4... [1] https://www.pcjs.org/ https://www.pcjs.org/ [2] https://www.pcjs.org/pubs/ https://www.pcjs.org/pubs/
- chewyland 8y agoWhoa, this little book just arrived in my mailbox in my Bulgarian village apartment. Can't wait to crack it open.