Shop OBEX P1 Docs P2 Docs Learn Events
Catalina: Now you can have your Cake and eat it too! - C89, C99, C11 or C23! — Parallax Forums

Catalina: Now you can have your Cake and eat it too! - C89, C99, C11 or C23!

RossHRossH Posts: 5,639
edited 2025-08-04 00:53 in C/C++

This is exciting! As well as C89, Catalina can now compile C99, C11 & C23 programs! And I didn't need to do a thing! How is that possible?

Well, the answer is that I just found out about cake - a transpiler for converting C23, C11 or C99 programs to pure C89. So now all these C variants can be compiled by Catalina^^. I had been working on my own utility to just add a few of the more useful C99 features (such as the ability to declare variables anywhere in the program, rather than just at the start of each block), but cake is already much more advanced and comprehensive, so I am going to adopt it instead.

While cake it is still experimental, everything I have tried so far seems to work. And its level of support for C99 (in particular) is far more complete than anything I would have attempted. For a summary, see here.

The main things currently missing from C99 support are things that I would have recommended avoiding in any case (e.g. Variable Length Arrays, which were eventually seen as a misfeature and made optional in C11) or things I would never use and would probably also not have bothered implementing (like complex numbers).

And the best thing is that on Linux, you can try it out right now! Just download cake, compile it and use it like a preprocessor - i.e. transpile your C99 program to C89 with cake, and then compile the C89 output with Catalina. For example, after setting it up, you might use commands like:

cake program.c -o cake.c
catalina -p2 -lc cake.c -o program

Currently, this is easy on Linux because cake also uses gcc on that platform (but see NOTE below), but on Windows it does not. I intend fixing that in a version for Catalina. Also, while cake generates relatively pure C89, it assumes the results will be compiled with gcc, which has a different set of header files than Catalina does. This means that while many things will work "out of the box", some things that needs specific header files to be included may not. So I will be adding a cake back-end targeted specifically at Catalina, so that eventually everything will just work.

I wanted to start a new thread for this because I am interested in feedback while I work on it - in particular any C99 features that do not work, or which are not implemented but someone thinks would be essential. I hope to get involved in the cake project myself, once I am more familiar with it, because I think this idea has legs!

However, it is not something that I need to do all by myself. This project could be undertaken by anyone with a reasonable C knowledge, and I would be happy to take a back seat and let someone else take the lead. Anyone who may be interested, please let me know.

Ross.

^^ which also shows that these later variants of C are really just show ponies! The only truly truly necessary C is ANSI C! :)

NOTE: I just noticed cake uses gcc's include files by default on Linux, which makes some things not work with Catalina. To make it use Catalina include files, modify src-main/src/cakeconfig.h to specify Catalina's include directory. For example, on my machine this file now says:

#pragma dir "/home/rossh/Catalina/include"

Comments

  • RaymanRayman Posts: 15,506

    That is great news.

  • RaymanRayman Posts: 15,506

    Was just looking at lvgl
    https://github.com/lvgl/lvgl

    Maybe this would help this to compile.
    I’m assuming it’s not ansi c cause doesn’t say it anywhere that I see..

  • RossHRossH Posts: 5,639

    @Rayman said:
    Was just looking at lvgl
    https://github.com/lvgl/lvgl

    Maybe this would help this to compile.
    I’m assuming it’s not ansi c cause doesn’t say it anywhere that I see..

    It appears lvgl is C99, so in theory yes.

  • RossHRossH Posts: 5,639
    edited 2025-08-03 23:50

    Okay! Getting cake working on Windows turned out to be ... (wait for it) ... a piece of cake!

    If you have gcc installed (I have the MinGW 64 bit version installed), then you just do the following:

    1. Go to https://github.com/thradams/cake and download the source as a zip file. Extract the zip file to your home directory. This will create a folder called cake-main.

    2. Open a Catalina command line window and go to the cake source you just unzipped ...

       cd %HOMEPATH%\cake-main\src
    
    1. Set the INCLUDE environment variable to point to the Catalina include files ...
       set INCLUDE=%LCCDIR%\include
    
    1. Build build.exe ...
       gcc build.c -o build
    
    1. Build cake.exe ...
       build
    

    NOTE - this will generate some warnings, and the command ./cake -autoconfig will fail (because this is Windows, not Linux) so we will do it manually in the next step.

    1. Configure cake, then return to your home directory ...
       cake -autoconfig
       cd %HOMEPATH%
    
    1. Write a simple program that requires C99, and save it as hello.c. For example ...
       #include <stdio.h>
       void main() {
         for (int i = 0; i < 10; i++) { // requires C99!
            printf("hello %d from %s\n", i, __func__); // requires C99!
         }
         while(1);
       }
    
    1. Use cake to convert the program to C89. then compile it with Catalina ...
       cake-main\src\cake hello.c -o cake.c
       catalina -p2 -lci cake.c -o hello
    
    1. Assuming you have a Propeller connected, execute the program ...
       payload -i hello
    

    Et voilà! Catalina now compiles C99 programs!

    This is not the end of the story, of course - Catalina's libraries will need some updates and additions to be C99 compatible - but this already does everything I had intended to do myself when I got time, and with zero effort!

  • RaymanRayman Posts: 15,506

    That is nice. Imagining that C23 is like a newer C++ but without classes.
    Is that close?

  • RaymanRayman Posts: 15,506

    C23 has #embed for binary data. That would be nice...

  • RossHRossH Posts: 5,639
    edited 2025-08-04 00:16

    @Rayman said:
    That is nice. Imagining that C23 is like a newer C++ but without classes.
    Is that close?

    C23 is definitely not C++. But it is true that a few of the changes introduced in C23 were designed to align it more with C++ than for their own intrinsic utility. Whether you think this is a good thing or not depends on whether you think C++ is a good language or not.

    C23 has #embed for binary data. That would be nice...

    The manual says #embed is 'partially implemented'. There is an example included of using it, so I'm not sure exactly what is missing. In general C23 is currently less fully implemented than C99 or C11.

  • RossHRossH Posts: 5,639

    I just added a NOTE to the first post in this thread about building cake on Linux - you need to manually adjust the include file directories to make it use Catalina's include files instead of gcc's. Otherwise some things won't work.

    I will fix this in the version I will include with Catalina.

    Ross.

  • RossHRossH Posts: 5,639
    edited 2025-08-08 03:14

    Apologies for the delay. I had to have the Cake author fix a couple of minor issues, but now Cake is ready to work with Catalina.

    I have just uploaded a version configured to suit Catalina 8.8 to SourceForge here. The zip file contains sources and Windows binaries. It should be unzipped in Catalina's source folder.

    Once unzipped, on Windows you can just install it - i.e.

    cd %LCCDIR%\source\cake
    install
    

    On Linux you have to build it from source (and you may need superuser privileges):

    cd $LCCDIR/source/cake
    chmod a+x build
    ./build
    

    A simple demo program (hello_99.c) is included, and can be built and executed using commands like:

    cake hello_99.c -o hello.c
    catalina -p2 -lc hello.c
    payload -i hello
    

    See the README.TXT file in the cake folder for more details.

Sign In or Register to comment.