Skip to content

clang-cl rejects "extern const __declspec(selectany)" variable as having "non-external linkage" when variable definition includes a struct definition #110208

@dunhor

Description

@dunhor

Wow that title is a mouthful. Here's a concrete example:

extern const __declspec(selectany) struct { int x; } foo = { 42 };

clang-cl gives the error:

> clang-cl main.cpp
main.cpp(2,25): error: 'selectany' can only be applied to data items with external linkage
    2 | extern const __declspec(selectany) struct { int x; } foo = { 42 };
      |                         ^
1 error generated.

> clang -v
clang version 19.1.0
Target: x86_64-pc-windows-msvc
Thread model: posix
InstalledDir: C:\Program Files\LLVM\bin

Of course, if you give the struct its own definition, the code compiles fine:

struct bar { int x; };
extern const __declspec(selectany) bar foo = { 42 };

MSVC compiles both without issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzerdiverges-from:msvcDoes the clang frontend diverge from msvc on this issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions