winsock2 redefinition Merging issues - c++

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!

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.

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.

Using GLEE w/ includes?

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.

JRTPLIB/header include problem

I'm having some problems with JRTPLIB c++ win32 version, compiling in visual studio2010.(http://research.edm.uhasselt.be/~jori/page/index.php?n=CS.Jrtplib). I've emailed the author but have yet to received a reply. The problem I am experiencing is this:
error C1083: Cannot open include file: 'rtpconfig_unix.h': No such file or directory c:\users\johan-bar\desktop\developer tools\3rd party software\jrtplib-3.8.1\src\rtpconfig.h
The two .h files I have are these:
MAIN.h:
enter code here
#include <WinSock2.h>
#include <Windows.h>
#include <WindowsX.h>
#include <stdlib.h>
#include <string>
#include <Richedit.h>
#include "jrtlibtest.h"
#include "resource.h"
jrtlibtest.h:
#include "rtpsession.h"
So I reason that I need to #include windows.h in jrtlibtest.h for it to recognise WIN32 to be defined (so it does not include unix .h files) but that in turn gives me about 100 redifinition errors.
I am unsure how to solve this problem and I can't find any information on the library homepage itself or on the internet. Has anyone else encountered this problem?
Cheers
I have not seen JRTPLIB c++ lib, but based on information that you provided ('rtpconfig_unix.h'can not be opened), it seems that it is taking default file for unix port? Look for something like a config file in the JRTPLIB folder and run it (./config on cygwin or something). That should generate the windows config files that you would be able to #include in your code.
Good luck!!
EDIT:
The fact that you are getting the error:
error C1083: Cannot open include file: 'rtpconfig_unix.h':
means: in your rtpconfig.h, the WIN32 macro is not enabled:
#ifndef RTPCONFIG_H
#define RTPCONFIG_H
#if (defined(WIN32) || defined(_WIN32_WCE))
#include "rtpconfig_win.h"
#else
#include "rtpconfig_unix.h"
#endif // WIN32
//#define RTPDEBUG
#endif // RTPCONFIG_H
And thatÅ› why it says it cant open rtpconfig_unix.h file.
Did you try #defining win32 macro in rtpconfig.h directly? (or do it in your project settings).
Include ws2_32.lib in your project. Had the same problem.
(And remove if you already include it, wsock32.lib and winsock.h header file to avoid colissions)
What are the redefinition errors?
If they are from winsock, removing winsock2.h from your includes might help.

refactoring my code. My headers (Header Guard Issues)

I had a post similar to this awhile ago based on a error I was getting. I was able to fix it but since then I been having trouble doing things because headers keep blocking other headers from using code. Honestly, these headers are confusing me and if anyone has any resources that will address these types of issues, that will be helpful.
What I essentially want to do is be able to have rModel.h be included inside RenderEngine.h. every time I add rModel.h to RenderEngine.h, rModel.h is no longer able to use RenderEngine.h. (rModel.h has a #include of RenderEngine.h as well).
So in a nutshell, RenderEngine and rModel need to use each others functionalities. On top of all this confusion, the Main.cpp needs to use RenderEngine.
stdafx.h
#include "targetver.h"
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
// Windows Header Files:
#include <windows.h>
// C RunTime Header Files
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>
#include "resource.h"
main.cpp
#include "stdafx.h"
#include "RenderEngine.h"
#include "rModel.h"
// Global Variables:
RenderEngine go;
rModel *g_pModel;
...code...........
rModel.h
#ifndef _MODEL_H
#define _MODEL_H
#include "stdafx.h"
#include <vector>
#include <string>
#include "rTri.h"
#include "RenderEngine.h"
........Code
RenderEngine.h
#pragma once
#include "stdafx.h"
#include "d3d10.h"
#include "d3dx10.h"
#include "dinput.h"
#include "rModel.h"
.......Code......
As I wrote in my previous answer on this question, google about Forward declaration in C++.
This may solve your problems, but, again, circular header dependencies indicate poor application design.
At least if I understand your question correctly, you have a little bit of a problem. You basically need to structure your headers so the inclusions form a directed acyclic graph (emphasis on acyclic).
What you may have to do is break your "renderengine.h" into two pieces, one of which just contains forward declarations, and the other of which contains the rest of your current contents. You'll then include that "forward declarations" header into "rmodel.h", and include "rmodel.h" into "renderengine.h".
While there are times that this is unavoidable, such a circular dependency often points to a problem with how you've organized your modules. It's entirely possible that what you currently have as renderengine.h and rmodule.h should be a single header, or perhaps multiple headers but broken along different lines.