Adding this “library” is as simple as including a header file. All the magic occurs at the preprocessor and compiler. There’s no code to link. The async routines only need two bytes of overhead and — unlike proper threads — don’t need a preallocated private stack.
Continue reading “Asynchronous Routines For C”
At first glance, it might not seem to make sense to write shell scripts in C/C++. After all, the whole point to a shell script is to knock out something quick and dirty. However, there are cases where you might want to write a quick C program to do something that would be hard to do in a traditional scripting language, perhaps you have a library that makes the job easier, or maybe you just know C and can knock it out faster.
While it is true that C generates executables, so there’s no need for a script, usually, the setup to build an executable is not what you want to spend your time on when you are just trying to get something done. In addition, scripts are largely portable. But sending an executable to someone else is fairly risky — but your in luck because C shell scripts can be shared as… well, as scripts. One option is to use a C interpreter like Cling. This is especially common when you are using something like Jupyter notebook. However, it is another piece of software you need on the user’s system. It would be nice to not depend on anything other than the system C compiler which is most likely gcc.
Luckily, there are a few ways to do this and none of them are especially hard. Even if you don’t want to actually script in C, understanding how to get there can be illustrative.
Continue reading “Linux Fu: Shell Scripts In C, C++, And Others”
A lot of programming languages these days feature lambda functions, or what I would be just as happy to call anonymous functions. Some people make a big deal out of these but the core idea is very simple. Sometimes you need a little snippet of code that you only need in one place — most commonly, as a callback function to pass another function — so why bother giving it a name? Depending on the language, there can be more to it that, especially if you get into closures and currying.
For example, in Python, the map function takes a function as an argument. Suppose you have a list and you want to capitalize each word in the list. A Python string has a capitalize method and you could write a loop to apply it to each element in the list. However,
map and a lambda can do it more concisely:
map(lambda x: x.capitalize(), ['madam','im','adam'])
The anonymous function here takes an argument x and calls the capitalize method on it. The map call ensures that the anonymous function is called once for each item.
Modern C++ has lambda expressions. However, in C you have to define a function by name and pass a pointer — not a huge problem, but it can get messy if you have a lot of callback functions that you use only one time. It’s just hard to think up that many disposable function names. However, if you use gcc, there are some nonstandard C features you can use to get most of what you want out of lambda expressions.
Continue reading “Lambdas For C — Sort Of”
Python is a versatile, powerful language but sometimes it’s not the best choice, especially if you’re doing work in embedded systems with limited memory. Sometimes you can get away with MicroPython for these cases, but the best language is likely C or assembly. If you’re really stubborn, like [amirgon], and really want C and Python to play well together, you can make use of his new tool which can bring any C library to MicroPython.
As an example of how this tool is used, a “Pure MicroPython” display driver for ILI9341 on the ESP32, which means that everything was implemented in MicroPython. [amirgon] wanted to see how the Python driver would compare to one that’s already been written in C, and use it to showcase MicroPython binding. This tool also automatically converts structs, unions, enums and arrays to Python objects, and provides a means to work with pointers which is something that Python doesn’t handle in the same way that C requires.
[amirgon] hopes that this tool will encourage the adoption of Micropython by removing the obstacle of missing APIs and libraries in MicroPython. Since most libraries for systems like these are written in C, a way to implement them in Python is certainly powerful. We featured one use case for this a while back, but this is a much more generic fix for this coding obstacle.
Image via [Frank Stajano]
If you have an opinion about C++, chances are you either love it for its extensiveness and versatility, or you hate it for its bloated complexity and would rather stick to alternative languages on both sides of the spectrum. Either way, here’s your chance to form a new opinion about the language. The C++ standard committee has recently gathered to work on finalizing the language standard’s newest revision, C++20, deciding on all the new features that will come to C++’s next major release.
After C++17, this will be the sixth revision of the C++ standard, and the language has come a long way from its “being a superset of C” times. Frankly, when it comes to loving or hating the language, I haven’t fully made up my own mind about it yet. My biggest issue with it is that “programming in C++” can just mean so many different things nowadays, from a trivial “C with classes” style to writing code that will make Perl look like prose. C++ has become such a feature-rich and downright overwhelming language over all these years, and with all the additions coming with C++20, things won’t get easier. Although, they also won’t get harder. Well, at least not necessarily. I guess? Well, it’s complex, but that’s simply the nature of the language.
Anyway, the list of new features is long, combining all the specification proposals is even longer, and each and every one of these additions could fill its own, full-blown article. But to get a rough idea about what’s going to come to C++ next year, let’s have a condensed look at some of these major new features, changes, and additions that will await us in C++20. From better type checking and compiler errors messages to Python-like string handling and plans to replace the
#include system, there’s a lot at play here!
Continue reading “C++20 Is Feature Complete; Here’s What Changes Are Coming”
If you are writing a program that has a technical user base, it is a nice touch to make the program scriptable. In fact, you might want to do the hard work in a programming language and then use your scripting language to build out features. In theory, this should be easy. There are plenty of embedded scripting libraries and they provide some way for your code to access script resources and for script resources to access selected host variables and functions. If you use C++, one of the easier ways to do this is with ChaiScript.
ChaiScript is BSD licensed and — assuming your compiler supports C++ 14 — it is as easy as including a header file and making a few calls. There are no special tools or libraries required. The code is portable between operating systems, including both 32-bit and 64-bit Windows. It is also threadsafe unless you turn that feature off.
Continue reading “Add Scripting To Your C++ Programs With ChaiScript”
C is the most perfect language and it will run on anything. It will even run on a computer without a CPU.
The computer in question here is the Gigatron, a fully-functional ‘home computer’ the likes of which you would find in the late 70s and early 80s, complete with a VGA output. What makes the Gigatron exceptional is the fact that there is no microprocessor; everything is just a RAM, a ROM, and a bunch of logic chips. There is no ALU chip. Or rather, there is; it’s just that an entire RISC CPU is implemented in basic logic chips and a whole lot of microcode on the ROM. It’s weird, yes, but it is cool. We’ve taken a look at the Gigatron before, and with this computer you get a glimpse of how clever engineers could have been if there were massive memories available in the late 70s.
While the Gigatron can be programmed in BASIC, the limiting factor of this computer is the fact that it remains exceptionally difficult to program. This is what the 8-Bit Guy says, and even though you can write some simple programs, it’s nothing compared to the likes of an Apple II or C64. If only there were a proper IDE, indeed if only there were a C compiler. That’s where [pgavlin] comes in. He has the LCC compiler working on the Gigatron. This is technically a C compiler for a computer without a CPU, or a computer that is entirely CPU. Either way you look at it, this is impressive.
As far as examples and demos go, [pgavlin] has a demo of Conway’s Game of Life working, and a program that will put dots on the screen. It’s not much, and it’s very slow, but check out the video below.
This isn’t a complete implementation of C, as multiplication, division, mod, and arbitrary shifts left or right haven’t been written yet. Floating point support will probably never be completed, and there’s no shame in that. The hardware is limited due to the fact of the fragmented memory map, but this can be improved by upgrading the Gigatron to a 64k memory model.
Continue reading “The No-CPU Computer Gets A C Compiler”