Weird compile error dealing with Winnt.h - c++

When trying to compile a file that include winnt.h via windows.h, I get the following error:
MyGl.cpp
..\microsoft sdks\windows\v6.0a\include\winnt.h(964) : error C2988: unrecognizable template declaration/definition
..\microsoft sdks\windows\v6.0a\include\winnt.h(964) : error C2059: syntax error : '&'
They point to the following lines in Winnt.h
extern "C++" // templates cannot be declared to have 'C' linkage
template <typename T, size_t N>
char (*RtlpNumberOf( UNALIGNED T (&)[N] ))[N];
#define RTL_NUMBER_OF_V2(A) (sizeof(*RtlpNumberOf(A)))
Any ideas for what's going on?
My compiler:
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.21022.08 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.

There are at least two ways to do this. The first is to simply include windows.h at the top of all your files. Then include winnt.h only if you need it. However, I find this a bit too much - I don't see the need of including all this goo in every single file.
What I do is this at the very top (first thing) in my C/C++ header files.
#ifndef __wtypes_h__
#include <wtypes.h>
#endif
#ifndef __WINDEF_
#include <windef.h>
#endif
This will get you you the data types, defines, and fundamental Windows API's. You may also need to add the following:
#ifndef _WINUSER_
#include <winuser.h>
#endif
#ifndef __RPC_H__
#include <rpc.h>
#endif
WinNT is a bit of a special animal - don't include it if including the above files works for you. If you do need it, include it after wtypes.h and `windef.h'
If this doesn't work, then check your include paths and predefined macros to see if those might be breaking your build.
Regards, Foredecker

Related

Using OpenBLAS LAPACKE in Visual Studio

i need some linear algebra in my project and want use OpenBLAS for this. I downloaded the precompiled version (64bit version) and unpacked it to my projectfolder. In Visual Studio, i added include-, bin-, and lib-folder to my Project and ran the this example without problems.
Next, i wanted to look at LAPACK, so i added lapacke.h to the includes, which is in the same directory as cblas.h and is included in the official download. But now i get hundreds of errors, for every function, as if a lib file was missing or something. E.g. for this line
85 lapack_complex_float lapack_make_complex_float( float re, float im );
i get
PATH\include\lapacke.h(85): error C2146: syntax error: missing ';' before identifier 'lapack_make_complex_float'
I can't find any further information on how to set up OpenBLAS/LAPACK, they usually just say 'include the files', which i have. Otherwise the cblas example wouldn't run either. And the (relevant) examples i can find only use cblas.h, not lapacke.h
Can some tell me what i'm doing wrong?
The problem is that OpenBlas uses C99 _Complex by default. This is not supported by Visual C++. You can solve this by using standard library definitions before including lapacke.h:
#include <complex>
#define lapack_complex_float std::complex<float>
#define lapack_complex_double std::complex<double>
#include <lapacke.h>
Using std::complex is problematic unless OpenBLAS was built with LAPACK_COMPLEX_CPP otherwise the library uses a different complex type internally, usually C99 _Complex.
Modern versions of the Microsoft compiler (e.g. the one in VSTUDIO 2022) support a similar "_Complex" mechanism. Therefore I include the following header file prior to "lapacke.h"
#ifdef _MSC_VER
#include <complex.h>
#define LAPACK_COMPLEX_CUSTOM
typedef _Dcomplex lapack_complex_float;
typedef _Fcomplex lapack_complex_double;
#define lapack_complex_float_real(z) (real(z))
#define lapack_complex_float_imag(z) (imag(z))
#endif // _MSC_VER
See the lapack.h header file that ships with OpenBLAS how #define LAPACK_COMPLEX_CUSTOM overrides complex variable type definitions

Define from include breaking other previous includes

I'm trying to debug the includes of my project's main file. Here's my include code.
//Gameplay
#include "gameplay.h"
//LibNoise
#include <noise/noise.h>
//Console Window
#ifndef _WINDOWS_
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#undef KEY_EVENT
#undef MOUSE_EVENT
#endif
#include <io.h>
#include <fcntl.h>
//RakNet
#include "MessageIdentifiers.h"
#include "RakPeerInterface.h"
#include "BitStream.h"
#include "RakNetTypes.h"
//My Includes
#include "Island.h"
The problem is, gameplay.h includes a file (specifically ScriptController.h) where there's an enum that contains the words KEY_EVENT and MOUSE_EVENT which is included through some includes in windows.h (specifically wincon.h). This breaks the enum and I get errors during compilation. Note, it is actually including windows.h because _WINDOWS_ isn't defined at this point according to MSVS (so it's not like it's defined before gameplay.h or something).
I can't see why this would be a problem as gameplay.h is included before windows.h which should mean that I would get no trouble with replacement of the terms in the enum? Undefining them doesn't help either.
Where did I go wrong? Is there any way I can "debug" the preprocessor and see the output from the preprocessor that is causing this syntax error and some kind of #include chain? I want to be able to fix this myself next time if it occurs.
Here's the errors
Error 3 error C2065: 'CALLBACK_COUNT' : undeclared identifier c:\users\pf\downloads\gameplay-master\gameplay\src\scriptcontroller.h 1024 1 testerino2
Error 1 error C2059: syntax error : 'constant' c:\users\pf\downloads\gameplay- master\gameplay\src\scriptcontroller.h 769 1 testerino2
Error 2 error C3805: 'constant': unexpected token, expected either '}' or a ',' c:\users\pf\downloads\gameplay-master\gameplay\src\scriptcontroller.h 769 1 testerino2
Here's the wincon.h defines
#define KEY_EVENT 0x0001 // Event contains key event record
#define MOUSE_EVENT 0x0002 // Event contains mouse event record
Here's the offending code lines of ScriptController.h
762| enum ScriptCallback
763| {
764| INITIALIZE = 0,
...
768| RESIZE_EVENT,
769| KEY_EVENT,
770| MOUSE_EVENT,
771| TOUCH_EVENT,
...
775| GAMEPAD_EVENT,
776| CALLBACK_COUNT,
777| INVALID_CALLBACK = CALLBACK_COUNT
778| };
...
1024| std::vector<std::string> _callbacks[CALLBACK_COUNT];
To avoid clashes with windows.h defines, I'd recommend making your own header file that:
Defines any macros that affect windows.h, such as WIN32_LEAN_AND_MEAN
Does #include <winsock2.h> if used, and #include <windows.h>
Does #undef on any annoying macros
Then make sure every source file that might come across a Windows include, includes this header. You could perhaps use a compiler feature to inject the header into all units.

3rd party libraries conflicting definitions/ redefinitons

I am working on Qt Platform with two separate libraries. The Problem that I am facing is that he two libraries have different declaration for int32_t.
The first library has :
#ifdef _WIN32
#if ULONG_MAX == 0xffffffff
typedef long int32_t;
#else
typedef int int32_t;
#endif
#endif
The second Library :
typedef signed __int32 int32_t;
typedef unsigned __int32 uint32_t;
The error that I get is :
C:\Program Files (x86)\SiliconSoftware\Runtime5.1\include\msinttypes\stdint.h:91: error: C2371: 'int32_t' : redefinition; different basic types
c:\program files (x86)\matlab\r2008a\extern\include\mclmcr.h:216: see declaration of 'int32_t'
I tried following this post on stackoverflow :
Typedef redefinition (C2371) for uint32 in two 3rd-party libraries
And i tried to implement it in my code :
#define int32_t VicTorv3_int32_t
#include"mclmcr.h"
#undef int32_t
#define int32_t Silicon_int32_t
#include "stdint.h"
#undef int32_t
I still get the same error. Please help.
stdint.h is also a system include file. Chances are good that it gets included before the define/undef workaround. And when you workaround tries to include the file again, the inclusion guards do their work. You might check the situation using this:
Displaying the #include hierarchy for a C++ file in Visual Studio
I suggest moving the part where you include stdint.h to the very top of the file, before ALL other includes.
Be watchful, shadowing the system include file stdint.h with another version asks for problems.

poppack.h wrong?

i got a Project to go on coding, so its not my code till now and i can´t explain the following warning message in VS2008 compiling my code:
Warnung 2 warning C4161: #pragma
pack(pop...): Mehr pop- als
push-Vorgänge c:\programme\microsoft
sdks\windows\v6.0a\include\poppack.h 29
/*++
Copyright (c) Microsoft Corporation. All rights reserved.
Module Name:
poppack.h
Abstract:
This file turns packing of structures off. (That is, it enables
automatic alignment of structure fields.) An include file is needed
because various compilers do this in different ways.
poppack.h is the complement to pshpack?.h. An inclusion of poppack.h
MUST ALWAYS be preceded by an inclusion of one of pshpack?.h, in one-to-one
correspondence.
For Microsoft compatible compilers, this file uses the pop option
to the pack pragma so that it can restore the previous saved by the
pshpack?.h include file.
--*/
#if ! (defined(lint) || defined(RC_INVOKED))
#if ( _MSC_VER >= 800 && !defined(_M_I86)) || defined(_PUSHPOP_SUPPORTED)
#pragma warning(disable:4103)
#if !(defined( MIDL_PASS )) || defined( __midl )
#pragma pack(pop)
#else
#pragma pack()
#endif
#else
#pragma pack()
#endif
#endif // ! (defined(lint) || defined(RC_INVOKED))
Can you explain why? Isnt this a Microsoft File?
You need to make sure that you match every
#include <pshpack.h>
with a use of
#include <poppack.h>
If my German is serving me right, it's complaining about a mis-match.
It seems that you must include pshpack.h at the beginning of your code, and include poppack.h at the end.

C++ preprocessor unexpected compilation errors

Please look at the following file: (it is a complete file)
#ifndef TEES_ALGORITHM_LIBRARY_WRAPPER_H
#define TEES_ALGORITHM_LIBRARY_WRAPPER_H
#ifdef _TEES_COMPILE_AS_LIB
#include <dfa\Includes\DFC_algorithms.hpp>
#include <DFA\FuzzyClassifier\FuzzyAlgorithmIntialization\InitFuzzyAlgorithm.hpp>
typedef teesalgorithm::tees_fuzzy_algorithms algorithms_switchyard_class;
#else
#include <DFA\Includes\commercial_algorithms.hpp>
//An incomplete class to hide implementation
class algorithms_switchyard_class;
#endif
class AlgorithmLibraryWrapper {
algorithms_switchyard_class * algorithmPtr_;
typedef teesalgorithm::tees_paramObj paramObj_type;
typedef teesalgorithm::tees_errorObj errorObj_type;
typedef teesalgorithm::tees_statusObj statusObj_type;
typedef teesalgorithm::tees_dataObj dataObj_type;
typedef teesalgorithm::tees_outputObj outputObj_type;
public:
AlgorithmLibraryWrapper(const std::string& sAlgName, paramObj_type& paramObj, errorObj_type& errObj, statusObj_type& statusObj, const char* sFilePath);
static bool dataReader(const std::string& sFileName, dataObj_type& dataObj, errorObj_type& errObj, statusObj_type& statusObj);
bool runalgorithm(const dataObj_type& dataObj, outputObj_type& outObj, errorObj_type& errObj, statusObj_type& statusObj);
~AlgorithmLibraryWrapper();
};
#ifdef _TEES_USE_COMPILED_ALGORITHM_LIB
# ifdef _MSC_VER
#if _MSC_VER < 1400 // If VC 2003
#ifdef _DEBUG
#error No AlgorithmLibWrapper libraries compiled for this version of VC
#else
#error No AlgorithmLibWrapper libraries compiled for this version of VC
#endif
#elif defined(UNDER_CE) // Win CE
#ifdef _DEBUG
#pragma comment( lib, "AlgorithmLibWrapperCEd" )
#else
#pragma comment( lib, "AlgorithmLibWrapperCE" )
#endif
#else // If VC 2005
#ifdef _DEBUG
#pragma comment( lib, "AlgorithmLibWrapperd" )
#else
#pragma comment( lib, "AlgorithmLibWrapper" )
#endif
#endif
#endif
#endif
#endif //TEES_ALGORITHM_LIBRARY_WRAPPER_H
I am getting the following errors; I don't know why. I manually counted the preprocessor directives also.
AlgorithmLibraryWrapper.hpp:10:1: unterminated #ifdef
AlgorithmLibraryWrapper.hpp:7:1: unterminated #ifndef
I am using the poor vxWorks gcc compiler. Please let me know if the fault is mine or the compiler's.
It could be that the problem is in the included files (if there actually are unbalaced #if/#endifs.
I would try preprocessing with another compiler. You can use gcc for that, doesn't matter it wouldn't compile. Just get gcc (or MinGW if you're on Windows) and run
cpp -Iinclude_direcories your_file
Or, if you don't like gcc, get MSVC Express edition. Again, you can preprocess code that even doesn't compile, so no problem with nonworking library etc.
Most compilers have an option that will give you the output from the preprocessor so you can check what it's doing. For example,
gcc -E file.c >file.preproc
will give you the pre-processed source so you can check the balancing of #if against #endif.
At a guess, one of the files you are #including from this one has a mismatched #ifdef/#endif pair. You need to look at all the files (as the preprocesor does), not just this one.
As others have noted, this is most likely due to mismatched include guards.
If the files you are including are under your control (i.e. not part of a 3rd party closed source library), you could consider replacing the #ifndef et. al. guards (which are used to prevent multiple inclusion) with #pragma once. This will eliminate the possibility of having mismatched preprocessor directives.
One caveat of this is that pragma once is non-standard, so it will only work if your compiler supports it.
I have tried to compile your source using vs 6.0 but did not get the error you have mentioned. As others said may be the error is coming from the included header file . For me to get your code compiled i need to comment the above header.
Please check the above header once.
I'd debug this by commenting out sections one by one and trying to identify which section is causing the error.
It could be your compiler does not like the nested #ifdefs or does not interpret the syntax correctly. Maybe it doesn't like the #pragmas.
This is a long shot, but in your source file you have the following line:
# ifdef _MSC_VER
where there is whitespace between the '#' character and the directive name (ifdef). This is valid in C/C++; however, it's not too commonly seen so I wouldn't be very surprised if the odd compiler choked on it.