mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
dl: move 'example' to 'examples/dynamic_library_loading' (#9187)
This commit is contained in:
@@ -1,19 +0,0 @@
|
||||
module library
|
||||
|
||||
// add_1 is exported with the C name `add_1`.
|
||||
// It can be called by external programs, when the module is compiled
|
||||
// as a shared library.
|
||||
// It is exported, because the function is declared as public with `pub`.
|
||||
// The exported C name is `add_1`, because of the export: tag.
|
||||
// (Normally, the exported name is a V mangled version based on the module
|
||||
// name followed by __, followed by the fn name, i.e. it would have been
|
||||
// `library__add_1`, if not for the export: tag).
|
||||
[export: 'add_1']
|
||||
pub fn add_1(x int, y int) int {
|
||||
return my_private_function(x + y)
|
||||
}
|
||||
|
||||
// this function is not exported and will not be visible to external programs.
|
||||
fn my_private_function(x int) int {
|
||||
return 1 + x
|
||||
}
|
@@ -1,17 +0,0 @@
|
||||
module main
|
||||
|
||||
import os
|
||||
import dl
|
||||
|
||||
type FNAdder = fn (int, int) int
|
||||
|
||||
fn main() {
|
||||
library_file_path := os.join_path(os.getwd(), dl.get_libname('library'))
|
||||
handle := dl.open(library_file_path, dl.rtld_lazy)
|
||||
eprintln('handle: ${ptr_str(handle)}')
|
||||
mut f := &FNAdder(0)
|
||||
f = dl.sym(handle, 'add_1')
|
||||
eprintln('f: ${ptr_str(f)}')
|
||||
res := f(1, 2)
|
||||
eprintln('res: $res')
|
||||
}
|
@@ -1,52 +0,0 @@
|
||||
module main
|
||||
|
||||
import os
|
||||
import dl
|
||||
|
||||
const (
|
||||
vexe = os.real_path(os.getenv('VEXE'))
|
||||
cfolder = os.dir(@FILE)
|
||||
so_ext = dl.dl_ext
|
||||
library_file_name = os.join_path(cfolder, dl.get_libname('library'))
|
||||
)
|
||||
|
||||
fn test_vexe() {
|
||||
eprintln('vexe: $vexe')
|
||||
assert vexe != ''
|
||||
eprintln('os.executable: ' + os.executable())
|
||||
eprintln('@FILE: ' + @FILE)
|
||||
eprintln('cfolder: $cfolder')
|
||||
eprintln('so_ext: $so_ext')
|
||||
eprintln('library_file_name: $library_file_name')
|
||||
}
|
||||
|
||||
fn test_can_compile_library() {
|
||||
os.chdir(cfolder)
|
||||
os.rm(library_file_name) or { }
|
||||
res := v_compile('-d no_backtrace -o library -shared library.v')
|
||||
eprintln('res: $res')
|
||||
assert os.is_file(library_file_name)
|
||||
}
|
||||
|
||||
fn test_can_compile_main_program() {
|
||||
os.chdir(cfolder)
|
||||
assert os.is_file(library_file_name)
|
||||
result := v_compile('run use.v')
|
||||
eprintln('result: $result')
|
||||
assert result.output.contains('res: 4')
|
||||
os.rm(library_file_name) or { }
|
||||
}
|
||||
|
||||
fn v_compile(vopts string) os.Result {
|
||||
cmd := '"$vexe" -showcc $vopts'
|
||||
eprintln('>>> v_compile cmd: $cmd')
|
||||
res := os.exec(cmd) or { panic(err) }
|
||||
eprintln('>>> v_compile res: $res')
|
||||
// assert res.exit_code == 0
|
||||
$if !windows {
|
||||
os.system('ls -al $cfolder')
|
||||
} $else {
|
||||
os.system('dir $cfolder /a')
|
||||
}
|
||||
return res
|
||||
}
|
Reference in New Issue
Block a user