SOCKET in winsock2.h not accepted in visual studio 2017 - c++

Getting error i freetds headers when i include them in my VS 2017 c++ project
in tds.h when i include that in my project
include\tds.h(1331): error C3646: 's': unknown override specifier
include\tds.h(1331): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
include\tds.h(1641): error C2065: 'TDS_SYS_SOCKET': undeclared identifier
include\tds.h(1641): error C2146: syntax error: missing ')' before identifier 's'
I am trying to use freetds 0.91.100 version in my c++ application which was built using VS 2010. It was fine then.
Now after migrating my C++ project to VS 2017 i am getting strange errors.
I have built freetds lib as well in VS 2017.
freetds has this declaration in tds_sysdep_private.h
#if !defined(__WIN32__) && !defined(_WIN32) && !defined(WIN32)
typedef int TDS_SYS_SOCKET;
#define INVALID_SOCKET -1
#define TDS_IS_SOCKET_INVALID(s) ((s) < 0)
#else
typedef SOCKET TDS_SYS_SOCKET;
#define TDS_IS_SOCKET_INVALID(s) ((s) == INVALID_SOCKET)
#endif
and the tds.h has
struct tds_socket
{
TDS_SYS_SOCKET s; /**< tcp socket, INVALID_SOCKET if not connected */
}
And the error is on this TDS_SYS_SOCKET declaration
My code include this header this way.
tdsloader.h
using namespace std;
#if defined (__cplusplus)
extern "C" {
#endif
#include "tds.h"
#if defined (__cplusplus)
}
#endif
As per the declaration of TDS_SYS_SOCKET in tds_sysdep_private.h , in case of windows build it is defined as SOCKET which is from winsock2.h
I read in other threads that the order of header file includion is important and i made sure that winsock2.h is included before windows.h or any other windows header file.
Now that SOCKET from winsock2.h is
typedef UINT_PTR SOCKET;
which in an unsigned , why is VS 2017 not able to recognize the type ?
Build should go through smoothly as it did in VS 2010.
Now with VS 2017 it shows build errors.

Here's the include order that works fine with VS 2017:
#include <tds_sysdep_private.h>
#include <tds.h>
alternatively you can:
#define _FREETDS_LIBRARY_SOURCE
#include <tds.h>

Related

Is WDK for Windows 10 compatible with the C++17 language standard?

Is WDK for Windwos 10 compatible with the C++17 language standard ?
I am asking because, after I switched the compiler form C++14 to the C++17 language standard
( with the WDK v10.0.19041.0 included ), I started to receive the following warning:
C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\km\crt\setjmp.h(206,107): warning C5040: dynamic exception specifications are valid only in C++14 and earlier; treating as noexcept(false)
According to Microsoft's documentation the C5040 warning was added in Visual Studio 2017 version 15.5 (compiler version 19.12.25830.0).
Below is the offending code:
#ifdef __cplusplus
extern "C"
{
#if _MSC_VER >= 1200
#pragma warning(push)
#pragma warning(disable:4987) /* nonstandard extension: throw (...) */
#endif
_CRTIMP __declspec(noreturn) void __cdecl longjmp(_In_reads_(_JBLEN) jmp_buf _Buf, _In_ int _Value) throw(...);
#if _MSC_VER >= 1200
#pragma warning(pop)
#endif
}
#else
_CRTIMP __declspec(noreturn) void __cdecl longjmp(_In_reads_(_JBLEN) jmp_buf _Buf, _In_ int _Value);
#endif
Note that this is a code in the Microsoft's WDK that generates this warning - not my code.
C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\km\crt\setjmp.h(206,107)
Apparently, the throw specifier was deprecated in C++11 and removed in C++17.
Someone has already complained to Microsoft about this 5 years ago (in May 2018) but nothing was done about it. See this bug report.

error: C2061: syntax error: identifier 'concurrent_vector<`template-type-parameter-1',`template-type-parameter-2'>'

A code compiles/runs fine on Linux and macOS. On Windows 10, I'm compiling the code with Visual Studio 2017 toolchain, but I'm receiving this error:
...\deps\tbb-2020.3-win\tbb\include\tbb\concurrent_vector.h:680: error: C2061: syntax error: identifier 'concurrent_vector<'template-type-parameter-1','template-type-parameter-2'>'
The error happens at this concurrent_vector template:
//! Copying constructor for vector with different allocator type
template<class M>
__TBB_DEPRECATED concurrent_vector( const concurrent_vector<T, M>& vector, const allocator_type& a = allocator_type() )
: internal::allocator_base<T, A>(a), internal::concurrent_vector_base()
{
vector_allocator_ptr = &internal_allocator;
__TBB_TRY {
internal_copy(vector.internal_vector_base(), sizeof(T), &copy_array);
} __TBB_CATCH(...) {
segment_t *table = my_segment.load<relaxed>();
internal_free_segments( table, internal_clear(&destroy_array), my_first_block.load<relaxed>() );
__TBB_RETHROW();
}
}
The error happens inside the TBB headers:
C:\...\deps\tbb-2020.3-win\tbb\include\tbb\concurrent_vector.h
What could possibly be the cause?
Here is a more complete error log:
c:\users\m3\repos\3d-editor\editorlib\deps\tbb-2020.3-win\tbb\include\tbb\concurrent_vector.h(680): error C2061: syntax error: identifier 'concurrent_vector<`template-type-parameter-1',`template-type-parameter-2'>'
c:\users\m3\repos\3d-editor\editorlib\deps\tbb-2020.3-win\tbb\include\tbb\concurrent_vector.h(1167): note: see reference to class template instantiation 'tbb::concurrent_vector<T,A>' being compiled
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\memory_resource(860): note: see reference to class template instantiation 'std::pmr::_Intrusive_stack<std::pmr::monotonic_buffer_resource::_Header,void>' being compiled
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\memory_resource(465): note: see reference to class template instantiation 'std::pmr::_Intrusive_stack<std::pmr::unsynchronized_pool_resource::_Pool::_Chunk,void>' being compiled
c:\users\m3\repos\3d-editor\editorlib\deps\tbb-2020.3-win\tbb\include\tbb\concurrent_vector.h(681): error C2334: unexpected token(s) preceding ':'; skipping apparent function body
c:\users\m3\repos\3d-editor\editorlib\deps\tbb-2020.3-win\tbb\include\tbb\concurrent_vector.h(61): fatal error C1075: '{': no matching token found
jom: C:\Users\m3\repos\build-3dsceneeditor-Desktop_Qt_5_12_9_MSVC2017_64bit-Debug\editorlib\Makefile.Debug [debug\openvdbutils.obj] Error 2
This code can be used to recreate the issue in combination with the /Zc:__cplusplus compiler flag in visual studio 2017:
#include <tbb/concurrent_vector.h>
int main()
{
tbb::concurrent_vector<int> vector;
vector.push_back(1);
return 0;
}
This seems to be a visual studio bug. From the issue on the TBB github respository the /Zc:__cplusplus compiler flag is the cause.
Enabling that flag causes this code to use the c++14 [[deprecated]] attribute:
#if (__cplusplus >= 201402L)
#define __TBB_DEPRECATED [[deprecated]]
#define __TBB_DEPRECATED_MSG(msg) [[deprecated(msg)]]
#elif _MSC_VER
#define __TBB_DEPRECATED __declspec(deprecated)
#define __TBB_DEPRECATED_MSG(msg) __declspec(deprecated(msg))
#elif (__GNUC__ && __TBB_GCC_VERSION >= 40805) || __clang__
#define __TBB_DEPRECATED __attribute__((deprecated))
#define __TBB_DEPRECATED_MSG(msg) __attribute__((deprecated(msg)))
#endif
Swapping this code around to always use the visual studio specific code seems to fix the problem:
#if _MSC_VER
#define __TBB_DEPRECATED __declspec(deprecated)
#define __TBB_DEPRECATED_MSG(msg) __declspec(deprecated(msg))
#elif (__cplusplus >= 201402L)
#define __TBB_DEPRECATED [[deprecated]]
#define __TBB_DEPRECATED_MSG(msg) [[deprecated(msg)]]
#elif (__GNUC__ && __TBB_GCC_VERSION >= 40805) || __clang__
#define __TBB_DEPRECATED __attribute__((deprecated))
#define __TBB_DEPRECATED_MSG(msg) __attribute__((deprecated(msg)))
#endif
A smaller demonstration of the same problem is:
template <typename T>
class Foo
{
public:
Foo() {}
template <typename X>
[[deprecated]] Foo(const Foo<T>& a)
{
}
};
int main()
{
Foo<int> x;
return 0;
}
This only doesn't work in Visual Studio 2017, it works in 2019 so the simplest fix is to update visual studio.
A pull request has been created to fix this issue and reported to Microsoft, I wouldn't expect MS to fix it though as it is already fixed in 2019.
As discussed here a short fix would be to downgrade to TBB 2020 Update 1 on Windows. Thanks to #AlanBirtles

problem in forward declaring closesocket function

I have a simple winsock in c++ and I don't want to include the entire windows.h header or other winsock headers in the wrapper header
This is to reduce compile times and be lightweight in the ide and to evade the windows.h before winsock headers problem
for most winsock functions it is strait to use them in the cpp implementation file only and let my wrapper do its job of simplifying things
but I only need closesocket function in my header file in order to use it in the raii handle of sockets
using socket_t = uintptr_t;
using SocketHandle = std::unique_ptr<void, ZeroOrMinusHandleDeleter<socket_t, static_cast<socket_t>(-1), false, socket_t, closesocket>>;
I could use a proxy function and implement it in the cpp file like this :
// in .h file
using socket_t = uintptr_t;
int CloseSocket(socket_t fd);
using SocketHandle = std::unique_ptr<void, ZeroOrMinusHandleDeleter<socket_t, static_cast<socket_t>(-1), false, socket_t, CloseSocket>>;
// in .cpp file
int CloseSocket(socket_t fd) { return closesocket(fd); }
but I don't want to add extra function call which only redirects to another call
so I tried to forward declare it like this :
extern "C"
{
using socket_t = uintptr_t;
__declspec(dllimport) int __stdcall closesocket(socket_t s);
}
but I got error : C2375 'closesocket': redefinition; different linkage
I tried eliminating __declspec(dllimport) and it compiled but with this warning :
C4273 'closesocket': inconsistent dll linkage
I see the previous one is the same as the definition in WinSock2.h :
WINSOCK_API_LINKAGE
int
WSAAPI
closesocket(
_In_ SOCKET s
);
but I can't understand why it produces this error
UPDATE :
I tired with clang and got these warnings
when using
__declspec(dllimport) int __stdcall closesocket(socket_t s);
I got
warning : redeclaration of 'closesocket' should not add 'dllimport' attribute [-Wdll-attribute-on-redeclaration]
C:\Program Files (x86)\Windows Kits\10\Include\10.0.17763.0\um\winsock.h(749): note: previous declaration is here
and when using
int __stdcall closesocket(socket_t s);
I get
warning : 'closesocket' redeclared without 'dllimport' attribute: previous 'dllimport' ignored [-Winconsistent-dllimport]
C:\Program Files (x86)\Windows Kits\10\Include\10.0.17763.0\um\winsock2.h(1668): note: previous declaration is here
C:\Program Files (x86)\Windows Kits\10\Include\10.0.17763.0\um\winsock2.h(1665): note: previous attribute is here
C:\Program Files (x86)\Windows Kits\10\Include\10.0.17763.0\um\winsock2.h(94): note: expanded from macro 'WINSOCK_API_LINKAGE'
C:\Program Files (x86)\Windows Kits\10\Include\10.0.17763.0\um\winnt.h(184): note: expanded from macro 'DECLSPEC_IMPORT'
so it's amazing that I'm warned not to add or remove dllimport to the function declaration !
from the warnings I found that there are two declarations for closesocket and other socket functions
one is in winsock.h header which causes the first warning :
int PASCAL FAR closesocket ( IN SOCKET s);
and another in winsock2.h which causes the second warning :
#if INCL_WINSOCK_API_PROTOTYPES
WINSOCK_API_LINKAGE
int
WSAAPI
closesocket(
_In_ SOCKET s
);
#endif /* INCL_WINSOCK_API_PROTOTYPES */
it seems that windows.h includes winsock.h so that when my header is included after windows.h I get the first warning
and the second warning is because I include winsock2.h before windows.h in the implementation file and due to this I get the second warning if I evade the first
in conclusion I have to include my header before windows.h or winsock headers so I came to the issue I was trying to solve (the order of inclusion) !

Compile errors when using C++ and bcrypt header

I'm trying to test Windows Bcrypt. I have a test program:
#include <bcrypt.h>
#include <iostream>
#include <string>
#pragma comment (lib, "bcrypt.lib")
int main(int argc, char* argv[])
{
return 0;
}
Attempting to compile it:
>cl.exe /DWINVER=0x0600 /TP /GR /EHsc bcrypt-test.cpp /link /out:bcrypt-test.exe
Microsoft (R) C/C++ Optimizing Compiler Version 19.00.24210 for x64
Copyright (C) Microsoft Corporation. All rights reserved.
bcrypt-test.cpp
C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\shared\bcrypt.h(39):
error C2059: syntax error: 'return'
C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\shared\bcrypt.h(40):
error C2143: syntax error: missing ';' before '*'
C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\shared\bcrypt.h(40):
error C4430: missing type specifier - int assumed. Note: C++ does not support d
efault-int
...
C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\shared\bcrypt.h(681)
: error C3646: 'cbKeyLength': unknown override specifier
C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\shared\bcrypt.h(681)
: fatal error C1003: error count exceeds 100; stopping compilation
I am using a Visual C++ x64 Build Tools command prompt. As I understand things, Bcrypt needs to target Vista or above. WINVER=0x0600 should satisfy the requirement. I found a similar question on the MSDN forums at bcrypt.h build error?, and it says to use a modern SDK. I believe the Windows Kit SDK should satisfy the requirement.
Why am I experiencing compile errors, and how do I fix it?
Line 39 in bcrypt.h is the first typedef below. The preamble, like copyright and header guards, were skipped for brevity.
#ifndef WINAPI
#define WINAPI __stdcall
#endif
#ifndef _NTDEF_
typedef _Return_type_success_(return >= 0) LONG NTSTATUS;
typedef NTSTATUS *PNTSTATUS;
#endif
#ifndef BCRYPT_SUCCESS
#define BCRYPT_SUCCESS(Status) (((NTSTATUS)(Status)) >= 0)
#endif
You are missing an include.
#include <Windows.h> // <- Added this
#include <bcrypt.h>
#include <iostream>
#include <string>
#pragma comment (lib, "bcrypt.lib")
int main()
{
return 0;
}

Error in official Twain header VS2012 C++

I am trying to program a C++ Wrapper for TWAIN with Visual Studio 2012, Windows 7.
I have installed Twain SDK and have all DLL like Twain_32.dll in C:/Windows
I have twain.h : https://svn.apache.org/repos/asf/incubator/ooo/trunk/main/twain/inc/twain.h
And I have my main :
#include <windows.h>
#include "twain.h"
int main()
{
return 0;
}
And I get 200 errors like for this line
#ifdef _MSWIN_
typedef HANDLE TW_HANDLE;
typedef LPVOID TW_MEMREF;
error C2146: syntax error : missing ';' before identifier 'TW_HANDLE'
How can I resolve that ?