FreeTDS / freetds

Official FreeTDS repository
http://www.freetds.org/
GNU General Public License v2.0
460 stars 159 forks source link

Problems when compiling with a c++ compiler #404

Closed degaart closed 3 years ago

degaart commented 3 years ago

Hello,

Freetds defines int64t etc... when \_STDCVERSION_\ is not defined. But clang++ and g++ do not define it. Which causes problem when the standard system include files redefines them. Example on macOS 10.14, Apple clang version 11.0.0 (clang-1100.0.33.17):

In file included from ../test/test_sybase.cpp:3:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/string:505:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/string_view:176:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__string:57:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/algorithm:642:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/cstring:61:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/string.h:61:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/string.h:141:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/sys/_types/_rsize_t.h:30:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/machine/types.h:35:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/i386/types.h:79:
/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/sys/_types/_int64_t.h:30:33: error: typedef redefinition with different types ('long long' vs 'long')
typedef long long               int64_t;
                                ^
/Volumes/Projects/boboloss/include/tds_sysdep_public.h:60:28: note: previous definition is here
  typedef   signed  long   int64_t;     /* 64-bit int */

Maybe in tds_sysdep_public.h, freetds should first check for __cplusplus >= 201103L in tds_sysdep_public.h, and if it's defined, include stdint.h. Something like:

#if (defined(__cplusplus) && __cplusplus >= 201103L) || (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L)
fziglio commented 3 years ago

The change makes sense however this assumes that C++11 means having these headers, which could be false. I think VC++ for instance added these headers much later :disappointed:. I think I'll cut the release without this change for the time being.

degaart commented 3 years ago

C++11 includes C99 and stdint.h per the standard:

For compatibility with the C standard library and the C Unicode TR, the C++ standard library provides the 25 C headers, as shown in Table 154.

Where Table 154 includes stdint.h

The problem lies with VC++ not adhering to the standard by lying about it's C++11 support.

I propose if possible, to check for _MSC_VER :

`

if (!defined(_MSC_VER) && defined(cplusplus) && cplusplus >= 201103L) || (defined(STDC_VERSION) && STDC_VERSION >= 199901L)

`

Edit: Another possibility which may reduce the likelihood of bugs/incompatibilities may be to add a macro FREETDS_USE_STDINT. That way, I can work around the problem by defining just that macro before including freetds headers.

`

if defined(FREETDS_USE_STDINT) || (defined(STDC_VERSION) && STDC_VERSION >= 199901L)

`

fziglio commented 3 years ago

Testing the

#if (!defined(_MSC_VER) && defined(__cplusplus) && __cplusplus >= 201103L) || (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L)

it seems to work

fziglio commented 3 years ago

@degaart can you provide a commit or at least give the email address so I can create a commit?

degaart commented 3 years ago

Pull request created

qkdreyer commented 1 year ago

I'm having issues compiling Qt 4.8.4 using macOS Ventura 11.3 :

/Applications/Xcode.app/Contents/Developer/usr/bin/make -f Makefile.Debug all
clang++ -c -pipe -stdlib=libc++ -mmacosx-version-min=10.9 -I/usr/local/opt/openssl@1.0/include -g -arch x86_64 -fvisibility=hidden -fvisibility-inlines-hidden -Wall -W -fPIC -DQT_NO_CAST_TO_ASCII -DQT_NO_CAST_FROM_ASCII -DQT_PLUGIN -DQT_SQL_LIB -DQT_CORE_LIB -DQT_HAVE_MMX -DQT_HAVE_3DNOW -DQT_HAVE_SSE -DQT_HAVE_MMXEXT -DQT_HAVE_SSE2 -DQT_HAVE_SSE3 -DQT_HAVE_SSSE3 -DQT_HAVE_SSE4_1 -DQT_HAVE_SSE4_2 -DQT_HAVE_AVX -D_LARGEFILE64_SOURCE -D_LARGEFILE_SOURCE -DQT_SHARED -I../../../../mkspecs/unsupported/macx-clang-libc++ -I. -I../../../../include/QtCore -I../../../../include/QtSql -I../../../../include -I.moc/debug-shared -o .obj/debug-shared/main.o main.cpp
In file included from main.cpp:50:
In file included from ./../../../sql/drivers/tds/qsql_tds.h:60:
In file included from /usr/local/include/sybfront.h:23:
In file included from /usr/local/include/sybdb.h:31:
/usr/local/include/tds_sysdep_public.h:61:28: error: typedef redefinition with different types ('long' vs 'long long')
  typedef   signed  long   int64_t;     /* 64-bit int */
                           ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/sys/_types/_int64_t.h:30:33: note: previous definition is here
typedef long long               int64_t;
                                ^
In file included from main.cpp:50:
In file included from ./../../../sql/drivers/tds/qsql_tds.h:60:
In file included from /usr/local/include/sybfront.h:23:
In file included from /usr/local/include/sybdb.h:31:
/usr/local/include/tds_sysdep_public.h:62:27: error: typedef redefinition with different types ('unsigned long' vs 'unsigned long long')
  typedef unsigned  long  uint64_t;     /* 64-bit int */
                          ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/_types/_uint64_t.h:31:28: note: previous definition is here
typedef unsigned long long uint64_t;
                           ^
2 errors generated.

__cplusplus is defined but its value is 199711L :

<built-in>:378:21: note: expanded from here
#define __cplusplus 199711L
qkdreyer commented 1 year ago

I found these implementations inside macOS SDKs :

qkdreyer commented 1 year ago

@freddy77 ping

fziglio commented 1 year ago

Can you try a more recent C++ version ? It's 2023

qkdreyer commented 1 year ago

sadly clang++ uses c++98 by default

fziglio commented 1 year ago

@qkdreyer you can add -std=c++11

degaart commented 1 year ago

Can you try a more recent C++ version ? It's 2023

Speaking of 2023, let's get rid of these integer typedefs from tds_sysdep_public.h. Everyone ought to use at least a C99 compiler these days.

fziglio commented 1 year ago

@degaart yes, probably the rule should be now the opposite. Assume we have stdint.h unless we found some issues.