Skip to content

Visual Studio 2017 and inttypes.h #29

Open

Description

@kdekker

When I use latest win_flex.exe, the code produced, even in --wincompat mode contains lines like:

/* flex integer type definitions */

#ifndef FLEXINT_H
#define FLEXINT_H

/* C99 systems have <inttypes.h>. Non-C99 systems may or may not. */

#if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L

/* C99 says to define __STDC_LIMIT_MACROS before including stdint.h,
 * if you want the limit (max/min) macros for int types. 
 */
#ifndef __STDC_LIMIT_MACROS
#define __STDC_LIMIT_MACROS 1
#endif

#include <inttypes.h>
typedef int8_t flex_int8_t;
typedef uint8_t flex_uint8_t;
typedef int16_t flex_int16_t;
typedef uint16_t flex_uint16_t;
typedef int32_t flex_int32_t;
typedef uint32_t flex_uint32_t;
#else
typedef signed char flex_int8_t;
typedef short int flex_int16_t;
typedef int flex_int32_t;
typedef unsigned char flex_uint8_t; 
typedef unsigned short int flex_uint16_t;
typedef unsigned int flex_uint32_t;

/* Limits of integral types. */
#ifndef INT8_MIN
#define INT8_MIN       

Unfortunately, Visual Studio does not set STDC_VERSION but has inttypes.h. Because the #else part of the code is used, duplicate macros for e.g. INT8_MIN will exist (and raise an compiler warning or even error if C4005 is marked as error, as was done in our case). Example:

1> c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.15.26726\include\stdint.h(48): error C4005: 'INT8_MIN': macro redefinition
1> t:\obj.dbg.winx64\lib\sql\libsql_c\sqllex.cpp(57): note: see previous definition of 'INT8_MIN'
1> c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.15.26726\include\stdint.h(49): error C4005: 'INT16_MIN': macro redefinition
1> t:\obj.dbg.winx64\lib\sql\libsql_c\sqllex.cpp(60): note: see previous definition of 'INT16_MIN'

Can you please enhance this macro and add a check on _MSCVER? The inttypes.h file is (at least) in any Windows 10 SDK. The other approach is to include stdint.h, which is - at least - existent for Visual Studio 2017 and later.

Any other suggestions are welcome.

Activity

  1. lexxmark commented on Jan 10, 2019

    @lexxmark
    Owner

    I use the following lines to avoid warnings:
    %top{
    #include <stdint.h>
    }

    It seems it's already fixed in the original repo issue#307.
    Should be available in the next flex release 2.6.5

    Let's wait for the next release.

  2. kdekker commented on Jan 10, 2019

    @kdekker
    Author

    Many thanks. I now did a similar solution by not including any system header file. One of our own header files was included and that one included some system headers. The real cause is that Visual Studio does not adhere to C99 (or later) but still has its own stdint,h and/or inttypes.h.

    I was not aware of mentioned issue, but that indeed addresses the same. Sorry for creating this duplicate.

    However, the issue was created in mid 2018, but 2.6.5 seems not (yet) released. I took latest version, which was 2.6.4.

  3. Slayer95 commented on Mar 11, 2025

    @Slayer95

    Sorry. It looks like westes/flex#309 was never ported to winflexbison, as I am still seeing redefinition warnings. Why was this issue closed?

  4. self-assigned this
    on Sep 7, 2026
  5. lexxmark commented on Sep 7, 2026

    @lexxmark
    Owner

    Fixed on dev in a4305c7 (a4305c7), ships in 2.5.26.

    MSVC never defines STDC_VERSION in C++ mode, so the generated scanner always took the non-C99 branch and defined INT8_MIN plus eight more limit macros. , a few lines later, pulls in the SDK's <stdint.h>, which defines them again — 9 × C4005 in every C++ scanner.

    Upstream fixed this in 5574881 (westes/flex@5574881) (March 2018): the typedefs moved into a new src/flexint_shared.h, and the skeleton includes that instead of flexint.h, so generated scanners carry no limit macros at all. It landed ten months after the 2.6.4 tag and there has been no flex release since, which is why this port never got it. Backported as-is.

    Also added a test. flex.flexint_h_stdint_cxx compiles a generated C++ scanner with /we4005. The existing flex.flexint_h_stdint only covered a C scanner, where nothing includes <stdint.h> afterwards, so it passed either way and never caught this.

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

Metadata

Metadata

Assignees

Labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions