Using GLEE w/ includes? - opengl

How do you implement GLEE in your code so that it loads extensions used within included files?
For example, I have a windows build environment using cygwin and GCC, and am linking to the libraries for GLEE, GLUT, and opengl32.
The includes in my main file are ..
#include <windows.h>
#include <stdio.h>
#include <GL/GLee.h>
#include <GL/glut.h>
#include "SampleUtils.h"
#include "LineShaders.h"
SampleUtils.h declares methods that utilize OpenGL extensions, such as glCreateShader, which are implemented in SampleUtils.cpp. But when I attempt to build these files, the extensions are undeclared. I've tried a couple of different approaches.
Such as including in SampleUtils
#include <GL/gl.h>
#include <GL/glext.h>
which results in undeclared errors
#include <GL/GLee.h>
which throws a long list of errors that seem to relate to the fact that GLEE has already been included.
I can load the same extensions by implementing these methods in the main file, but can't get them to load from an included file. How is this dealt with?

Look in GLee.h:55
#ifdef WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <GL/gl.h>
#elif defined(__APPLE__) || defined(__APPLE_CC__)
#define GL_GLEXT_LEGACY
#include <OpenGL/gl.h>
#else // GLX
#define __glext_h_ /* prevent glext.h from being included */
#define __glxext_h_ /* prevent glxext.h from being included */
#define GLX_GLXEXT_PROTOTYPES
#include <GL/gl.h>
#include <GL/glx.h>
#endif
Wherever you #include "GLee.h" you don't have to #include gl.h, glext.h, or windows.h.

Related

Do not-precompiled headers use precompiled headers if they are Included or are they for .cpp files only?

Visual Studio 2022:
I want to Include Precompiled headers in my .cpp file but I don't know if it's worth it since I'll also need to include a non-precompiled header with almost the same headers that are in the precompiled header.
Will the non-precompiled header use the precompiled headers or will it generate the code again on each compilation?
CPP:
#pragma once
#include "Precompiled.h"
#include "No-Precompiled.h" // Basic Headers: Windows.h, Psapi.h
int main()
{
// Functions that I need from "No-Precompiled.h" but I can't Precompile it since changes in it are made on regular basis
}
No-Precompiled.h:
#pragma once
#include <windows.h>
#include <Psapi.h>
#include <d3d11.h>
class Template
{
public:
//Functions that need many same Headers.
}
Precompiled.h:
#pragma once
#include <windows.h>
#include <Psapi.h>
#include <d3d11.h>
#include <limits>
#include <complex>
#include <filesystem>
#include <iostream>
#include <string>
#include <chrono>
#include <thread>
#include <tchar.h>
#include <wincred.h>
#include <complex>
#include <math.h>
Should I just Precompile the headers that the .cpp file uses (which is not much) or is there a way to allow No-Precompiled headers to use the Precompiled headers?
Using pre-compiled headers doesn't change that much. In particular, header guards continue to work. The header guard for <windows.h> is also included in the pre-compiled state. Hence, when the compiler sees <windows.h> for the second time, it's immediately skipped.
In your case, the No-Precompiled.h header turns out to be pretty trivial, as all its headers have already been included. You're just compiling the Template.
I'd wonder a bit about the particular set of precompiled headers, though. PSapi and DirectX and IOstream? I can't really imagine a big program where you have many files using all of them. Note that <iostream> is really about std::cout, which doesn't make a lot of sense for DirectX programs.

winsock2 redefinition Merging issues

im trying to merge 2 projects together, one being a project in openGl and one being a windows console application, with some basic winsock networking implemented.
i started merging them by just inlcuding all the #include's that were in the original network program, but im getting just over 100 errors telling me that i've redefined all the structs and functions within ws2def.h and winsock.h
so these are the #includes at the to of my main.cpp
#include <windows.h>
#include <stdio.h>
#include <mmsystem.h>
#include <math.h>
#include <time.h>
#include <list>
#include <iostream>
#include <stdlib.h>
#include <string>
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#undef WIN32_LEAN_AND_MEAN
This can prevent windows.h from including the old winsock header file.
I came across the same problem while trying to integrate a previously working live555 server with an encoder application.
Each of the libraries I was using were including winsock and winsock2 to fulfill their tasks.
I first tried with the WIN32_LEAN_AND_MEAN define but that didn't work for me.
What ended working in my case (VS 2013) was to add _WINSOCKAPI_ in Configuration Properties -> Preprocessor -> Preprocessor Definitions.
What this does is to set, project-wide, windows.h not to include winsock when it's included.
Also, as my separate programs used winsock and winsock2 I unified the code by using winsock2 for both of them.
Hope this helps someone!

Does the order of include files matter when pclint executes?

I am finding some issue in the order the include headers are defined in the c / c++ files when i execute pclint.
Say the include order is ,
#include <sys/timerfd.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <stdarg.h>
#include <string.h>
and when i execute the pclint it gives error in say , FILE is un declared etc.
Later i changed the order of include to
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <stdarg.h>
#include <string.h>
#include <sys/timerfd.h>
i could see that many errors were gone . I am not able to figure out why is this behavior. I am using PC-lint for C/C++ (NT) Vers. 8.00w.
i have marked the include path as say, +libdir(D:\timesys\nitrogen6x\toolchain\include)
Thank You
Brijesh
Supposedly, the inclusion of header files does slightly matter, although it's rare to find such an occasion. Some include files use types, enums or something else that is only defined in another include file.
On Linux, for example, some functions require the inclusion of multiple headers. Some of the programs using those, fail if you include those headers in the wrong order.
Kinda like the final linking stage. You have to set the libs in the correct order, otherwise you may get unresolved dependencies.
If I find an example, i will post it here.
EDIT:
Found an example. Qt. Qt has the most absurdly complicated set of headers. If you include, for example, opengl.h before QtOpenGL.h, it gives you a compilation error, because inside the Qt headers it checks for the inclusion of opengl. For some reason, QtOpenGL.h must come first.

Makefile runs code that a compile-time #if says it shouldn't

I am trying to compile my code on Linux and created a simple make file.
In one of my headers, there is a compile-time conditional #include for a file that emulates Unix's dirent.h.
I put this include in a #if defined(_WIN32) || defined(_WIN64), and yet g++ happily includes the file, which results in endless errors.
The whole block looks like this:
#if defined(_WIN32) || defined(_WIN64)
#include "dirent.h"
#define strncasecmp _strnicmp
#else
#include <unistd.h>
#include <dirent.h>
#include <fcntl.h>
#include <sys/types.h>
#include <utime.h>
#endif
Is there any obvious reason for this to happen?
So, given the comments: Don't call your "compatibility header" "dirent.h" but something like "dirent_compat.h" or "dirent_win.h".
Alternatively, don't add current directory to the include path for system includes (you really shouldn't have <...> includes in current directory anyway)

#error gl.h included before glew.h

So I'm trying to move my OpenGL code from Main() into a specific class that will handle the 3D graphics only when necessary. Previously, the top of my main.cpp file looked like this:
#define GLEW_STATIC
#include <GL/glew.h>
#include <SFML/Graphics.hpp>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include "Game.h"
This worked well enough. What I tried to do was move all the OpenGL-relevant code into methods of the Game class. So I removed #define GLEW_STATIC and #include <GL/glew.h> from the above, and put them into Game.h, such that the top of Game.h now looks like this:
#define GLEW_STATIC
#include <GL/glew.h>
#include <SFML/Graphics.hpp>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include "Environment.h"
When I try to compile, I get the title error, #error gl.h included before glew.h.
Why is this happening, and how can I use OpenGL code (almost) entirely inside the functions of a specific class without this happening?
EDIT:
I have also tried this configuration in main.cpp, in an attempt to make sure that nothing includes SFML before GLEW.
#include <cstdlib>
#include <iostream>
#include <fstream>
#include "Game.h"
#include <SFML/Graphics.hpp>
Unfortunately, that doesn't help (there's nothing else being included that I'm not mentioning here).
Some other library is including gl.h. My guess would be SFML. Make sure you include GLEW first in Game.h and check the places where you include Game.h to make sure you're not including SFML or something else that includes gl.h before Game.h.
If you have something like:
#include <something_that_includes_gl.h>
#include "Game.h"
It will effectively include gl.h before GLEW.
I think I had this issue once, too. It's somehow caused by the way SFML (1.6?) includes the OpenGL stuff.
IIRC (been some time and I don't need GLEW anymore since switching to SFML2) it's due to SFML's Graphics.hpp including GLEW.h, too. Shouldn't happen due to include guards, but I think with some versions this might still happen. It might be possible for you to skip GLEW's header completely, as it's included through SFML anyway.
Which version of SFML are you running? 1.6, 2.0 or the 2.0 with the new API? Also, what's the reason for using GLEW? Something you're missing from SFML? Maybe it's something included in the latest version, so keeps you from having to include it too.