mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
docs: call_v_from_c example (#15844)
This commit is contained in:
2
examples/call_v_from_c/.gitignore
vendored
Normal file
2
examples/call_v_from_c/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
# Ignore the generated C files in this directory
|
||||
*.c
|
39
examples/call_v_from_c/README.md
Normal file
39
examples/call_v_from_c/README.md
Normal file
@ -0,0 +1,39 @@
|
||||
## A simple example to show how to call a function written in v from c
|
||||
|
||||
### Compile as a shared library
|
||||
|
||||
#### On Linux:
|
||||
|
||||
Step 1: Compile the v code to a shared library using `v -cc gcc -shared v_test_print.v` or
|
||||
`v -cc gcc -shared v_test_math.v`.
|
||||
|
||||
Step 2: Compile the c file using `gcc test_print.c v_test_print.so -o test_print` or
|
||||
`gcc test_math.c v_test_math.so -o test_math`.
|
||||
|
||||
Step 3: Run the compiled c file using `LD_LIBRARY_PATH=. ./test_print` or
|
||||
`LD_LIBRARY_PATH=. ./test_math`.
|
||||
|
||||
#### On Windows:
|
||||
|
||||
Step 1: Compile the v code to a shared library using `v -cc gcc -shared v_test_print.v` or
|
||||
`v -cc gcc -shared v_test_math.v`.
|
||||
|
||||
Step 2: Compile the c file using `gcc test_print.c v_test_print.dll -o test_print.exe` or
|
||||
`gcc test_math.c v_test_math.dll -o test_math.exe`.
|
||||
|
||||
Step 3: Run the compiled c file using `test_print.exe` or `test_math.exe`.
|
||||
|
||||
### Compile as a c file
|
||||
|
||||
***Requirements: `libgc` must be installed***
|
||||
|
||||
Step 1: Compile the v code to a shared library using
|
||||
`v -shared -cc gcc -o v_test_print.c v_test_print.v` or
|
||||
`v -shared -cc gcc -o v_test_math.c v_test_math.v`.
|
||||
|
||||
*Specifying the output with a `.c` extension will generate the corresponding C source file.*
|
||||
|
||||
Step 2: Compile the c file using `gcc test_print.c v_test_print.c -o test_print -lgc` or
|
||||
`gcc test_math.c v_test_math.c -o test_math -lgc -lm`.
|
||||
|
||||
Step 3: Run the compiled c file using `./test_print` or `./test_math`.
|
14
examples/call_v_from_c/test_math.c
Normal file
14
examples/call_v_from_c/test_math.c
Normal file
@ -0,0 +1,14 @@
|
||||
#include <stdio.h>
|
||||
|
||||
extern int square(int i);
|
||||
|
||||
extern double sqrt_of_sum_of_squares(double x, double y);
|
||||
|
||||
int main()
|
||||
{
|
||||
int i = 10;
|
||||
printf("square(%d) = %d\n", i, square(i));
|
||||
double x=0.9;
|
||||
double y=1.2;
|
||||
printf("sqrt_of_sum_of_squares(%f, %f) = %f\n", x, y, sqrt_of_sum_of_squares(x, y));
|
||||
}
|
7
examples/call_v_from_c/test_print.c
Normal file
7
examples/call_v_from_c/test_print.c
Normal file
@ -0,0 +1,7 @@
|
||||
extern void foo(const char* s);
|
||||
|
||||
int main()
|
||||
{
|
||||
foo("hello");
|
||||
foo("bye");
|
||||
}
|
13
examples/call_v_from_c/v_test_math.v
Normal file
13
examples/call_v_from_c/v_test_math.v
Normal file
@ -0,0 +1,13 @@
|
||||
module test_math
|
||||
|
||||
import math
|
||||
|
||||
[export: 'square']
|
||||
fn calculate_square(i int) int {
|
||||
return i * i
|
||||
}
|
||||
|
||||
[export: 'sqrt_of_sum_of_squares']
|
||||
fn calculate_sqrt_of_sum_of_squares(x f64, y f64) f64 {
|
||||
return math.sqrt(x * x + y * y)
|
||||
}
|
6
examples/call_v_from_c/v_test_print.v
Normal file
6
examples/call_v_from_c/v_test_print.v
Normal file
@ -0,0 +1,6 @@
|
||||
module test_print
|
||||
|
||||
[export: 'foo']
|
||||
fn show_foo(s &char) {
|
||||
println(unsafe { cstring_to_vstring(s) })
|
||||
}
|
Reference in New Issue
Block a user