-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Raylib doesn't build with windows.h #1217
Comments
Use CMake to generate a MSVS project file. I have just confirmed it compiles fine on Windows. You'll tend to get those errors if you attempt to compile the raylib C files inside a separate C++ project. The only other work around would be the use of namespaces, but that won't work with raylib given it's a C library, not a C++ library. |
Hey @scottmudge , it compiles fine on windows by itself, the problem is when you're trying to compile it with the windows.h header for native window functions. |
@ZelCloud you have three options to avoid conflicts:
#if defined(_WIN32)
// To avoid conflicting windows.h symbols with raylib, some flags are defined
// WARNING: Those flags avoid inclusion of some Win32 headers that could be required
// by user at some point and won't be included...
//-------------------------------------------------------------------------------------
// If defined, the following flags inhibit definition of the indicated items.
#define NOGDICAPMASKS // CC_*, LC_*, PC_*, CP_*, TC_*, RC_
#define NOVIRTUALKEYCODES // VK_*
#define NOWINMESSAGES // WM_*, EM_*, LB_*, CB_*
#define NOWINSTYLES // WS_*, CS_*, ES_*, LBS_*, SBS_*, CBS_*
#define NOSYSMETRICS // SM_*
#define NOMENUS // MF_*
#define NOICONS // IDI_*
#define NOKEYSTATES // MK_*
#define NOSYSCOMMANDS // SC_*
#define NORASTEROPS // Binary and Tertiary raster ops
#define NOSHOWWINDOW // SW_*
#define OEMRESOURCE // OEM Resource values
#define NOATOM // Atom Manager routines
#define NOCLIPBOARD // Clipboard routines
#define NOCOLOR // Screen colors
#define NOCTLMGR // Control and Dialog routines
#define NODRAWTEXT // DrawText() and DT_*
#define NOGDI // All GDI defines and routines
#define NOKERNEL // All KERNEL defines and routines
#define NOUSER // All USER defines and routines
//#define NONLS // All NLS defines and routines
#define NOMB // MB_* and MessageBox()
#define NOMEMMGR // GMEM_*, LMEM_*, GHND, LHND, associated routines
#define NOMETAFILE // typedef METAFILEPICT
#define NOMINMAX // Macros min(a,b) and max(a,b)
#define NOMSG // typedef MSG and associated routines
#define NOOPENFILE // OpenFile(), OemToAnsi, AnsiToOem, and OF_*
#define NOSCROLL // SB_* and scrolling routines
#define NOSERVICE // All Service Controller routines, SERVICE_ equates, etc.
#define NOSOUND // Sound driver routines
#define NOTEXTMETRIC // typedef TEXTMETRIC and associated routines
#define NOWH // SetWindowsHook and WH_*
#define NOWINOFFSETS // GWL_*, GCL_*, associated routines
#define NOCOMM // COMM driver routines
#define NOKANJI // Kanji support stuff.
#define NOHELP // Help engine interface.
#define NOPROFILER // Profiler interface.
#define NODEFERWINDOWPOS // DeferWindowPos routines
#define NOMCX // Modem Configuration Extensions
// Type required before windows.h inclusion
typedef struct tagMSG *LPMSG;
#include <windows.h>
// Type required by some unused function...
typedef struct tagBITMAPINFOHEADER {
DWORD biSize;
LONG biWidth;
LONG biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
} BITMAPINFOHEADER, *PBITMAPINFOHEADER;
#include <objbase.h>
#include <mmreg.h>
#include <mmsystem.h>
// Some required types defined for MSVC/TinyC compiler
#if defined(_MSC_VER) || defined(__TINYC__)
#include "propidl.h"
#endif
#endif |
I've found a better solution: #define WIN32_LEAN_AND_MEAN
#if defined(_WIN32)
#define WIN32
#endif
#if defined(_WIN64)
#define WIN64
#define _AMD64_
#undef _X86_
#else
#undef _AMD64_
#define _X86_
#endif
#include <minwindef.h>
#include <stdio.h>
#include <stdlib.h> and change int main() to int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, char* pCmdLine, int nCmdShow) |
Hi, |
@Availleight See RobLoach/raylib-cpp#45 assuming you are using C++. |
For me this was enough to compile raylib with #if defined(_WIN32)
#define NOGDI // All GDI defines and routines
#define NOUSER // All USER defines and routines
#endif
#include <Windows.h> // or any library that uses Windows.h
#if defined(_WIN32) // raylib uses these names as function parameters
#undef near
#undef far
#endif |
That is quite interesting, but is that for command line EXEs, or Windows GUIs? |
I can compile it with Also note: if you want to include
Because raylib uses these two names as function parameters. (I've updated my answer above) |
So much for my superior knowledge. I guess after 45+ years as a programmer I've already peaked... 🤣 Thanks for keeping on top of this and keeping it SIMPLE. |
Heading for my 65th anniversary as a programmer, I definitely have peaked :). I am befuddled that |
thank you, it works and help me so much |
@Staxcelrom thank you very much for your feedback! You are welcome! 👍🙂 |
I had similar issue with my C++ project, and the best way I found was to just rename the functions. So for example I had issues with |
Unfortunately I also ended up renaming the raylib functions and types in my own fork 😕 It is only one example. The complete list of renamings that I had to do in order to make raylib compile is as follows:
|
I also stumbled on this, so I wrote a Python script to automatically rename all public raylib declarations. It adds "Raylib" before function and type names, and "RAYLIB_" before define, macro, and enum member names. It is also able to update references in source files, in case you have existing dependencies using raylib. So, for example, The script is available here. So far it's tested with raylib 4.5.0 and building with I'd really like to suggest that the next major version upgrade (4.x -> 5.x) should include adding an "rl" or "RL_" prefix to all public declarations, or something similar. |
@pineapplemachine The problem is that such a 5.x would break all current raylib applications. I suspect that outweighs the occasional need for I also suppose this is not in the spirit of BGI, when there was no Not my problem to solve, fortunately. I wish Ramon the best in coping with these kinds of dependency conflicts. |
I wish Ramon would just prefix all functions and variables with rl_ |
no plans for prefixes, sorry. |
@orcmid it's simply impossible
WIN32_LEAN_AND_MEAN does help, but not by very much:
it's less than 0.3 seconds difference coping with the "dependency conflicts" is trivial, and many strategies for doing it have been mentioned https://nullprogram.com/blog/2023/05/31/ the separate translation including windows.h is the simplest option for people who don't want to bother with the slightly more tedious method described before |
@Peter0x44 Either way, trimming down to the sub-#includes that are of interest won't help with these Mexican Standoff name collisions at Link time. |
How can that be true if I don't include it? |
I'm talking about the timing cases you tested.
The linker-time ShowCursor collisions that are a trigger for this issue. |
|
I cannot include the header files of cef and reylib in a cpp file because the header file of cef depends on the definition in winuser but Raylib must define NOUSER to remove him. I don't understand why structures and functions are not prefixed with something like RL. Macros can be used to enable/disable prefixed versions to maintain compatibility. |
I found a simple solution: |
@joojn1122 I think to be fully correct, the declaration should also be |
Just wanted to share my case. cmake *** -DCUSTOMIZE_BUILD=ON -DSUPPORT_WINMM_HIGHRES_TIMER=OFF |
I have a similar issue when using Raylib with libuv. Was a macro taken into consideration? I see how this conflicts with @raysan5's vision and how the define approach would fragment the community but maybe it is worth to account for an easy path to production of Raylib-based projects? There is an opportunity for a good middle ground and potential evolution. Of course it's up to the core maintainer. |
Hi all, Just as an experiment, I've made a small POSIX script that let's one prefix every symbol/directive used by Raylib. It is available as Gist (link below) and should support all examples, and probably can be used for code that uses old Raylib names. I'm posting it here as I see a lot of people returning to this issue. Have in mind, you would have to vendor Raylib yourself, if you wish to use it. Don't use Also linking this issue with #3945 and #2490 as they relate to each other. |
See also: #1217 (comment) It looks like we took different approaches here, I'm actually unsure which would be the more robust. |
@pineapplemachine oh, I didn't notice your comment. We are using pretty similar approach (I think). |
Thank you @sleeptightAnsiC for letting me know about these issues raysan5/raylib#1217 (comment)
Oh dang, thank you for letting me know about the issues. I updated the script to fix the missed typedefs (Camera, etc.) and to work with 5.5:
Oh, I missed those typedefs. The script renames Camera3D to RaylibCamera3D but missed the
Looks like there were some updates to Raylib since I wrote the script that broke its assumptions about what files would contain Raylib declarations. That should also be fixed.
I think you can do this using the |
Raylib has a lot of same named functions to windows.h such as Rectangle, LoadImage, DrawText etc etc. Could we get some kind of workaround for this?
The text was updated successfully, but these errors were encountered: