C链接错误(使用tcc)

4
我正在尝试运行来自 tiny cc (tcc-0.9.26-win64-bin.zip) 的示例,名为libtcc_test.c
我已经从libtcc中复制了libtcc.hinclude并将libtcc.def复制到lib目录下。 然后我运行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'

我有什么遗漏吗?

更多信息:

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'

你需要链接库文件和包含文件来编译你的代码。确保使用必要的库和头文件进行链接和包含。 - danglingpointer
我对C语言非常陌生,但是有一个 #include "libtcc.h" 和 tcc ... -vv 的输出显示已经添加了,但我不知道如何“加载” .def 文件。 - Maciej Kozieja
@LethalProgrammer:“*...需要链接到...包含文件...*” 包含(头)文件不是在链接阶段使用的,而是只在编译期间使用的,当它涉及到链接时。 - alk
2个回答

5

要链接库,您需要在所有 c 文件或 o 文件后添加 -l $ {library_basename} 标志。 如果库的名称为 libtcc.a libtcc.so (在Windows上可能是 tcc.dll libtcc.dll ),则需要添加 -ltcc

tcc  ./examples/libtcc_test.c  -ltcc

如果您要链接的库不在系统的标准库目录中,您可能需要添加-L标志以添加搜索路径:

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

在tinycc代码库中的test/libtcc_test.c中的libtcc_test.c文件也需要dl库(用于动态加载的标准库)来构建:

tcc -L .  tests/libtcc_test.c  -ltcc -ldl #worked 

它抱怨了未定义的 dlopendlclosedlsym,这些函数来自已知的 libdl 库。


0
以下命令在Windows上运行成功:

cd your-tcc-directory
tcc -Ilibtcc -L. -ltcc examples/libtcc_test.c

你可能想添加-run以跳过生成exe文件并直接运行源代码。
我在Linux上尝试了它,但找不到libtcc.h。我猜以下内容会起作用(请注意-ltcc1而不是-ltcc):
tcc -I/path/to/libtcc.h/location -L/usr/lib/tcc/x86-64 -ltcc1 path/to/libtcc_test.c

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接