在我的C++项目中,我使用tcclib实时编译和运行C代码。
我正在使用这里提供的二进制文件,https://bellard.org/tcc/
然后打开一个vs2019开发人员提示符,并运行这两个命令。
lib /def:libtcc\libtcc.def /out:libtcc.lib
cl /MD examples/libtcc_test.c -I libtcc libtcc.lib
我的代码构建得很好,我正在使用这段代码。这段代码类似于tcclib示例中的代码,即:https://repo.or.cz/tinycc.git/blob/HEAD:/tests/libtcc_test.c (这是另一种回购,但代码相同)。
我运行的代码是这个。这是在一个extern "C" {}
中。
int tcc_stuff(int argc, const char** argv) {
TCCState* s;
int i;
int (*func)(int);
s = tcc_new();
if (!s) {
fprintf(stderr, "Could not create tcc state\n");
exit(1);
}
/* if tcclib.h and libtcc1.a are not installed, where can we find them */
for (i = 1; i < argc; ++i) {
const char* a = argv[i];
if (a[0] == '-') {
if (a[1] == 'B')
tcc_set_lib_path(s, a + 2);
else if (a[1] == 'I')
tcc_add_include_path(s, a + 2);
else if (a[1] == 'L')
tcc_add_library_path(s, a + 2);
}
}
/* MUST BE CALLED before any compilation */
tcc_set_output_type(s, TCC_OUTPUT_MEMORY);
{
const char* other_file = ReadFile2(argv[1]);
if (other_file == NULL)
{
printf("invalid filename %s\n", argv[1]);
return 1;
}
if (tcc_compile_string(s, other_file) == -1)
return 1;
}
/* as a test, we add symbols that the compiled program can use.
You may also open a dll with tcc_add_dll() and use symbols from that */
tcc_add_symbol(s, "add", add);
tcc_add_symbol(s, "hello", hello);
/* relocate the code */
if (tcc_relocate(s, TCC_RELOCATE_AUTO) < 0)
return 1;
/* get entry symbol */
func = (int(*)(int))tcc_get_symbol(s, "foo");
if (!func)
return 1;
/* run the code */
msg(func(32));
//msg(func2(4));
/* delete the state */
tcc_delete(s);
return 0;
}
运行我的代码时,TCC出现了错误。
tcc: error: library 'libtcc1-32.a' not found
我通过将这个文件放在.exe旁边的lib/目录中来修正它
我还复制了包含/文件夹,以包含stdio.h等。
我的问题是:为什么它需要这个文件在lib/文件夹中而不是提供的tcclib.dll文件?是否有可能像stdio.h那样“运送”某些标头?
发布于 2022-07-18 16:43:46
这个问题没有答案,只有360次浏览,所以我想我应该回答。
库不一定需要在那个文件夹中。要引用作者的命令行文档(仍然适用于库),
-Ldir
为-l
选项指定一个附加的静态库路径。默认库路径是/usr/local/lib
、/usr/lib
和/lib
。
我推断您的程序是libtcc_test.c的一个修改后的main() &将其修正到了功能的程度。然后我使用VS2022来追溯您的步骤,将.a
文件放入与我的新tests_libtcc_test.exe
相同的文件夹中,然后运行以下命令:
tests_libtcc_test c:/lang/tcc/examples/fib.c -Ic:/lang/tcc/include -L.
如果我没有-L
,库问题就会出现,如果我至少包括"."
,就会消失。
当然,您可以将include文件夹放到您的可再发行版中,并在默认情况下从代码中包含它。
因为tcc只是同一个编译器的另一个接口,所以它需要与tcc.exe构建可执行文件相同的东西;在这种情况下,它需要相同的库。
https://stackoverflow.com/questions/60131423
复制相似问题