【问题标题】:when clang compiling to target wasm, it return error for function declaration当clang编译到目标wasm时,函数声明返回错误
【发布时间】:2020-08-03 16:25:34
【问题描述】:

我已尝试从 c 程序生成wasm 文件。

clang test.c --target=wasm32-unknown-unknown-wasm -nostartfiles -nostdlib -Wl,--no-entry -Wl,--export-all -o test.wasm

test.c文件内容如下

extern void __VERIFIER_error(void);
extern void __VERIFIER_assume(int);
void __VERIFIER_assert(int cond) {
  if (!(cond)) {
      ERROR: __VERIFIER_error();
  }
  return;
}
int __VERIFIER_nondet_int();

int test() {
    int x = 1;
    int y = 0;
    while (y < 1000 && __VERIFIER_nondet_int()) {
        x = x + y;
        y = y + 1;
    }
    __VERIFIER_assert(x >= y);
    return 0;
}

遇到以下错误信息:

clang test.c  --target=wasm32-unknown-unknown-wasm -nostartfiles -nostdlib -Wl,--no-entry -Wl,--export-all -o test.wasm
wasm-ld: error: /tmp/test-e520f3.o: undefined symbol: __VERIFIER_error
wasm-ld: error: /tmp/test-e520f3.o: undefined symbol: __VERIFIER_nondet_int
clang-10: error: linker command failed with exit code 1 (use -v to see invocation)

我已将文件内容更新如下:

extern "C" void __VERIFIER_error(void);
extern "C" void __VERIFIER_assume(int);
extern "C" void __VERIFIER_assert(int cond) {
  if (!(cond)) {
      ERROR: __VERIFIER_error();
  }
  return;
}
extern "C" int __VERIFIER_nondet_int();

int test() {
    int x = 1;
    int y = 0;
    while (y < 1000 && __VERIFIER_nondet_int()) {
        x = x + y;
        y = y + 1;
    }
    __VERIFIER_assert(x >= y);
    return 0;
}

遇到以下错误信息,如何摆脱此类错误。任何人都可以在这方面指导。

test/test.c:1:8: error: expected identifier or '('
extern "C" void __VERIFIER_error(void);
       ^
test/test.c:2:8: error: expected identifier or '('
extern "C" void __VERIFIER_assume(int);
       ^
test/test.c:3:8: error: expected identifier or '('
extern "C" void __VERIFIER_assert(int cond) {
       ^
test/test.c:9:8: error: expected identifier or '('
extern "C" int __VERIFIER_nondet_int();
       ^
test/test.c:13:24: warning: implicit declaration of function '__VERIFIER_nondet_int' is invalid in C99 [-Wimplicit-function-declaration]
    while (y < 1000 && __VERIFIER_nondet_int()) {
                       ^
test/test.c:17:5: warning: implicit declaration of function '__VERIFIER_assert' is invalid in C99 [-Wimplicit-function-declaration]
    __VERIFIER_assert(x >= y);
    ^
2 warnings and 4 errors generated.

【问题讨论】:

  • 为什么是这些extern "C" ?例如,您在 C 中,而不是在 C++ 中。这些函数的名称以 2 '_' 开头的事实非常可疑。这些函数假设来自哪里,它们是在库中定义的吗?如果是,可能你有关联的标题包括
  • 是的,我已经在文件 extest.h 中定义了这些函数。您能否指导我如何使用选项链接文件...我已经尝试过“clang test.c --target=wasm32-unknown-unknown-wasm --sysroot =/../inlcude/ -nostartfiles -nostdlib -Wl ,--no-entry -Wl,--export-all -o test.wasm”。我也试过选项--include-directory
  • I have defined those functions in file extest.h 我想你是declared它们,而不是defined,声明只是没有正文的签名,而定义也有正文. extern void __VERIFIER_error(void); 和 `void __VERIFIER_error(void);` 是声明,void __VERIFIER_error(void) { ... } 是定义,就像你定义的 test

标签: c linker clang webassembly


【解决方案1】:

你有两个没有定义的函数。您不链接任何库或其他目标文件。所以链接器没有找到它们并发出错误消息。

【讨论】:

  • 您能否指导我如何使用任何选项链接头文件。假设我所有的头文件都存在于目录/../include 中。我在命令中尝试过 --sysroot =/../include 和 --include-directory =/../include 选项
  • 1. “clang test.c --target=wasm32-unknown-unknown-wasm --sysroot =/../inlcude/-nostartfiles -nostdlib -Wl,--no-entry -Wl,--export-all -o 测试。黄蜂”
  • 2. "clang test.c --target=wasm32-unknown-unknown-wasm --include-directory =/../inlcude/ -nostartfiles -nostdlib -Wl,--no-entry -Wl,--export-all -o test.wasm"
  • 你不能链接头文件。您只能链接目标文件。
  • 您需要将目标文件与这些函数的“主体”链接起来。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-09
  • 1970-01-01
  • 2018-03-12
  • 2020-04-27
  • 1970-01-01
  • 2020-06-06
相关资源
最近更新 更多