C 链接错误(使用 tcc)

Mac*_*eja 4 c linker-errors tcc undefined-symbol lib

我正在尝试从名为 的小型 cc (tcc-0.9.26-win64-bin.zip)运行该示例libtcc_test.c

我已将libtcc.hfrom libtccintoinclude复制libtcc.deflib.
然后我运行tcc ./examples/libtcc_test.c并收到链接错误:/

tcc: error: undefined symbol 'tcc_new'
tcc: error: undefined symbol 'tcc_set_lib_path'
tcc: error: undefined symbol 'tcc_set_output_type'
tcc: error: undefined symbol 'tcc_compile_string'
tcc: error: undefined symbol 'tcc_add_symbol'
tcc: error: undefined symbol 'tcc_relocate'
tcc: error: undefined symbol 'tcc_get_symbol'
tcc: error: undefined symbol 'tcc_delete'
Run Code Online (Sandbox Code Playgroud)

我缺少什么?


更多信息:

P:\cpp\tcc>tcc ./examples/libtcc_test.c -vv
tcc version 0.9.26 (i386 Win32)
-> ./examples/libtcc_test.c
-> p:/cpp/tcc/include/stdlib.h
->  p:/cpp/tcc/include/_mingw.h
->   p:/cpp/tcc/include/stddef.h
->   p:/cpp/tcc/include/stdarg.h
->  p:/cpp/tcc/include/limits.h
->  p:/cpp/tcc/include/sec_api/stdlib_s.h
->   p:/cpp/tcc/include/stdlib.h
->  p:/cpp/tcc/include/malloc.h
-> p:/cpp/tcc/include/stdio.h
->  p:/cpp/tcc/include/vadefs.h
->  p:/cpp/tcc/include/sec_api/stdio_s.h
->   p:/cpp/tcc/include/stdio.h
-> p:/cpp/tcc/include/string.h
->  p:/cpp/tcc/include/sec_api/string_s.h
->   p:/cpp/tcc/include/string.h
-> p:/cpp/tcc/include/libtcc.h
-> p:/cpp/tcc/lib/libtcc1.a
-> p:/cpp/tcc/lib/msvcrt.def
-> p:/cpp/tcc/lib/kernel32.def
tcc: error: undefined symbol 'tcc_new'
tcc: error: undefined symbol 'tcc_set_lib_path'
tcc: error: undefined symbol 'tcc_set_output_type'
tcc: error: undefined symbol 'tcc_compile_string'
tcc: error: undefined symbol 'tcc_add_symbol'
tcc: error: undefined symbol 'tcc_relocate'
tcc: error: undefined symbol 'tcc_get_symbol'
tcc: error: undefined symbol 'tcc_delete'
Run Code Online (Sandbox Code Playgroud)

PSk*_*cik 5

要链接到库中,您需要在所有文件或文件-l${library_basename}后面添加一个标志。如果库被命名为或(在 Windows 上可能是或),则需要添加.colibtcc.alibtcc.sotcc.dlllibtcc.dll-ltcc

tcc  ./examples/libtcc_test.c  -ltcc
Run Code Online (Sandbox Code Playgroud)

您可能还需要添加一个-L标志来添加搜索路径,以防您要链接的库不是系统的标准库目录:

tcc -L . ./examples/libtcc_test.c -ltcc
#also look for libtcc.so or libtcc.a in the current directory (.)
Run Code Online (Sandbox Code Playgroud)

tinycc repo 中的libtcc_test.cfrom还需要库(动态加载的标准库)来构建:test/libtcc_test.cdl

tcc -L .  tests/libtcc_test.c  -ltcc -ldl #worked 
Run Code Online (Sandbox Code Playgroud)

(它抱怨 undefined dlopendlclose、 和dlsym已知来自libdl)。