From 7439ed7e2b326d12bc62e99784d9203a6f41dff0 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Thu, 17 Dec 2020 12:25:31 +0100 Subject: [PATCH] doc: document C2V wrapper generator --- doc/docs.md | 48 +++++++++++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/doc/docs.md b/doc/docs.md index 820c496b8d..3971920738 100644 --- a/doc/docs.md +++ b/doc/docs.md @@ -84,7 +84,7 @@ Anything you can do in other languages, you can do in V. * [Compile-time reflection](#compile-time-reflection) * [Limited operator overloading](#limited-operator-overloading) * [Inline assembly](#inline-assembly) - * [Translating C/C++ to V](#translating-cc-to-v) + * [Translating C to V](#translating-c-to-v) * [Hot code reloading](#hot-code-reloading) * [Cross compilation](#cross-compilation) * [Cross-platform shell scripts in V](#cross-platform-shell-scripts-in-v) @@ -3101,39 +3101,49 @@ fn main() { } ``` -## Translating C/C++ to V +## Translating C to V -TODO: translating C to V will be available in V 0.3. C++ to V will be available later this year. +TODO: translating C to V will be available in V 0.3. -V can translate your C/C++ code to human readable V code. -Let's create a simple program `test.cpp` first: +V can translate your C code to human readable V code and generate V wrappers on top of C libraries. -```cpp -#include -#include -#include + +Let's create a simple program `test.c` first: + +```c +#include "stdio.h" int main() { - std::vector s; - s.push_back("V is "); - s.push_back("awesome"); - std::cout << s.size() << std::endl; + for (int i = 0; i < 10; i++) { + printf("hello world\n"); + } return 0; } ``` -Run `v translate test.cpp` and V will generate `test.v`: +Run `v translate test.c`, and V will generate `test.v`: ```v fn main() { - mut s := []string{} - s << 'V is ' - s << 'awesome' - println(s.len) + for i := 0; i < 10; i++ { + println('hello world') + } } ``` -An online C/C++ to V translator is coming soon. +To generate a wrapper on top of a C library use this command: + +```bash +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?