Error compiling program against SDL2 with -std=c++11 g++ flag

Hello!

I think there is something odd with this combination… or maybe i’m just
too stupid to figure it out :smiley:

I’m using Mingw to compile SDL2. The configure searchs for non-standard
functions like itoa and strdup, and found it. Then I compile it, and all OK.

When I try to compile my program against that SDL2 library without
-std=c++11 all goes flawless. But when I try to use the C++11 features and
then compile it with the -std=c++11 flag, the compiler can’t find the
non-standard functions like itoa or strdup that the SDL header have.

My solution was to modify the SDL configure file to force it to not found
any non-standard function, removing this:**************************
for ac_func in malloc calloc realloc free getenv setenv putenv unsetenv
qsort abs bcopy memset memcpy memmove strlen strlcpy strlcat strdup _strrev
_strupr _strlwr strchr strrchr strstr itoa _ltoa _uitoa _ultoa strtol
strtoul _i64toa _ui64toa strtoll strtoull atoi atof strcmp strncmp _stricmp
strcasecmp _strnicmp strncasecmp sscanf snprintf vsnprintf fseeko fseeko64
sigaction setjmp nanosleep sysconf sysctlbyname
do :
as_ac_var=$as_echo "ac_cv_func_$ac_func" | $as_tr_sh
ac_fn_c_check_func “$LINENO” “$ac_func” “$as_ac_var"
if eval test “x$”$as_ac_var”" = x"yes"; then :
cat >>confdefs.h <<_ACEOF
#define $as_echo "HAVE_$ac_func" | $as_tr_cpp 1
_ACEOF

fi
done


Now the SDL2 compile without using that functions from the compiler and my
program with the -std=c++11 flag set is building ok.

Well… This is not the solution of course.I Just want to let you know of
this problem and maybe you could bring a more elegant solution to all of
use trying to use C++11

Thanks !!

Hello!

I think there is something odd with this combination… or maybe i’m just too stupid to figure it out :smiley:

I’m using Mingw to compile SDL2. The configure searchs for non-standard functions like itoa and strdup, and found it. Then I compile it, and all OK.

When I try to compile my program against that SDL2 library without -std=c++11 all goes flawless. But when I try to use the C++11 features and then compile it with the -std=c++11 flag, the compiler can’t find the non-standard functions like itoa or strdup that the SDL header have.

My solution was to modify the SDL configure file to force it to not found any non-standard function, removing this:

This has been a subject of debate. I believe SDL does the wrong thing.
A problem also exists if you build SDL with C89 but then try to
compile using C99. Strangely, I am using clang with --std=c++11 and don’t
have a problem … on OSX though and using clang’s libraries not gccs.

Sam pointed out you can exclude SDL_stdinc.h by just:

#define _SDL_stdinc_h
#include “SDL.h”

The keeps the crap out of your header files.On 03/07/2013, at 4:15 AM, Daniel Guzm?n wrote:


john skaller
@john_skaller
http://felix-lang.org

Hello,

Uhmmm… maybe because clang, using std=c++11 flag will use non standard
functions and GCC only standard functions.

Well… I think we should make a flag for SDL configure to allow SDL compile
only with standard functions support and the problem will be solved. Don’t
know if this will cause more problems… but i think it should be a solution
to all of us trying to use C++11

2013/7/3, Daniel Guzm?n <daniel.guzman85 at gmail.com>:

Uhmmm… maybe because clang, using std=c++11 flag will use non standard
functions and GCC only standard functions.

GCC has nothing to do with it, it’s the system headers that do.
They’re the ones that decide to define something or not based on
whether STDC is defined. Now, if you mean glibc, that’s a
different issue.

Try compiling with -std=gnu++11 (C++11 with GNU extensions). The
default is gnu++98 – C++98/C++03 with GNU extensions.

Cheers,
~~ OndraOn Tue, Jul 2, 2013 at 8:15 PM, Daniel Guzm?n <daniel.guzman85 at gmail.com> wrote:

When I try to compile my program against that SDL2 library without
-std=c++11 all goes flawless. But when I try to use the C++11 features and
then compile it with the -std=c++11 flag, the compiler can’t find the
non-standard functions like itoa or strdup that the SDL header have.

When I try to compile my program against that SDL2 library without
-std=c++11 all goes flawless. But when I try to use the C++11 features and
then compile it with the -std=c++11 flag, the compiler can’t find the
non-standard functions like itoa or strdup that the SDL header have.

Try compiling with -std=gnu++11 (C++11 with GNU extensions). The
default is gnu++98 – C++98/C++03 with GNU extensions.

But this doesn’t solve the problem. The client programmer has
the authority and responsibility to choose the compiler.

A programmer may be working with a team that
has established rules.

In my case I have a compilation system which uses fixed compiler
switches for everything. When using clang I have -std=c++11. when
using gcc --std=c++ (because on OSX gcc 4.2 is the last available
version ever and it doesn’t have C++11 and never will).

My system allows the client to fix the compiler once when building
the system and after that all the switches etc are automated and
beyond the programmers control.

So no matter how SDL is built, it has to be used with ANY C++ compiler
without modification and without changing the way it was built.

So configuration switches when building are not a solution.

I have the same problem with the SDL_main junk. This assumes
an architectural model which does not apply to many people,
including my system. I’m not building mainlines I’m building
libraries. It is unfortunate in this case the SDL has to interface
to badly designed libraries like Xlib and OpenGL, and so also
ends up reflecting the bad design.On 04/07/2013, at 7:19 AM, Ond?ej Ho?ek wrote:

On Tue, Jul 2, 2013 at 8:15 PM, Daniel Guzm?n <daniel.guzman85 at gmail.com> wrote:


john skaller
@john_skaller
http://felix-lang.org

2013/7/3, Daniel Guzm?n <daniel.guzman85 at gmail.com>:

Uhmmm… maybe because clang, using std=c++11 flag will use non standard
functions and GCC only standard functions.

GCC has nothing to do with it, it’s the system headers that do.
They’re the ones that decide to define something or not based on
whether STDC is defined. Now, if you mean glibc, that’s a
different issue.

On OSX there are no system headers. It doesn’t work like that.
If you use gcc you get one set of headers, if you use clang with
libc++ its an entirely different set of headers and libraries.

The standard libraries belong to the compiler: they’re technically
part of the compiler.On 04/07/2013, at 7:00 AM, Sik the hedgehog wrote:


john skaller
@john_skaller
http://felix-lang.org

Clang (and Xcode) on OS X can use -std=gnu++11.On 2013-07-03, at 8:21 PM, john skaller wrote:

On 04/07/2013, at 7:19 AM, Ond?ej Ho?ek wrote:

On Tue, Jul 2, 2013 at 8:15 PM, Daniel Guzm?n <daniel.guzman85 at gmail.com> wrote:

When I try to compile my program against that SDL2 library without
-std=c++11 all goes flawless. But when I try to use the C++11 features and
then compile it with the -std=c++11 flag, the compiler can’t find the
non-standard functions like itoa or strdup that the SDL header have.

Try compiling with -std=gnu++11 (C++11 with GNU extensions). The
default is gnu++98 – C++98/C++03 with GNU extensions.

In my case I have a compilation system which uses fixed compiler
switches for everything. When using clang I have -std=c++11. when
using gcc --std=c++ (because on OSX gcc 4.2 is the last available
version ever and it doesn’t have C++11 and never will).

Clang (and Xcode) on OS X can use -std=gnu++11.

I know, but so what? I’m not using that and I cannot do so.

Neither can someone else who has already chosen C99 or c++11
standards for their application.On 04/07/2013, at 9:33 AM, Alexander Szpakowski wrote:


john skaller
@john_skaller
http://felix-lang.org

But this doesn’t solve the problem.

I believe it solves Daniel’s original issue, i.e. not being able to
use SDL and C++11 (or, more precisely, a C++ dialect with features
from the C++11 standard) together. You are right in that it does not
solve your issue of “compile once, use everywhere”.

So no matter how SDL is built, it has to be used with ANY C++ compiler
without modification and without changing the way it was built.

That is, through no fault of the SDL developers, impossible.

Look at the whole MinGW/g++ vs. MSVC++ story on Windows: the two C++
ABIs are incompatible. (The situation is effectively the same with
libstdc++ and libc++ on OS X.) Yes, this is bad, but no amount of
complaining on the SDL mailing lists is going to change it. Unless
you’re willing to make the modifications to g++ to be compatible with
the MSVC++ ABI, you’re gonna have to bite the bullet and compile SDL
differently if your current build doesn’t match your internal
policies.

SDL’s job is to abstract system-specific details away, and as
Microsoft’s decade-long unwillingness to provide stdint.h shows,
differences between what C/C++ language standard is supported belong
into the category of “system-specific details”. If you give your
clients the power to change such system-specific details on a whim,
well, I guess you’ll have to add code that rebuilds SDL according to
whatever set of flags has been chosen this time around. Flexibility
doesn’t come for free.

Yours sincerely,
~~ OndraOn Thu, Jul 4, 2013 at 1:21 AM, john skaller wrote:

But this doesn’t solve the problem.

I believe it solves Daniel’s original issue, i.e. not being able to
use SDL and C++11 (or, more precisely, a C++ dialect with features
from the C++11 standard) together.

But that’s really the problem: suppose Daniel, being sane,
does not want the gnu variant of C++ but prefers standards
conformance?

you have to also consider, many people building stuff on
top of SDL are not making games for their own playing.

They’re making code that other people can build
on other platforms too, using other compilers,
in fact they might be making a library not an application.

You are right in that it does not
solve your issue of “compile once, use everywhere”.

Its “write once, use everywhere”. I.e. my language source
code (Felix) should work on all platforms if you restrict yourself
to platform independent code.

Using SDL to do graphics, for example, should just “work” on
all platforms (that’s the theory and the hope anyhow :slight_smile:

The Felix compiler generates C++, conforming as close as possible
to ISO standards. There is configuration data on each installation,
including, for example, what compiler to use with which switches
to compile the generated C++.

SDL and other libraries may require configuration data for
a given platform. Felix provides a way to specify the include file
path (to find SDL.h) and switches so the linker will link SDL.

However the client (typically) choses the C++ compiler to use once when
installing my system and by default gcc is used on Linux with -std=c++
and clang on OSX with -std=c++11. (because that’s all I can test)
Internal run time libraries can also be built with C, however SDL
isn’t an internal library.

So MY situation is: my clients run on many platforms with many
compilers and there are rules about what they have to provide
in the way of the compiler: it has to have certain capabilities.
For example I recently changed the rules: stdint.h is no longer
optional. int32_t etc must be defined. Felix USED to try to calculate
a suitable alternative (as does SDL) but I gave up on that.

Another rule: the compiler must not do strict aliasing.
So Felix sets --fno-strict-aliasing switch because Felix itself
breaks strict aliasing rules left right and centre :slight_smile:

the point is: SDL is a mere library. A very good one, but its just
a library. It must not interfere with the client’s choice of compiler.
SDL is free to build itself any way it wants to ensure

(a) the library binaries are ABI compliant for the platform, and,
(b) the header files are ISO compliant for a suitably chosen standard

It is NOT free to interfere in the compliance requirements by adding
its own versions of functions it thinks are missing. This is the job
of the application or framework or client, it is NOT the job of SDL library.
[Technically its actually the job of the compiler vendor :]

Similarly SDL should NOT make architectural assumptions about the
client always making C code with static linkage including a "main"
function. I don’t. My own system is a framework, IT takes care of the
linkage model and framework requirements its own way.

This is NOT to say SDL should not ALSO provide some useful
frameworks for basic game development, but they should be
clearly separated from the library.

There are libraries around my framework isn’t compatible with
because these libraries think they own the world. One of the
worst culprits is Xlib which insists you run its event loop.
Felix runs its own event loop (you can write Xlib callbacks
or you can use threads, but you lose the fibration and other
features the Felix event loop provides).

Windows on the other hand does it right. The USER has to
write the event loop. In fact no event loop is required.

The thing I like about SDL is that it does NOT mandate an event loop,
it provides SDL_PumpEvents and SDL_PollEvents instead which allows
the client programmer to organise their own infrastructure. This is what makes
SDL such a great product.

So no matter how SDL is built, it has to be used with ANY C++ compiler
without modification and without changing the way it was built.

That is, through no fault of the SDL developers, impossible.

Look at the whole MinGW/g++ vs. MSVC++ story on Windows: the two C++
ABIs are incompatible.

Ok, I should have said “with the same ABI”. Obviously the binaries
should be ABI compliant, and you need a different binary library for each
ABI.

It’s the headers for a given ABI that are at issue here.

SDL’s job is to abstract system-specific details away, and as
Microsoft’s decade-long unwillingness to provide stdint.h shows,
differences between what C/C++ language standard is supported belong
into the category of “system-specific details”.

SDL’s job is to abstract systems specific Graphics, Audio, and
other details away NOT abstract aways problems in standard libraries.

if SDL wants to help with that fine, but such support should be cleanly
separated from the library. The client can then choose to use SDL’s
helper stuff OR do it themselves. Then if things mess up its THEIR
FAULT not SDLs.

For my system this means, if SDL stuff doesn’t work on some
platform and a client complains its MY problem to fix it.

If you give your
clients the power to change such system-specific details on a whim,
well, I guess you’ll have to add code that rebuilds SDL according to
whatever set of flags has been chosen this time around.

How can you do that if you’re using C++ and SDL builds by
"autoconf" magic for some variant of C?

Flexibility
doesn’t come for free.

i agree but the point is this: there’s no good reason for the current
broken design. There’s a right way to do this, and that’s to separate
out
the parts of the SDL that can help with adapting broken compilers
from the library.

The need to do this in modern contexts is clear. Already two people have
run into grief, one using C99 and one using gcc with -std=c++11.

As it happens I have NOT had any problems. I’m running OSX 10.6.8 using
gcc 4.2.something and clang 3.3 from SVN (bleeding edge) with LLVM’s
libraries (not gcc’s because they don’t support C++11 which i want to
migrate to eventually). i build plugins and static libraries and SDL
just works with all these combinations (including no SDL_main crud).

What I need is that if I put my code on Linux or Windows with some
compiler being used selected by the client, it will still work, and,
if it doesn’t because some requirement wasn’t met by ME creating
the binding, its MY job to fix it. SDL is denying me the authority
required to do that by taking responsibility for something outside
its scope. (i mean authority and responsibility in the sense
of programming by contract).

The last thing I want to do is have to modify SDL and bundle it
with Felix just to ensure I can remove functions from SDL that
are specified as part of an ISO standard, or specified as NOT
being part (and therefore are not allowed in headers).

I understand your argument and Sam’s but it is fallacious.
It worked well only when compilers, compiler standards, and
linkage models were not so diverse as they are today, when
an SDL app was just SDL and nothing else, whereas today
even a game will have a lot of other stuff in it such as a high
performance physics engine, networking, http interfacing,
and heap of other messy complex stuff. If all those things
also tried to fix broken compilers all hell would break loose.
SDL (the library I mean) shouldn’t try either.On 04/07/2013, at 10:45 AM, Ond?ej Ho?ek wrote:

On Thu, Jul 4, 2013 at 1:21 AM, john skaller <@john_skaller> wrote:


john skaller
@john_skaller
http://felix-lang.org

I believe it solves Daniel’s original issue, i.e. not being able to
use SDL and C++11 (or, more precisely, a C++ dialect with features
from the C++11 standard) together.

But that’s really the problem: suppose Daniel, being sane,
does not want the gnu variant of C++ but prefers standards
conformance?

SDL is a C only library, and this is one of the reason is so used around,
since it does not suffer of abi incompatibilities and a binary distribution
containing static/shared library or framweork usually works out of the box
without complaining about different compilers.

So, why it is so important this C+±11 thing?

Is the OP trying to compile SDL itself inside his project and forcing it to
use its project compiler settings?

If he is doing so probably he plans to mantain a particular version of SDL
with some custom patches, so modifying also SDL_stdinc.h to work with
c+±11 shouldn’t be a problem.

If the inclusion of SDL_stdinc inside his project break with c+±11 gcc
switch he can also use the workaround previously posted (#define
_SDL_stdinc_h before including SDL.h)…–
Bye,
Gabry

So, why it is so important this C+±11 thing?

Is the OP trying to compile SDL itself inside his project and forcing it to use its project compiler settings?

No, the OP is trying to simply use SDL, not build it.

The problem is it contains headers which conflict with environments
other than the one for which SDL was built.

One should be able to use SDL (not build it) with any abi compatible
compiler: gcc, g++, clang, clang++ with and appropriate language
modle (-std=c89, -std=c99, -std=c11, -std=gnu90, -std=c++, -std=c++1,
-std …) [All these options are available on OSX and Linux]

There are a lot of combinations. Most other libraries work this way.
SDL should too. if library A restricted these combinations too much,
and so did library B, and library C … eventually you’d find these libraries
couldn’t be used together at all.

In C land, this happened in the past with “bool”. Everyone defined
a boolean type and too many libraries thought THEY were God
and called it “bool” instead of LIBRARYXXX_bool. It only takes
two libraries defining bool to cause havoc, and havoc we had.

This is what SDL is doing. Its defining missing types and functions.

Only they’re NOT missing. They’re only missing in the environment
which autoconf decided to build the library (or whatever happens
to be used on Windows).

[If SDL actually makes real replacement functions the situation
is very much worse: that would lead to duplicate symbols linking.]On 04/07/2013, at 6:21 PM, Gabriele Greco wrote:


john skaller
@john_skaller
http://felix-lang.org

I think you got it wrong.

I’m not building SDL with that flag… I’m USING SDL with a project that
uses -std=C++11 flag.

I agree with all john skaller said. For me, it’s a problem that should be
discussed and fixed.

2013/7/4 Gabriele Greco <gabriele.greco at darts.it>>

I believe it solves Daniel’s original issue, i.e. not being able to

use SDL and C++11 (or, more precisely, a C++ dialect with features
from the C++11 standard) together.

But that’s really the problem: suppose Daniel, being sane,
does not want the gnu variant of C++ but prefers standards
conformance?

SDL is a C only library, and this is one of the reason is so used around,
since it does not suffer of abi incompatibilities and a binary distribution
containing static/shared library or framweork usually works out of the box
without complaining about different compilers.

So, why it is so important this C+±11 thing?

Is the OP trying to compile SDL itself inside his project and forcing it
to use its project compiler settings?

If he is doing so probably he plans to mantain a particular version of SDL
with some custom patches, so modifying also SDL_stdinc.h to work with
c+±11 shouldn’t be a problem.

If the inclusion of SDL_stdinc inside his project break with c+±11 gcc
switch he can also use the workaround previously posted (#define
_SDL_stdinc_h before including SDL.h)…


Bye,
Gabry


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Okay, this should be fixed in the latest RC candidate:
http://www.libsdl.org/tmp/download-2.0.php

Thanks!On Tue, Jul 2, 2013 at 11:15 AM, Daniel Guzm?n <daniel.guzman85 at gmail.com>wrote:

Hello!

I think there is something odd with this combination… or maybe i’m just
too stupid to figure it out :smiley:

I’m using Mingw to compile SDL2. The configure searchs for non-standard
functions like itoa and strdup, and found it. Then I compile it, and all OK.

When I try to compile my program against that SDL2 library without
-std=c++11 all goes flawless. But when I try to use the C++11 features and
then compile it with the -std=c++11 flag, the compiler can’t find the
non-standard functions like itoa or strdup that the SDL header have.

My solution was to modify the SDL configure file to force it to not found
any non-standard function, removing this:


for ac_func in malloc calloc realloc free getenv setenv putenv

unsetenv qsort abs bcopy memset memcpy memmove strlen strlcpy strlcat
strdup _strrev _strupr _strlwr strchr strrchr strstr itoa _ltoa _uitoa
_ultoa strtol strtoul _i64toa _ui64toa strtoll strtoull atoi atof strcmp
strncmp _stricmp strcasecmp _strnicmp strncasecmp sscanf snprintf vsnprintf
fseeko fseeko64 sigaction setjmp nanosleep sysconf sysctlbyname
do :
as_ac_var=$as_echo "ac_cv_func_$ac_func" | $as_tr_sh
ac_fn_c_check_func “$LINENO” “$ac_func” “$as_ac_var"
if eval test “x$”$as_ac_var”" = x"yes"; then :
cat >>confdefs.h <<_ACEOF
#define $as_echo "HAVE_$ac_func" | $as_tr_cpp 1
_ACEOF

fi
done


Now the SDL2 compile without using that functions from the compiler and my
program with the -std=c++11 flag set is building ok.

Well… This is not the solution of course.I Just want to let you know of
this problem and maybe you could bring a more elegant solution to all of
use trying to use C++11

Thanks !!


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org