Using the C API on Linux

So I want to chronicle what I did to get a Playdate game with the C API compiling on Linux. This might not be the ideal solutions, but it's what got me running.

For reference:
I downloaded the 1.4.0 Linux SDK https://devforum.play.date/t/playdate-sdk-1-4-0/1858
I'm on Manjaro Linux (KDE).
I've done my initial Playdate setup according to info in this thread: Playdate SDK for Linux?

Copy the Example Project
As a starting point I copied the folder /C_API/Examples/Hello World/ from the Playdate SDK, as it contains a simple example project.

Install the correct Compiler
I'm on Manjaro, so in my case I had to install the community/arm-none-eabi-gcc and community/arm-none-eabi-newlib packages. This might be different for your distro.

CMake or Make?
There are two possible approaches to take, either using only a Makefile, or using CMake. Both work just fine. Just a Makefile is less complex, but if you want integration with IDEs (e.g. CLion) you will want to use CMake/CLion. I'll go over both. (I'll assume you've already installed make/cmake.)

Just a Makefile

Okay, what we want to do, is open that Makefile and fix the path to the Playdate SDK. It will have something like

# Locate the SDK
SDK = $(shell egrep '^\s*SDKRoot' ~/.Playdate/config | head -n 1 | cut -c9-)

in there, which seems to try to parse the path out of a config file, which doesn't exist. So instead I just hardcoded the path (of course your path will most likely be different)

# Locate the SDK
SDK = /home/toni/playdate

While you're here, you can change the name of your output folder by changing:

PRODUCT = HelloWorld.pdx

Then simply call make in your project directory and you will get a HelloWorld.pdx (or whatever you changed it to), which you should be able to open in the simulator or upload to the device.

CMake + CLion

Again, first we want to fix the Playdate SDK path in the CMakeLists.txt file in our example project. We will change

execute_process(
		COMMAND bash -c "egrep '^\\s*SDKRoot' $HOME/.Playdate/config"
		COMMAND head -n 1
		COMMAND cut -c9-
		OUTPUT_VARIABLE SDK
		OUTPUT_STRIP_TRAILING_WHITESPACE
)

to (of course again, use YOUR sdk path)

execute_process(
		COMMAND bash -c "echo /home/toni/playdate"
		OUTPUT_VARIABLE SDK
		OUTPUT_STRIP_TRAILING_WHITESPACE
)

Again, you can change the name of the output pdx by changing the hello_world in the following lines

# Game Name Customization
set(PLAYDATE_GAME_NAME hello_world)
set(PLAYDATE_GAME_DEVICE hello_world_DEVICE)

The instructions in the Inside Playdate with C document in the SDK actually do a good job of explaining how to import the project in CLion, so best to follow those instructions next.

After you've set up the CLion project you will want to go to your Playdate SDK and open up the file C_API/buildsupport/arm.cmake and fix the following line

set(TOOLCHAIN_DIR "/usr/local/playdate/gcc-arm-none-eabi-9-2019-q4-major")

which is most likely not the correct path to your arm gcc. Use the command which arm-none-eabi-gcc to find the correct path, in my case it was /usr/bin/arm-none-eabi-gcc. So I changed the line to

set(TOOLCHAIN_DIR "/usr")

since the /bin/arm-none-eabi-gcc is appended after it.

Now when you build your project in CLion, you will get a hello_world.pdx (or however you named it) which again you should be able to use in the simulator and device.

Conclusio
All in all it's not to complicated, it's just not fully documented. Things might change in future SDK releases, but this hopefully can still be a useful resource. Some things I did might not be a good idea, so please correct me in that case, I want to learn.
I haven't even tackled debugging yet, but this at least gets me started to get something working.

2 Likes

Addendum:
Only my approach using the Makefile creates a .pdx which can be used for the Simulator and the Device, while with CMake I can only build for the Device.
The .pdx folder (or "bundle") will contain a pdex.bin and a pdex.so file. The pdex.bin is for running on Device, while the pdex.so file is for running on the simulator.
I did not notice the CMake solution not working for the simulator, as there was still a pdex.so from an earlier call to make in there.

My CLion project has two Targets, "Debug" (for the simulator) and "Debug_DEVICE" (for the device, as described in "Inside Playdate with C"). "Debug_DEVICE" is working fine, but when I try to build "Debug" I get the following output/error:

Full output from building Debug target
====================[ Build | hello_world | Debug ]=============================
/usr/bin/cmake --build "/home/toni/MyProjects/2021/playdate-vault/Hello World/cmake-build-debug" --target hello_world -- -j 9
/usr/bin/cmake -S"/home/toni/MyProjects/2021/playdate-vault/Hello World" -B"/home/toni/MyProjects/2021/playdate-vault/Hello World/cmake-build-debug" --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/make  -f CMakeFiles/Makefile2 hello_world
make[1]: Entering directory '/home/toni/MyProjects/2021/playdate-vault/Hello World/cmake-build-debug'
/usr/bin/cmake -S"/home/toni/MyProjects/2021/playdate-vault/Hello World" -B"/home/toni/MyProjects/2021/playdate-vault/Hello World/cmake-build-debug" --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start "/home/toni/MyProjects/2021/playdate-vault/Hello World/cmake-build-debug/CMakeFiles" 2
/usr/bin/make  -f CMakeFiles/Makefile2 CMakeFiles/hello_world.dir/all
make[2]: Entering directory '/home/toni/MyProjects/2021/playdate-vault/Hello World/cmake-build-debug'
/usr/bin/make  -f CMakeFiles/hello_world.dir/build.make CMakeFiles/hello_world.dir/depend
make[3]: Entering directory '/home/toni/MyProjects/2021/playdate-vault/Hello World/cmake-build-debug'
cd "/home/toni/MyProjects/2021/playdate-vault/Hello World/cmake-build-debug" && /usr/bin/cmake -E cmake_depends "Unix Makefiles" "/home/toni/MyProjects/2021/playdate-vault/Hello World" "/home/toni/MyProjects/2021/playdate-vault/Hello World" "/home/toni/MyProjects/2021/playdate-vault/Hello World/cmake-build-debug" "/home/toni/MyProjects/2021/playdate-vault/Hello World/cmake-build-debug" "/home/toni/MyProjects/2021/playdate-vault/Hello World/cmake-build-debug/CMakeFiles/hello_world.dir/DependInfo.cmake" --color=
make[3]: Leaving directory '/home/toni/MyProjects/2021/playdate-vault/Hello World/cmake-build-debug'
/usr/bin/make  -f CMakeFiles/hello_world.dir/build.make CMakeFiles/hello_world.dir/build
make[3]: Entering directory '/home/toni/MyProjects/2021/playdate-vault/Hello World/cmake-build-debug'
[ 50%] Linking C shared library libhello_world.so
/usr/bin/cmake -E cmake_link_script CMakeFiles/hello_world.dir/link.txt --verbose=1
/usr/bin/cc -fPIC -g -shared -Wl,-soname,libhello_world.so -o libhello_world.so CMakeFiles/hello_world.dir/src/main.c.o 
/usr/bin/cmake -E copy /home/toni/MyProjects/2021/playdate-vault/Hello\ World/cmake-build-debug/libhello_world.dylib /home/toni/MyProjects/2021/playdate-vault/Hello\ World/Source/pdex.dylib
Error copying file "/home/toni/MyProjects/2021/playdate-vault/Hello World/cmake-build-debug/libhello_world.dylib" to "/home/toni/MyProjects/2021/playdate-vault/Hello World/Source/pdex.dylib".
make[3]: *** [CMakeFiles/hello_world.dir/build.make:97: libhello_world.so] Error 1
make[3]: *** Deleting file 'libhello_world.so'
make[3]: Leaving directory '/home/toni/MyProjects/2021/playdate-vault/Hello World/cmake-build-debug'
make[2]: *** [CMakeFiles/Makefile2:86: CMakeFiles/hello_world.dir/all] Error 2
make[2]: Leaving directory '/home/toni/MyProjects/2021/playdate-vault/Hello World/cmake-build-debug'
make[1]: *** [CMakeFiles/Makefile2:93: CMakeFiles/hello_world.dir/rule] Error 2
make[1]: Leaving directory '/home/toni/MyProjects/2021/playdate-vault/Hello World/cmake-build-debug'
make: *** [Makefile:127: hello_world] Error 2
Error copying file "/home/toni/MyProjects/2021/playdate-vault/Hello World/cmake-build-debug/libhello_world.dylib" to "/home/toni/MyProjects/2021/playdate-vault/Hello World/Source/pdex.dylib".
make[3]: *** [CMakeFiles/hello_world.dir/build.make:97: libhello_world.so] Error 1

Copying a file seems to fail, but I don't understand why. Any help would be appreciated.

Thanks for this write-up! I've updated the Simulator to create the '~/.Playdate/config` file when the SDK changes so the example projects Makefiles will build OOB after installing the arm/clang tools. That should help get device and simulator building.

1 Like

I'm also updating the docs to explain how to get up and running on linux.

2 Likes

Hi, I'm kinda new here, and in C with all this make and cmake stuff. And actually and in Linux too. So, I am stuck here, and decided ask some dumb question and be social. So my story is that I am trying to build it from hello_world example, but as I've seen, C_API/buildsupport/arm.> cmake and it doesn't contain set(TOOLCHAIN_DIR. anymore. So from here i though maybe someone could point me in the right direction and hint me where I could possibly find the new definition of the mighty TOOLCHAIN. I'll add a screen of my error when I am tried to build pdx with clion/cmake. And also a screen related to the question, is it possible to set arm-none-eabi-gcc here? For me, it changes nothing for now, but maybe it could lead me somewhere interesting. Thank you for attention.

screens


I am using garuda Linux with gnome (arch) btw

@nyanfall Are you trying to build for the simulator or for the device? (That's one difference with Lua versus C, you need different builds for the device and the simulator.)

If you're just trying it out for the simulator, you shouldn't need the ARM toolchain. If you are building for the device, you need to point cmake to use that arm.cmake file as the toolchain set-up.

Since you're using CLion, have you done step 3 of; Inside Playdate with C ?

Considering the error-messages (and perhaps other missed steps from the docs), there's a few other things that you may or may not need to solve first though... :

  • It seems like you're missing make, have you installed it, or any of what you'd call the 'build-essentials'? (You could install make separately, but to prevent further problems down the line, the package you'd actually want is likely called 'build-essential' or 'base-devel' depending on the distro, since there's more than make that could be missing if that isn't there.)
  • Is that file there that it says it's missing in the 1st error message? Have you set the PLAYDATE_SDK environment variable?
  • (If you are building for the device.) Have you installed the arm-none-eabi-newlib package, like it says in the 'Inside Playdate with C' documentation?

As someone who's set this up recently, I found the documentation quite good throughout, mostly, fortunately. The thing that's 'missing' there (that is relevant in this case) is that it assumes you already have C and your build-tools (and the IDE) set-up before doing the whole Playdate install.

Maybe I'm assuming too much here, but; if you're new to game-development and/or programming, I'd really suggest starting with the developer recommended route (even mentioned at the start of the documentation) of using Lua (as it's easier to set-up, and not just because there's only 1 build for both the device and the sim needed) and only going for C if you really need the performance, that is your game is lagging and you can't optimize it further in Lua. (There's also a bunch of convenient functions you get that aren't present in the C API as well...)

@remco Thank you for your answer.

I was trying to build for the device first of all. But my next step would be probably to build for simulator.
Yeah, I added a profile to cmake, I guess. Screenshot for proof (I am really not sure, because I didn't understand fully how make and cmake work. And for me, it is the second time ever in clion, and rider experience looks to me absolutely irrelevant )

ScreenShot with my cmake profile and some --version


Yeah, I have installed make and cmake packages before, also I have arm-none-eabi-newlib. Trying to add base-devel with my fingers crossed. And it's not helped me. ;C
Playdate SDK env var was set when I was trying to build with Lua, and it was successful. By now want to try C-like language.
If I can provide any additional info on my case to make it clear - let me know, please.
That error occurred to me when I was following docs at 3.3 .3.

I am pretty new to C., in college, I studied CPP, but at a time it didn't get me where it should. I am not new in the industry, working as a game designer. But passionate about learning c and embedded myself, decided that the starting point of developing a simple game for playdate would suit me I guess.

@nyanfall Ah, I see... you're missing the starting / in the path to the toolchain file. It should be ...=/home/..., not ...=home/.... (Otherwise it's relative to whatever working directory your IDE is operating from.)

edit: After you've corrected that, it might be that you need to change the Build Tool from 'Ninja' to 'make', as I'm not sure Ninja will work. (Though it might autodetect correctly once you got the other things set-up.)

1 Like

thank you so much. It is really the case. very sorry for that dumb question!

1 Like