Skip to content

[Clang] Linker error at -O0 due to missing static const int definition, hidden at -O2 #139227

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
shzaiz opened this issue May 9, 2025 · 2 comments
Labels
clang:codegen IR generation bugs: mangling, exceptions, etc. diverges-from:msvc Does the clang frontend diverge from msvc on this issue

Comments

@shzaiz
Copy link

shzaiz commented May 9, 2025

In Clang 20.1.0, a class with static const int members that are declared but not defined results in a linker error at -O0, but compiles successfully at -O2.

This seems to be due to constant propagation at higher optimization levels, which removes the need for symbol resolution, masking the fact that a required definition is missing.

This behavior differs from MSVC, which compiles successfully in both optimization levels.

Code:

#include <map>
#include <vector>
using namespace std;
const pair<int, int> START = make_pair(0, 0);
class GridWorld {
public:
    // change `const' to `constexpr', it will successfully execute
    // or change compiler option with -O2
    static const int NORTH = 123456, SOUTH = 1, EAST = 2, WEST = 3;
    typedef pair<int, int> State;
    GridWorld(int x = 0, int y = 0) {}
    State state() { return make_pair(x, y); }
    map<State, double> benefit_matrix;
    void learn_benefit() {
        GridWorld temp(0, 0);
        for (auto action_state : temp.available_actions())
            ;
    }
private:
    int x, y;
    vector<pair<int, State>> available_actions() {
        vector<pair<int, State>> actions;
        if (y != 0)
            actions.push_back(make_pair(NORTH, make_pair(x, y - 1)));
        if (y != 4)
            actions.push_back(make_pair(SOUTH, make_pair(x, y + 1)));
        if (x != 4)
            actions.push_back(make_pair(EAST, make_pair(x + 1, y)));
        if (x != 0)
            actions.push_back(make_pair(WEST, make_pair(x - 1, y)));
        return actions;
    }
};

int main() {
    GridWorld env = GridWorld(START.first, START.second);
    env.learn_benefit();
    return 0;
} 

Reproduction Link: https://godbolt.org/z/dTdxWzMjP

@llvmbot llvmbot added the clang Clang issues not falling into any other category label May 9, 2025
@frederick-vs-ja
Copy link
Contributor

std::make_pair take arguments by reference (since C++11), so the static data members are ord-used and need to be defined. If you change const to constexpr, the static data members become inline variables and thus get defined (since C++17). Using unary operator+ is a possible workaround, which avoids odr-using.

In the original example, the variables are odr-used but not defined, and thus the program is ill-formed, no diagnostic required. Compilers can either accept or reject it.

MSVC seems to make these initialized const static data members effectively inline, which is a conforming extension, IIUC.

@frederick-vs-ja frederick-vs-ja added clang:codegen IR generation bugs: mangling, exceptions, etc. diverges-from:msvc Does the clang frontend diverge from msvc on this issue and removed clang Clang issues not falling into any other category labels May 9, 2025
@llvmbot
Copy link
Member

llvmbot commented May 9, 2025

@llvm/issue-subscribers-clang-codegen

Author: Guangwei Zhang (AUGPath) (shzaiz)

In `Clang 20.1.0`, a class with static const int members that are declared but not defined results in a linker error at `-O0`, but compiles successfully at `-O2`.

This seems to be due to constant propagation at higher optimization levels, which removes the need for symbol resolution, masking the fact that a required definition is missing.

This behavior differs from MSVC, which compiles successfully in both optimization levels.

Code:

#include &lt;map&gt;
#include &lt;vector&gt;
using namespace std;
const pair&lt;int, int&gt; START = make_pair(0, 0);
class GridWorld {
public:
    // change `const' to `constexpr', it will successfully execute
    // or change compiler option with -O2
    static const int NORTH = 123456, SOUTH = 1, EAST = 2, WEST = 3;
    typedef pair&lt;int, int&gt; State;
    GridWorld(int x = 0, int y = 0) {}
    State state() { return make_pair(x, y); }
    map&lt;State, double&gt; benefit_matrix;
    void learn_benefit() {
        GridWorld temp(0, 0);
        for (auto action_state : temp.available_actions())
            ;
    }
private:
    int x, y;
    vector&lt;pair&lt;int, State&gt;&gt; available_actions() {
        vector&lt;pair&lt;int, State&gt;&gt; actions;
        if (y != 0)
            actions.push_back(make_pair(NORTH, make_pair(x, y - 1)));
        if (y != 4)
            actions.push_back(make_pair(SOUTH, make_pair(x, y + 1)));
        if (x != 4)
            actions.push_back(make_pair(EAST, make_pair(x + 1, y)));
        if (x != 0)
            actions.push_back(make_pair(WEST, make_pair(x - 1, y)));
        return actions;
    }
};

int main() {
    GridWorld env = GridWorld(START.first, START.second);
    env.learn_benefit();
    return 0;
} 

Reproduction Link: https://godbolt.org/z/dTdxWzMjP

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:codegen IR generation bugs: mangling, exceptions, etc. diverges-from:msvc Does the clang frontend diverge from msvc on this issue
Projects
None yet
Development

No branches or pull requests

3 participants