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:
#include "stdio.h"
int main() {
for (int i = 0; i < 10; i++) {
printf("hello world\n");
}
return 0;
}
Run v translate test.c
, and V will generate test.v
:
fn main() {
for i := 0; i < 10; i++ {
println('hello world')
}
}
To generate a wrapper on top of a C library use this command:
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.