C error: 'ld-linux-x86-64.so.2' not found
Describe the bug
A C error occurs when using C2V to generate a wrapper for a function that returns char* in C. It seems like it's not linking to the right libc though. This occurs in both, TCC and GCC.
Expected Behavior
For it to print Hello, World!
Current Behavior
TCC
==================
/tmp/v_1000/src.8173178478193935718.tmp.c:12465: warning: cast between pointer and integer of different size
/lib64/libc.so.6: error: referenced dll 'ld-linux-x86-64.so.2' not found
/lib/libc.so.6: error: bad architecture
tcc: error: /usr/lib64/libc.so: unrecognized file type
/lib/libc.so.6: error: bad architecture
/lib/libc.so.6: error: bad architecture
tcc: error: /usr/lib/libc.so: unrecognized file type
tcc: error: undefined symbol 'hello'
tcc: error: undefined symbol '__ehdr_start'
tcc: error: undefined symbol '__gcc_personality_v0'
tcc: error: undefined symbol '_Unwind_Resume'
tcc: error: undefined symbol '_Unwind_GetCFA'
...
==================
(Use `v -cg` to print the entire error message)
builder error:
==================
C error. This should never happen.
This is a compiler bug, please report it using `v bug file.v`.
https://github.com/vlang/v/issues/new/choose
You can also use #help on Discord: https://discord.gg/vlang
GCC
/tmp/v_1000/src.7237482130334976229.tmp.c: In function ‘main__hello’:
/tmp/v_1000/src.7237482130334976229.tmp.c:12467:19: warning: implicit declaration of function ‘hello’; did you mean ‘ftello’? [-Wimplicit-function-declaration]
12467 | i8* _t1 = hello();
| ^~~~~
| ftello
/tmp/v_1000/src.7237482130334976229.tmp.c:12467:19: warning: initialization of ‘i8 *’ {aka ‘signed char *’} from ‘int’ makes pointer from integer without a cast [-Wint-conversion]
/usr/lib/gcc/x86_64-pc-linux-gnu/12/../../../../x86_64-pc-linux-gnu/bin/ld: /tmp/ccYHRGOb.o: in function `main__hello':
/tmp/v_1000/src.7237482130334976229.tmp.c:12467: undefined reference to `hello'
collect2: error: ld returned 1 exit status
builder error:
==================
C error. This should never happen.
This is a compiler bug, please report it using `v bug file.v`.
https://github.com/vlang/v/issues/new/choose
You can also use #help on Discord: https://discord.gg/vlang
Clang
/tmp/v_1000/c_interop.13513377727128119546.tmp.c:12467:12: warning: call to undeclared function 'hello'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
i8* _t1 = hello();
^
/tmp/v_1000/c_interop.13513377727128119546.tmp.c:12467:6: error: incompatible integer to pointer conversion initializing 'i8 *' (aka 'signed char *') with an expression of type 'int' [-Wint-conversion]
i8* _t1 = hello();
^ ~~~~~~~
1 warning and 1 error generated.
builder error:
==================
C error. This should never happen.
This is a compiler bug, please report it using `v bug file.v`.
https://github.com/vlang/v/issues/new/choose
You can also use #help on Discord: https://discord.gg/vlang
Reproduction Steps
// main.v
module main
fn main() {
msg := hello()
println(msg)
}
// demo.v
[translated]
module main
fn C.hello() &i8
pub fn hello() &i8 {
return C.hello()
}
// demo.h
char* hello();
// demo.c
#include <stdio.h>
#include "demo.h"
char* hello() {
return "Hello, World!\n";
}
Possible Solution
No response
Additional Information/Context
I tried changing the file extension to .c.v and it didn't seem to work either.
Also tried adding #include "demo.h" but it said it couldn't find the header file.
V version
V 0.3.3 8c35ee0.adcd16b
Environment details (OS name and version, etc.)
V full version: V 0.3.3 8c35ee0.adcd16b
OS: linux, "Gentoo Linux"
Processor: 16 cpus, 64bit, little endian, AMD Ryzen 9 5900HX with Radeon Graphics
getwd: /home/csfore/workspaces/v/demos/c_interop/src
vexe: /home/csfore/v/v
vexe mtime: 2023-04-20 14:43:19
vroot: OK, value: /home/csfore/v
VMODULES: OK, value: /home/csfore/.vmodules
VTMP: OK, value: /tmp/v_1000
Git version: git version 2.39.2
Git vroot status: weekly.2023.13-127-gadcd16b1
.git/config present: true
CC version: cc (Gentoo 12.2.1_p20230121-r1 p10) 12.2.1 20230121
thirdparty/tcc status: thirdparty-linux-amd64 12f392c3
v.mod
Module {}
main.v
// main.v
module main
#include "demo.h"
#flag @VMODROOT/demo.c -I @VMODROOT
fn main() {
msg := hello()
println(msg)
}
Other files should be same.
The errors about libc are definitely not related. This is a problem with tcc and your os.
There is a problem with this code too. C strings are not V strings cstring_to_vstring() has to be used first to convert it.