llvm / llvm/llvm-project

Clang++ ignores visibility attribute for namespace of forward-declared symbol

Open
#170,963 8 comments 0 reactions 0 assignees View on GitHub
clang
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

`clang++` seems to be ignoring `"default"` visibility of symbols that have their visibility specified by a namespace in a header. Originally, I was swapping my hobby graphics engine over to `-fvisibility=hidden`, so I started specifying the symbol visibility by setting the visibility for an entire namespace in the header they're exposed by. Setting the visibility for a whole namespace in a header seems to be ignored, but setting the visibility on every symbol in the header is respected. Setting the visibility of the namespace where the symbol is actually defined also works as expected, it's only namespace visibility for headers that's not working.

I stripped it down to a MWE, tested with clang 19, 21 and 22-git.

`main.cpp`:
```c++
#include "testSpace.hpp"

int main() {
return testSpace::run();
}
```

`testSpace.hpp`:
```c++
namespace __attribute__((visibility("default"))) testSpace {
int run();
}
```

`testSpace.cpp`:
```c++
#include "testSpace.hpp"

namespace testSpace {
int run() {
return 1;
}
}
```

Compiling with:
```console
comp=clang++
$comp -fvisibility=hidden -c testSpace.cpp
$comp -fvisibility=hidden -c main.cpp
$comp -shared -o testSpace.so testSpace.o -fvisibility=hidden
$comp -o main main.o testSpace.so
```

Gives me:
```
/usr/bin/ld: main.o: in function `main':
main.cpp:(.text+0x10): undefined reference to `testSpace::run()'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
```

When `comp` is `g++-15`, it compiles and runs as expected.

Also, `__has_cpp_attribute(visibility)` seems to evaluate to false, but `__has_cpp_attribute(gnu::visibility)` evaluates to true. Is this expected / related? I ran into this in the larger project, but it may well be expected since it's an extension.

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.