I'm having a problem including both files.
Now, I know I need to either include Winsock2 first, then windows.h, or simply put:
#define WIN32_LEAN_AND_MEAN
but, I'm still having problems
I have a header file that is called XS.h which looks like this
#ifndef XS_H
#define XS_H
#include <winsock2.h>
#include <ws2tcpip.h>
#include <Windows.h>
#endif
and I'm including XS.h in the header Client.h.
Client.h include is looks like this :
#ifndef CLIENT_H
#define CLIENT_H
#include "XS.h"
XS.h is my only include in Client.h, yet I still get errors (and as you can see, Winsockis included before windows.h
I'm getting about 78 errors, here are some of them :
Error 90 error C3861: 'WSASetLastError': identifier not found c:\program files (x86)\windows kits\8.0\include\um\ws2tcpip.h 703
Error 61 error C2375: 'WSAStartup' : redefinition; different linkage c:\program files (x86)\windows kits\8.0\include\um\winsock2.h 2296
Error 49 error C2375: 'send' : redefinition; different linkage c:\program files (x86)\windows kits\8.0\include\um\winsock2.h 2026
How can I solve this issue?
Thanks!
Edit: I've tried to use #define _WINSOCKAPI_ as well, though it did not resolve my problems...
I have winsock.h first, then windows.h, though it still does the error for me.
Make sure that <windows.h> doesn't include <winsock.h> (which provides many of the same declarations as <winsock2.h>). In the <winsock2.h> file on my system there is this line:
#define _WINSOCKAPI_ /* Prevent inclusion of winsock.h in windows.h */
The _WINSOCKAPI_ include guard may be an internal implementation detail, but as a practical solution I would rely on it, just defining this symbol before including <windows.h>, e.g. in the compiler invocation (which for an IDE means in the IDE project settings).
Alternatively you can try to always include <winsock2.h> before <windows.h>, in order to establish the relevant include guard whatever it is (but this is I think much more fragile than just assuming that the above guard is practically well-defined);
or you can define WIN32_LEAN_AND_MEAN, which prevents <windows.h> from including <winsock.h> but also some other headers (listing from source on my system those are <cderr.h>, <dde.h>, <ddeml.h>, <dlgs.h>, <lzexpand.h>, <mmsystem.h>, <nb30.h>, <rpc.h>, <shellapi.h>, <winperf.h>, <wincrypt.h>, <winefs.h>, <winscard.h>, <winspool.h>, <ole2.h>, and <commdlg.h>). I do not recommend relying on WIN32_LEAN_AND_MEAN optimization for correctness.
I.e., minimum:
#undef UNICODE
#define UNICODE
#undef _WINSOCKAPI_
#define _WINSOCKAPI_
#include <windows.h>
#include <winsock2.h>
auto main()
-> int
{}
I made sure that an #include "Winsock2.h" is before any #include "windows.h" and "#include "Winsock.h" and this solved the case.
Just a matter of patience, look at includes one by one and establish this order, first #include "Winsock2.h" then #include "windows.h"
I checked the recursive includes, I spotted the header files which include (recursively) some #include "windows.h" and "#include "Winsock.h"and write a#include "Winsock2.h". in this files, i added#include "Winsock2.h"` as the first include.
Add the includes to pch.h:
// TODO: add headers that you want to pre-compile here
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
// Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib")
Related
For some reason I can't explain, the compiler is outputting an error saying that it found an unexpected #else token.
This occurs at the beginning of the file :
#if defined( _USING_MFC )
#include "stdafx.h"
#else
#include <windows.h>
#endif
There is nothing before that peice of code expect several (single-line) comments.
This error occurs in a .cpp file. What you see above is the beginning of the file. There is nothing before that.
I tried adding the following code before the code defined above, and the error is now an unexpected #endif
#if 1
#include "stdafx.h"
#endif
So I suspect there is an issue with the included stdafx.h file which contains the following code :
#ifndef STDAFX_H_INCLUDED
#define STDAFX_H_INCLUDED
#include <Afx.h>
#include <Windows.h>
using namespace ATL;
#endif // STDAFX_H_INCLUDED
There's really nothing special about it. I'm also including this stdafx.h file from a stdafx.cpp file that only contains the #include statement, and it compiles correctly.
Here are the project preprocessor definitions :
_DEBUG
_WIN32_WCE=$(CEVER)
UNDER_CE
WINCE
DEBUG
_WINDOWS
$(ARCHFAM)
$(_ARCHFAM_)
_UNICODE
UNICODE
_TERMINAL_FALCONX3_CE6
_NO_CPP_EXCEPTIONS
_DONT_INCLUDE_WS_HEADERS
_USING_MFC
And some extra informations :
Compiling for Windows CE 6 using Visual Studio 2008.
What would be causing this ? Thank you.
Based on the name stdafx, I assume it is a precompiled header.
A precompiler header must be the first include (preprocessor) directive in the file, you can't put anything (not even an ifdef) before it. The only exception being a few comment lines, as those would be ignore anyway.
Based on your example, you should put the #ifdef _USING_MFC into your stdafx.h, and include Afx.h there.
I'm using rapidjson, which is an all header library. In rapidjson.h, there is a macro RAPIDJSON_ASSERT, in one of my cpp files, I would like to redefine it, so I have this code at the top of my file:
#include "stdafx.h" // for windows
#pragma push_macro("RAPIDJSON_ASSERT")
#define RAPIDJSON_ASSERT(x) if(!(x)) throw std::logic_error("rapidjson exception");
#include "rapidjson/rapidjson.h"
#include "rapidjson/document.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/writer.h"
....
....
#pragma pop_macro("RAPIDJSON_ASSERT")
Here is the whay that rapidjson.h defines RAPIDJSON_ASSERT:
#ifndef RAPIDJSON_ASSERT
#include <cassert>
#define RAPIDJSON_ASSERT(x) assert(x)
#endif // RAPIDJSON_ASSERT
The documentation states that to override the RAPIDJSON_ASSERT logic, you just have to define RAPIDJSON_ASSERT before you include any of the files.
The issue is that when I run the code in the debugger, RAPIDJSON_ASSERT is not being redefined. I checked stdafx.h for anything which would include the rapidjson header files, and there isn't anything.
I was under the assumption that each compilation unit should run through the header files.
Note that if I move the redefinition of the macro into stdafx.h I get the macro redefined, but I was hoping to be able to do it per compilation unit.
It seems like you want to change the definition of RAPIDJSON_ASSERT for the rapidjson code itself
If so, you need to add a #define after the place where it is defined. Unless you want to edit the rapidjson.h file, the only alternative is to do this:
#include "stdafx.h" // for windows
// One would assume that the macro gets defined somewhere inside here
#include "rapidjson/rapidjson.h"
// Compiler will complain about macro redefinition without this #undef
#undef RAPIDJSON_ASSERT
#define RAPIDJSON_ASSERT(x) if(!(x)) throw std::logic_error("rapidjson exception");
#include "rapidjson/document.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/writer.h"
Now the definition of RAPIDJSON_ASSERT is changed for the rest of the header files. You don't need the push_macro and pop_macro shenanigans - macros only are valid for each unit
Note that it's not a a good thing to redefine things for libraries using #define
I'm trying to compile some code to render stuff on windows that don't belong to my application. I want to use DirectX 9 to achieve this, but for some reason linker errors are killing me right now.
Im including all my libs in a .h file (see main.h below).
Now i include this .h file in every other .h file i have, to make use of it everywhere. Sometimes i only include a .h file that includes my main file.
The problem is now, that when i go to any class I want to use DirectX in (with included .h file), i get the LNK2019 error.
auto hResult = Direct3DCreate9Ex( D3D_SDK_VERSION, &this->m_pDirect3D9Ex );
Error at Direct3DCreate9Ex
The corresponding .h file includes the main .h file in the following way:
#ifdef _MSC_VER
#pragma once
#endif
#include "main.h"
main.h (complete):
#pragma once
#endif
#include <Windows.h>
#include <iostream>
#include <vector>
#include <memory>
#include <chrono>
#include <thread>
#include <d3d9.h>
#include <d3dx9.h>
#include <dwmapi.h>
#pragma comment( lib, "d3d9.lib" )
#pragma comment( lib, "d3dx9.lib" )
#pragma comment( lib, "dwmapi.lib" )
#endif
This even works when compiling with VisualStudio, but I need to achieve this using the cl.exe. When i try to build it after i moved everything out of VisualStudio, the unresolved symbol errors occur.
I would greatly appreciate any help i could get, already wasted hours on this.
Edit: Error
You are pointing to the 64 bit DirectX libraries on your command line. You need to use the 32 bit libs.
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.
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.