Skip to content

special_math.cpp: Statically linked library contains and depends on Boost symbols #362

Open

Description

Description
Boost symbols embedded in static library can conflict with user code and/or custom boost installations.

Example 1
In this example one can override behaviour of standard library function using custom boost symbol.

PS G:\dev\test> type .\test.cpp
#include <cmath>
#include <cstdio>

namespace boost::math {
template<typename T>
T zeta(T)
{
    return 0;
}
}

int main()
{
    auto a = boost::math::zeta(10.0);
    auto b = std::riemann_zeta(10.0);
    std::printf("boost = %f\nstd   = %f\n", a, b);
    return 0;
}
PS G:\dev\test> cl -EHsc -std:c++17 -MDd -nologo .\test.cpp
test.cpp
PS G:\dev\test> .\test.exe
boost = 0.000000
std   = 1.000995
PS G:\dev\test> cl -EHsc -std:c++17 -MTd -nologo .\test.cpp
test.cpp
PS G:\dev\test> .\test.exe
boost = 0.000000
std   = 0.000000
PS G:\dev\test> cl
Microsoft (R) C/C++ Optimizing Compiler Version 19.24.28314 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

Playing with different compiler flags I also got following output once:

boost = 1.000995
std   = 1.000995

but I forgot exact combination and wasn't able to reproduce it later.

Example 2
In this example one can link to boost symbols contained in STL.

PS G:\dev\test> type .\test.cpp
#include <cmath>
#include <cstdio>

namespace boost::math {
template<typename T>
T zeta(T);

extern template double zeta<double>(double);
}

int main()
{
    auto a = boost::math::zeta(10.0);
    std::printf("boost = %f\n", a);
    return 0;
}
PS G:\dev\test> cl -EHsc -std:c++17 -MT -nologo .\test.cpp
test.cpp
PS G:\dev\test> .\test.exe
boost = 1.000995
PS G:\dev\test> cl -EHsc -std:c++17 -MD -nologo .\test.cpp
test.cpp
test.obj : error LNK2019: unresolved external symbol "double __cdecl boost::math::zeta<double>(double)" (??$zeta@N@math@boost@@YANN@Z) referenced in function main
test.exe : fatal error LNK1120: 1 unresolved externals

Expected behavior
STL shouldn't expose any third-party symbols to user code in order to avoid conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions