Skip to content

Suppress MSVC C4996 warnings in implementation section#539

Open
shyraptor wants to merge 1 commit intoraysan5:masterfrom
shyraptor:fix/msvc-c4996-pragma-warning
Open

Suppress MSVC C4996 warnings in implementation section#539
shyraptor wants to merge 1 commit intoraysan5:masterfrom
shyraptor:fix/msvc-c4996-pragma-warning

Conversation

@shyraptor
Copy link
Copy Markdown

When raygui.h is included after raylib.h (the common case), MSVC emits
C4996 warnings on every fopen/sscanf call inside the implementation section:

raygui.h(4376): warning C4996: 'fopen': This function or variable may be unsafe.
raygui.h(4397): warning C4996: 'sscanf': This function or variable may be unsafe.
raygui.h(4408): warning C4996: 'sscanf': This function or variable may be unsafe.

raygui already defines _CRT_SECURE_NO_WARNINGS (line 361) to prevent this,
but it has no effect when stdio.h was already pulled in by an earlier header.
MSVC's CRT decides at first include whether to mark these functions deprecated;
a later define cannot undo that.

This PR adds a #pragma warning(push/disable/pop) block around the
RAYGUI_IMPLEMENTATION section, which suppresses C4996 at the call site
regardless of include order. The suppression is scoped — it does not
leak into user code.

Tested on MSVC 19.44 (Visual Studio 2022 Build Tools).

The existing _CRT_SECURE_NO_WARNINGS define (line 361) does not work
when CRT headers are already included before raygui.h — which is the
common case when using raylib.h, since it pulls in <stdio.h> first.

MSVC's CRT headers check _CRT_SECURE_NO_WARNINGS once at first include
to decide whether to add __declspec(deprecated) to functions like fopen
and sscanf. Once the include guard fires, the decision is permanent.
Defining _CRT_SECURE_NO_WARNINGS later has no effect.

Use #pragma warning(push/disable/pop) scoped to the RAYGUI_IMPLEMENTATION
section to suppress C4996 at the call sites regardless of include order.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant