Include standard C lib in .p4

Hi,

I tried to include C lib (e.g. #include <stdlib.h>) in .p4 file but failed compilation. I’m wondering whether p4 compiler supports it or not. Thank you very much.

Hi @svenchen,

Without going into an over detailed answer, and as far as I am concerned, this is not possible. You cannot import a C standard lib and use it in a P4 file, as you would do while programming in C :slight_smile: . Even though you can see some similar syntax patterns in P4 programs that remind to what you can see in a .c file, you cannot just call vsprintf or scanf from a P4 file and expect them to work.

However, you could use them in a compiler written in C or a similar library for C++ or even in libraries that support the execution of P4 externs in some targets. Creating a P4 extern could be what you are looking for, and you can actually program them for the bmv2 Simple Switch or a Netronome SmartNIC, but this last one might be deprecated nowadays. There might be other targets, especially the software targets, can also support them.

PD: If anything of what I said is wrong, I would be happy if any forum member corrects me :slight_smile:

Cheers,

Thank you so much. It’s really helpful!

Gong