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: https://devforum.play.date/t/playdate-sdk-for-linux/628

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.

1 Like