Skip to content

Unresolved symbol errors when compiling C++ project with just C math header included transitively #1304

@PatTheMav

Description

@PatTheMav

When compiling a C++ project that transitively only has math.h included, the SIMDe language detection fails to detect the correct environment and thus uses symbols that aren't actually available:

include\simde\simde-math.h(413,7): error C2039: 'isnan': is not a member of 'std'

Both cmath and math.h are wrappers around corecrt_math.h in Microsoft's standard library, which uses macros when used in a C language project and template functions when used in a C++ language project (official documentation):

When compiled as C++, the isnan macro isn't defined, and an isnan template function is defined instead. It behaves the same way as the macro, but returns a value of type bool instead of an integer.

This leads to the following chain of events:

  • Some transitive header might include math.h, which includes corecrt_math.h
  • Because the project language is C++ and __cplusplus_ is defined, the header will not set up a isnan macro, but a template function
  • Because cmath is not included, those templated math functions are not added to the std namespace
  • SIMDe will neither detect a isnan macro nor the libc++ header guards, but will detect __cplusplus and assume that cmath is included
  • Because of this the simde aliases will attempt to use std::isnan even though isnan only exists as a templated function

So the underlying assumption that the absence of a isnan macro also means that <cmath> was included and thus the associated math functions exist in the std namespace are not correct for Microsoft's standard library.

Note that this issue can occur if any header included by any other included header at any point just includes math.h, most possibly when using a pure C library in a C++ project that naively exposes its dependencies in its public headers.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions