Noob C Question: #include cannot open source file


Hi all, I'm new here. I'm a C# developer and want to expand my skills with a more low level project so I decided to give Playdate development with C a go.

I'm developing on Windows with Visual Studio and I followed the instructions in the C documentation. So far so good - I built the project with cmake, got got it to open in VS, tinkered around with it and built it for the simulator.

Right now I want to add a new C file to the project so I can separate my code. I do this by adding a new class with a .c extension and this generates the header file for me as well. However, as soon as I try to #include "mynewclass.h" I get the error: "cannot open source file "mynewclass.h".

Why can't I find the header I have just created in the project?

If you're using a Visual Studio project and following our documentation, each time you create a new file you'll need to regenerate the project using cmake to loop that file into the project. Hope this helps!

1 Like

Thanks so much for the answer! That helped. I added a game.c and a game.h file and added them to CMakeLists after main:

add_library(${PLAYDATE_GAME_NAME} SHARED src/main.c src/game.c)

VisualStudio now seems to recognize them. It's a somewhat cumbersome process but for learning purposes is great. Maybe I can make a tool to automate this process in future.

Anyways, thanks so much again!

Glad that helped. A quick tip, if you look at the CMake file from the 3D Library example you can see the use of the cmake glob pattern which loops in all files that match a pattern. While some folks frown on it, it's fine for small projects like Playdate games where the project is generated once. If you use a glob, you don't have to manually add the file to the CMakeLists.txt file, you just have to regenerate the project.

1 Like