Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

undefined reference to `tcc_backtrace' when linking against v generated .so file on Linux #23198

Open
plicease opened this issue Dec 18, 2024 · 6 comments
Labels
Bug This tag is applied to issues which reports bugs.

Comments

@plicease
Copy link

plicease commented Dec 18, 2024

Describe the bug

When creating a dynamic library on Linux I get a .so file referring to tcc symbols. Trying to link a C program with the system compiler (gcc) will fail as these symbols are missing. Using the same procedure it worked on macOS.

Aside: I know I can put C files into my V project and build a program that way, what I really want to be able to do is call into the v .so using FFI from Perl.

Reproduction Steps

✅ plicease-clean% v up
Updating V...
> git_command: git pull https://github.com/vlang/v master
V is already updated.
Current V version: V 0.4.8 afc07f4, timestamp: 2024-12-18 00:49:07 +0200
✅ plicease-clean% v new --lib libfoo
Input your project description: foo
Input your project version: (0.0.0)
Input your project license: (MIT)
Initialising ...
Created library project `libfoo`
✅ plicease-clean% cd libfoo
✅ plicease-clean% v -shared .
✅ plicease-clean% cat > foo.c
#include <stdio.h>

int libfoo__square(int);

int main() {
  printf("%d\n", libfoo__square(2));
}
✅ plicease-clean% gcc foo.c -L. -lfoo
/usr/bin/ld: ./libfoo.so: undefined reference to `tcc_backtrace'
/usr/bin/ld: ./libfoo.so: undefined reference to `__bt_exit'
/usr/bin/ld: ./libfoo.so: undefined reference to `__bt_init'
collect2: error: ld returned 1 exit status

Expected Behavior

I would expect a binary to be built and it should print out 4 when run.

Current Behavior

/usr/bin/ld: ./libfoo.so: undefined reference to `tcc_backtrace'
/usr/bin/ld: ./libfoo.so: undefined reference to `__bt_exit'
/usr/bin/ld: ./libfoo.so: undefined reference to `__bt_init'
collect2: error: ld returned 1 exit status

Possible Solution

I looked at the C source generated by I noticed that references to Tiny C were wrapped in #ifdefs for __TINYC__, so some knob to turn that define off or not including whatever that does define might do it. Trying to build generated by V with gcc gave me this error:

libfoo.c:749:2: error: #error VERROR_MESSAGE Header file <gc.h>, needed for module `builtin` was not found. Please install the corresponding development headers.
  749 | #error VERROR_MESSAGE Header file <gc.h>, needed for module `builtin` was not found. Please install the corresponding development headers.
      |  ^~~~~

Additional Information/Context

I tried this with both the 2024.50 and 2024.51 pre-built binaries and by building from git, and always got the same error. Using the pre-built 2024.51 on macOS worked fine though.

V version

Current V version: V 0.4.8 afc07f4, timestamp: 2024-12-18 00:49:07 +0200

Environment details (OS name and version, etc.)

V full version: V 0.4.8 afc07f4
OS: linux, Ubuntu 24.04.1 LTS (VM)
Processor: 32 cpus, 64bit, little endian, AMD EPYC 7702P 64-Core Processor

getwd: /home/ollisg/test/v/libfoo
vexe: /home/ollisg/opt/v/git/v
vexe mtime: 2024-12-18 03:47:43

vroot: OK, value: /home/ollisg/opt/v/git
VMODULES: OK, value: /home/ollisg/.vmodules
VTMP: OK, value: /tmp/v_2855

Git version: git version 2.43.0
Git vroot status: afc07f46
.git/config present: true

CC version: cc (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0
emcc version: N/A
thirdparty/tcc status: thirdparty-linux-amd64 0134e9b9

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

Huly®: V_0.6-21635

@plicease plicease added the Bug This tag is applied to issues which reports bugs. label Dec 18, 2024
@plicease
Copy link
Author

Okay building generating the C and then building that with -lgc and -ltcc seems to have worked:

✅ plicease-clean% gcc -fPIC -I/home/ollisg/opt/v/git/thirdparty/libgc/include  -shared -L/home/ollisg/opt/v/git/thirdparty/tcc/lib/ libfoo.c -L/home/ollisg/opt/v/git/thirdparty/tcc/lib/ -o libfoo.so -lgc -ltcc
✅ plicease-clean% perl -MFFI::Platypus -E '$ffi = FFI::Platypus->new( api => 2, lib => "./libfoo.so"); say $ffi->function(libfoo__square => ["int"] => "int")->call(2)'
4

so I am guessing something went wrong in the link step.

@spytheman
Copy link
Member

You can pass -d no_backtrace too, when compiling with tcc,
or you can pass -cc gcc, and compile your .so that way, i.e. v -shared -cc gcc . .

@spytheman
Copy link
Member

Turning off the GC can be done with -gc none .

@spytheman
Copy link
Member

image

@spytheman
Copy link
Member

After running cpan i FFI::Platypus:

#0 16:51:44 ^ /v/oo/libfoo>perl -MFFI::Platypus -E '$ffi = FFI::Platypus->new( api => 2, lib => "./libfoo.so"); say $ffi->function(libfoo__square => ["int"] => "int")->call(2)'
4

@plicease
Copy link
Author

@spytheman Thanks that is super helpful! I think it should work without having to know how to use those flags but will leave it up to other folks to decide if this needs addressing further.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This tag is applied to issues which reports bugs.
Projects
None yet
Development

No branches or pull requests

2 participants