programmer
programmer

Reputation: 813

Import External Symbol with Clang and WebAssembly

When compiling C(++) code that has extern function declarations to WebAssembly with Clang, it gives undefined reference errors rather than importing the symbol. What option will make the linker add those as imports to the WebAssembly module instead of trying to resolve them?

#ifdef __cplusplus
    extern "C" {
#endif

extern void externalFunction(void);

#ifdef __cplusplus
    }
#endif

int main() {
    externalFunction();
    return 0;
}

Upvotes: 5

Views: 2145

Answers (1)

sbc100
sbc100

Reputation: 3022

You can use the import_module and import_name clang attributes to mark the function as imported. e.g.:

__attribute__((import_module("env"), import_name("externalFunction"))) void externalFunction(void);

Or you can pass --allow-undefined to the linker. See https://lld.llvm.org/WebAssembly.html#imports.

Upvotes: 10

Related Questions