Translating C to V

TODO: translating C to V will be available in V 0.3.

V can translate your C code to human readable V code and generate V wrappers on top of C libraries.

Let’s create a simple program test.c first:

  1. #include "stdio.h"
  2. int main() {
  3. for (int i = 0; i < 10; i++) {
  4. printf("hello world\n");
  5. }
  6. return 0;
  7. }

Run v translate test.c, and V will generate test.v:

  1. fn main() {
  2. for i := 0; i < 10; i++ {
  3. println('hello world')
  4. }
  5. }

To generate a wrapper on top of a C library use this command:

  1. v wrapper c_code/libsodium/src/libsodium

This will generate a directory libsodium with a V module.

Example of a C2V generated libsodium wrapper:

https://github.com/medvednikov/libsodium

When should you translate C code and when should you simply call C code from V?

If you have well-written, well-tested C code, then of course you can always simply call this C code from V.

Translating it to V gives you several advantages:

  • If you plan to develop that code base, you now have everything in one language, which is much safer and easier to develop in than C.
  • Cross-compilation becomes a lot easier. You don’t have to worry about it at all.
  • No more build flags and include files either.