-l
for adding C library names that you want to get linked-L
for adding C library files search paths-D
for setting compile time variables
You can use different flags for different targets. Currently the linux
, darwin
, freebsd
, and windows
flags are supported.
NB: Each flag must go on its own line (for now)
Add #pkgconfig
directive is used to tell the compiler which modules should be used for compiling and linking using the pkg-config files provided by the respective dependencies.
As long as backticks can’t be used in #flag
and spawning processes is not desirable for security and portability reasons, V uses its own pkgconfig library that is compatible with the standard freedesktop one.
If no flags are passed it will add --cflags
and --libs
, both lines below do the same:
You can also include C code directly in your V module. For example, let’s say that your C code is located in a folder named ‘c’ inside your module folder. Then:
- Put a v.mod file inside the toplevel folder of your module (if you created your module with
v new
you already have v.mod file). For example:
NB: @VROOT will be replaced by V with the nearest parent folder, where there is a v.mod file. Any .v file beside or below the folder where the v.mod file is, can use #flag @VROOT/abc
to refer to this folder. The @VROOT folder is also prepended to the module lookup path, so you can import other modules under your @VROOT, by just naming them.
The instructions above will make V look for an compiled .o file in your module folder/c/implementation.o
. If V finds it, the .o file will get linked to the main executable, that used the module. If it does not find it, V assumes that there is a @VROOT/c/implementation.c
file, and tries to compile it to a .o file, then will use that.
This allows you to have C code, that is contained in a V module, so that its distribution is easier. You can see a complete minimal example for using C code in a V wrapper module here: . Another example, demonstrating passing structs from C to V and back again: interoperate between C to V to C.
You can use -cflags
to pass custom flags to the backend C compiler. You can also use -cc
to change the default C backend compiler. For example: -cc gcc-9 -cflags -fsanitize=thread
.
NB: The .vstring() and .vstring_with_len() methods do NOT create a copy of the cstring
, so you should NOT free it after calling the method .vstring()
. If you need to make a copy of the C string (some libc APIs like getenv
pretty much require that, since they return pointers to internal libc memory), you can use cstring_to_vstring(cstring)
.
On Windows, C APIs often return so called strings (utf16 encoding). These can be converted to V strings with string_from_wide(&u16(cwidestring))
.
V has these types for easier interoperability with C:
voidptr
for C’svoid*
,byteptr
for C’sbyte*
andcharptr
for C’schar*
.&charptr
for C’schar**
To cast a voidptr
to a V reference, use user := &User(user_void_ptr)
.
voidptr
can also be dereferenced into a V struct through casting: user := User(user_void_ptr)
.