ld.exe 在将 c 文件传递给 swipl-ld 时给出“对‘PL_initialise’的未定义引用”(以及 SWI-Prolog.h 中声明的其他函数)错误。
我在 Windows 11 上使用 SWI-Prolog。
我使用 MinGW 在我的计算机上安装了 gcc,阅读 this page 并尝试做同样的事情。 我制作了下面的两个文件。
计算器
#include <stdio.h>
#include <string.h>
#include <SWI-Prolog.h>
#define MAXLINE 1024
int
main(int argc, char** argv)
{
char expression[MAXLINE];
char* e = expression;
char* program = argv[0];
char* plav[2];
int n;
/* combine all the arguments in a single string */
for (n = 1; n < argc; n++)
{
if (n != 1)
*e++ = ' ';
strcpy(e, argv[n]);
e += strlen(e);
}
/* make the argument vector for Prolog */
plav[0] = program;
plav[1] = NULL;
/* initialise Prolog */
if (!PL_initialise(1, plav))
PL_halt(1);
/* Lookup calc/1 and make the arguments and call */
{ predicate_t pred = PL_predicate("calc", 1, "user");
term_t h0 = PL_new_term_refs(1);
int rval;
PL_put_atom_chars(h0, expression);
rval = PL_call_predicate(NULL, PL_Q_NORMAL, pred, h0);
PL_halt(rval ? 0 : 1);
}
return 0;
calc.pl
calc(Atom) :-
term_to_atom(Expr, Atom),
A is Expr,
write(A),
nl.
然后运行命令
swipl-ld -goal true -o test calc.c calc.pl
在这里,我希望得到一个 test.exe 文件,但我得到了错误,
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: calc.obj:calc.c:(.text+0xc8): undefined reference to `PL_initialise'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: calc.obj:calc.c:(.text+0xd8): undefined reference to `PL_halt'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: calc.obj:calc.c:(.text+0xf4): undefined reference to `PL_predicate'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: calc.obj:calc.c:(.text+0x107): undefined reference to `PL_new_term_refs'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: calc.obj:calc.c:(.text+0x125): undefined reference to `PL_put_atom_chars'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: calc.obj:calc.c:(.text+0x14f): undefined reference to `PL_call_predicate'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: calc.obj:calc.c:(.text+0x16c): undefined reference to `PL_halt'
collect2.exe: error: ld returned 1 exit status
gcc.exe returned code 1
*** swipl-ld exit status 1