emscripten-core / emscripten-core/emscripten

SIDE_MODULE=2 deletes functions of exported class that are used and called

Open
#16,762 6 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

Steps to reproduce:
// 1.h
```
class __attribute__((visibility("default"))) A
{
public:
int foo();
};
```
// 1.cpp
```
#include "1.h"
int A::foo() {
return 123;
}
```
emcc 1.cpp -fvisibility=hidden -sSIDE_MODULE=2 -o out/1.wasm
// main.cpp
```
#include "1.h"
#include
int main() {
A a;
printf("%d\n", a.foo());
return 0;
}
```
emcc main.cpp out/1.wasm -sMAIN_MODULE=2 -o out/main.html

This results in linking error complaining about A::Foo() not being defined. Yes I know that EMSCRIPTEN_KEEPALIVE on function declaration will fix this, but we are currently porting very large(8.5 million LOC) project that has lots of exported classes and marking every function declaration with EMSCRIPTEN_KEEPALIVE seems insane. Why does dead code elimination kill member functions of an exported class to begin with? The usual C++ practice is to mark all symbols of dynamic library hidden by default, then explicitly mark classes we want to export with special compiler attributes. We need dead code elimination to reduce size of dynamic libraries, but deleting exports is very unexpected. Is there a way to stop dead code elimination from removing exports?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.