VSCode and C API - #if TARGET_EXTENSION is causing errors

I am trying to set up a Playdate project using the C API using VSCode. Even after adding the C_API folder to the default include path in VSCodes c/c++ extension I am getting errors. I tracked down what was causing the error to the header files in C_API. Specifically where they have #if TARGET_EXTENSION. I got it to work by using a similar solution to what someone on the forum had done for swift. I made my own header file in C_API called playdate.h ...
#define TARGET_EXTENSION 1
#include "pd_api.h"
This is fixing the errors in VSCode if I import it instead of pd_api.h directly. But I'm wondering if this will cause any problem? is there a better way to get VSCode to recognize the playdate C API?

What OS and compiler are you using? Thanks!

I'm on Windows 10 and I am using the compiler from the 'Inside Playdate with C' guide. The arm compiler and whatever comes with visual studio I think and CMake. I'm kind of new to the C stuff so I'm not sure where id check the name otherwise. Also the errors are only in the VSCode editor not after being compiled. It builds and runs on playdate/simulator just fine. Its the red squiggles and lack of autocomplete stuff that was bothering me.

it is normal, and the red squigles are not compile errors, it is just vscode fuzzing out because the define is not define, if you do not use VSCODE that is defined as well and your not supposed to change source code or do what you did but you need to define the defines in the required files so you do not have to change source code
same for autocomplete stuff you need to add the include paths for the header files under C_API
for example like so :

For reference here you can see a msvc project (for the simulator) that got autogenerated adds the define as well

And when one builds for ARM for the device that define is defined as well on the commandline

when you use vscode you just have to setup more of these things manually, things that gets autoadded / generated if you use the normal msvc way. In theory when you build for the simulator you should define the target_simulator as well but not when you build for the device (then you should define target_playdate) and perhaps also the windlll define when building for simulator in windows but not when building for the device

1 Like

Thanks so much! I replaced my fix with this and it works great. Still learning about a bunch of stuff so I appreciate the details.