Skip to content

Commit c3dc075

Browse files
authored
v.help: document the -is_o option (in v help build-c) and add a test for it (#25052)
1 parent 5413a25 commit c3dc075

6 files changed

Lines changed: 55 additions & 2 deletions

File tree

‎vlib/v/builder/cc.v‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ fn (mut v Builder) thirdparty_object_args(ccoptions CcompilerOptions, middle []s
592592

593593
fn (mut v Builder) setup_output_name() {
594594
if !v.pref.is_shared && v.pref.build_mode != .build_module && v.pref.os == .windows
595-
&& !v.pref.out_name.ends_with('.exe') {
595+
&& !v.pref.is_o && !v.pref.out_name.ends_with('.exe') {
596596
v.pref.out_name += '.exe'
597597
}
598598
// Output executable name

‎vlib/v/help/build/build-c.txt‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,15 @@ see also `v help build`.
176176
Tell V to compile a shared object instead of an executable.
177177
The resulting file extension will be `.dll` on Windows and `.so` on Unix systems
178178

179+
-is_o
180+
Tell V to compile an .o object file instead of an executable.
181+
This is useful if you plan to create a module, that will be later linked to a
182+
larger program, potentially written in another language.
183+
Note: this option currently works with gcc/clang/tcc, but not msvc.
184+
Note: use either -no-skip-unused or tag the functions you expect to be publicly
185+
usable in the .o file with `@[markused]` . Adding `-gc none` may be also needed,
186+
unless you plan to link with -lgc later.
187+
179188
# Memory management
180189
-autofree
181190
Free memory used in functions automatically.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module abc
2+
3+
@[markused]
4+
pub fn addition(x int, y int) int {
5+
return x + y
6+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
module main
2+
3+
import os
4+
5+
const tfolder = os.join_path(os.vtmp_dir(), 'vobjfile_${os.getuid()}')
6+
const vexe = os.quoted_path(@VEXE)
7+
const gcc = os.quoted_path(os.find_abs_path_of_executable('gcc') or {
8+
println('This program needs `gcc` to be present.')
9+
println('OK')
10+
exit(0)
11+
})
12+
13+
fn lexec(cmd string) os.Result {
14+
println('>>> lexec cmd: ${cmd}')
15+
res := os.execute_or_exit(cmd)
16+
return res
17+
}
18+
19+
fn main() {
20+
os.chdir(@DIR)!
21+
os.rmdir_all(tfolder) or {}
22+
os.mkdir_all(tfolder)!
23+
lexec('${vexe} -is_o -o "${tfolder}/abc.o" -gc none -cc ${gcc} abc/')
24+
lexec('${gcc} -o "${tfolder}/main_in_c.o" -c main_in_c.c')
25+
lexec('${gcc} -o "${tfolder}/program.exe" "${tfolder}/abc.o" "${tfolder}/main_in_c.o"')
26+
res := lexec('${tfolder}/program.exe')
27+
print(res.output)
28+
assert res.output.starts_with('Result of addition: 30')
29+
os.rmdir_all(tfolder) or {}
30+
println('OK')
31+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include <stdio.h>
2+
extern int abc__addition(int x, int y);
3+
int main() {
4+
int res = abc__addition(10,20);
5+
printf("Result of addition: %d\n", res);
6+
return 0;
7+
}

‎vlib/v/slow_tests/run_project_folders_test.v‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ fn test_v_profile_works() {
3030
res := os.execute('${os.quoted_path(vexe)} run ${os.quoted_path(folder_path)}')
3131
delta := time.ticks() - t
3232
// eprintln('res: $res')
33-
assert res.exit_code == 0
33+
assert res.exit_code == 0, 'failing res: ${res}'
3434
assert res.output.len > 0
3535
assert res.output.contains('OK')
3636
term.clear_previous_line()

0 commit comments

Comments
 (0)