SFML community forums

Help => General => Topic started by: MarcuzPwnz on February 14, 2013, 04:42:46 am

Title: C++11 and SFML 2.0
Post by: MarcuzPwnz on February 14, 2013, 04:42:46 am
I was working with the network module from SFML 2.0 this morning and tried to use the std::thread class that was introduced in C++11 and noticed that I could only use sf::Thread class from SFML.

Am I doing something wrong? or is C++11 not supported yet on SFML 2.0?

As a side note I'm on a Mac so I'm not sure if that's the issue.
Title: Re: C++11 and SFML 2.0
Post by: Gan on February 14, 2013, 04:56:07 am
You have to compile SFML using clang and the C++11 flag.
The flag needs to be for C and CXX.
I think it gets more specific in the compile for Xcode tutorial.
Title: Re: C++11 and SFML 2.0
Post by: MarcuzPwnz on February 14, 2013, 06:10:36 am
Where do I set flags? and are you talking about the tutorial found in the tutorials section of the site? or is it somewhere on the forums? :)
Title: Re: C++11 and SFML 2.0
Post by: Gan on February 14, 2013, 06:39:21 am
You set the flags when setting up the Cmake.

It's in the tutorial section for 2.0. Specifically Cmake.

http://www.sfml-dev.org/tutorials/2.0/start-osx.php
Title: Re: C++11 and SFML 2.0
Post by: MarcuzPwnz on February 14, 2013, 06:49:09 am
Thanks! :D

Is it known if SFML will ever just support C++11 out of the box like it does with the current version of C++?
Title: Re: C++11 and SFML 2.0
Post by: Gan on February 14, 2013, 07:28:56 am
C++11 only works on mac os 10.7+ I believe. That's probably why it isn't default.
Title: Re: C++11 and SFML 2.0
Post by: MarcuzPwnz on February 14, 2013, 07:38:13 am
I am running Mac OSX 10.7.5 and have my Xcode update to the latest version.  I can run C++11 features fine without SFML, just with SFML I can't seem to use C++11 :o
Title: Re: C++11 and SFML 2.0
Post by: Laurent on February 14, 2013, 08:07:36 am
SFML doesn't have to support C++11 for it to work in your own code. If it doesn't work, then there's probably something wrong in your setup or environment. Please describe the exact errors that you get.
Title: Re: C++11 and SFML 2.0
Post by: MarcuzPwnz on February 14, 2013, 08:30:43 am
Okay, I typed up the code under the Networking tutorial for SFML and included <thread>

#include <iostream>
#include <thread>
#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include "ResourcePath.hpp"

using namespace sf;
using namespace std;

int main(){
    RenderWindow window(VideoMode(800, 600), "SFML Networking - MarcuzPwnz");
   
    cout << "Would you like to be a server or client? (c/s)" << endl;
    char input = ' ';
    cin >> input;
    if(input == 'c'){
    /* CLIENT */
        TcpSocket socket;
        socket.connect("localhost", 1337);
       
        string message = "Hey, I'm the client!";
        socket.send(message.c_str(), message.size() + 1);
       
        char buffer[1024];
        size_t received = 0;
        socket.receive(buffer, sizeof(buffer), received);
        cout << "[Server]: " << buffer << endl;
            }else{
    /* SERVER */
        TcpListener listener;
        listener.listen(1337);
       
        TcpSocket socket;
        listener.accept(socket);
        cout << "Client connected: " << socket.getRemoteAddress() << endl;
       
        char buffer[1024];
        size_t received;
        socket.receive(buffer, sizeof(buffer), received);
        cout << "Client: " << buffer << endl;
       
        string message = "Welcome client!";
        socket.send(message.c_str(), message.size() + 1);
       
    }
   
    while(window.isOpen()){
       
       
        Event event;
        while(window.pollEvent(event)){
           
        }
    }
    return 0;
}

The code runs fine without #include <thread> on the second line, but with it I get errors.

Error: Lexical or Preprocessor Issue. 'thread' file not found.

Thanks for the reply. :)
Title: Re: C++11 and SFML 2.0
Post by: Laurent on February 14, 2013, 08:38:34 am
Your compiler is probably not configured properly.

What about this code, with the exact same setup (ie. don't create a new project to try it)?

#include <thread>

int main()
{
    return 0;
}
Title: Re: C++11 and SFML 2.0
Post by: MarcuzPwnz on February 14, 2013, 08:44:30 am
Yep, I still get an error with the code you posted above.

Any clue what I would be doing wrong with my setup?
Title: Re: C++11 and SFML 2.0
Post by: Laurent on February 14, 2013, 08:49:21 am
C++11 is probably not enabled. I can't help more, I'm not an OS X expert.
Title: Re: C++11 and SFML 2.0
Post by: MarcuzPwnz on February 14, 2013, 08:53:28 am
Hmm ok :/

Like I said, projects without SFML work with C++11 fine, just seems odd it stops working as soon as I link SFML.
Title: Re: C++11 and SFML 2.0
Post by: Laurent on February 14, 2013, 09:10:40 am
Linking SFML cannot produce preprocessor errors. These are two different worlds. So while adding SFML, you probably changed something else. Try to restart a project from scratch.
Title: Re: C++11 and SFML 2.0
Post by: MarcuzPwnz on February 14, 2013, 09:12:03 am
Sorry, my language isn't 100% correct.

Will do. :)
Title: Re: C++11 and SFML 2.0
Post by: MarcuzPwnz on February 14, 2013, 09:33:32 am
Okay I have found what causes the issue. :)

In my build settings I have this by default:
(http://i.imgur.com/8uCg8D5.png)

Which gives me errors on the SFML side of things:
(http://i.imgur.com/SHBrco5.png)

To fix it I set it to the compiler default:
(http://i.imgur.com/sSs2R4s.png)

Which causes me to lose C++11 compatibility:
(http://i.imgur.com/m4imM53.png)

Anyone know how to get around this?
Title: Re: C++11 and SFML 2.0
Post by: Gan on February 14, 2013, 02:36:20 pm
Recompile and install SFML with C++11 did the trick for me.
Title: Re: C++11 and SFML 2.0
Post by: Hiura on February 14, 2013, 04:06:22 pm
You should use the new templates for Xcode (i.e. do as Gan said). They now support C++11 setup painlessly.
Title: Re: C++11 and SFML 2.0
Post by: MarcuzPwnz on February 14, 2013, 06:53:58 pm
Thanks for the help guys :)

So to get the new templates for Xcode I have to recompile SFML with C++11, any tutorials on how to do this?

Is this what you mean? http://www.sfml-dev.org/tutorials/2.0/compile-with-cmake.php
Title: Re: C++11 and SFML 2.0
Post by: Gan on February 14, 2013, 09:23:12 pm
That's it. But make sure to set is as a Unix Makefile, clang compiler and set the c++11 flags like it says on the Xcode page.

Then do sudo make install on the make file and boom. Installed. Then you can open Xcode and make a new SML app from the template.
Title: Re: C++11 and SFML 2.0
Post by: MarcuzPwnz on February 15, 2013, 10:03:52 pm
Okay, I followed the tutorial and have configured and generated with CMake.

I have the following files but I'm finding the rest of the tutorial hard to follow, I'm really lost on what to do next.

(http://i.imgur.com/uCXXck4.png)

Also I got no options about c++11 flags or anything like that, so I may not have even done it right. :/
Title: Re: C++11 and SFML 2.0
Post by: Gan on February 15, 2013, 10:17:48 pm
If you wanna be super patient, in like 3-4 hours I'll be able to make a video tutorial on how I get mine working.
Title: Re: C++11 and SFML 2.0
Post by: MarcuzPwnz on February 15, 2013, 10:24:30 pm
Sure man, that would be awesome! :D

Edit: Trying to be patient and all but if it's possible are you able to provide a download to SFML 2.0 compiled to work with C++11? :)
Title: Re: C++11 and SFML 2.0
Post by: Gan on February 16, 2013, 03:58:14 am
There we go.

http://www.youtube.com/watch?v=OyUb8bDP2mw&feature=youtu.be

Compiled: http://cl.ly/3P1y0z3m3o30
But the process in the video auto installs it for you.
Title: Re: C++11 and SFML 2.0
Post by: MarcuzPwnz on February 16, 2013, 04:52:23 am
Followed it exactly.

Once I got to the sudo make install in terminal I got errors.

I pasted them to Pastebin: http://pastebin.com/4ZnpKFnm

 :-\
Title: Re: C++11 and SFML 2.0
Post by: Gan on February 16, 2013, 05:07:50 am
:/ I wouldn't know how to fix that.

Are you using the clang compiler? What's your OS version?
Title: Re: C++11 and SFML 2.0
Post by: MarcuzPwnz on February 16, 2013, 05:19:06 am
Drats, should I just instal the download you provided?

Mac OSX: 10.7.5
My compiler is the one that comes default with the latests Xcode.
'clang' does work as a command in terminal though so I'm guessing that's what I'm using.
Title: Re: C++11 and SFML 2.0
Post by: Gan on February 16, 2013, 05:22:23 am
I google searched some of the errors in your log. Came up with this:
http://stackoverflow.com/questions/14263735/c-std-c11-stdlib-libc-giving-errors-on-osx-lion

It seems that your clang version may be too old. If you open Xcode, go to Preferences -> Downloads, you may be able to install Command Line tools. That'd upgrade it. (Also make sure you have the latest Xcode, it's free!)

If you want to check your clang version, do this in terminal:
Quote
c++ --version
Title: Re: C++11 and SFML 2.0
Post by: MarcuzPwnz on February 16, 2013, 05:28:32 am
Thanks!

I'll go and install the command line tools and try again :)

Also I recently got the latest version of Xcode so I could use C++11 and the feedback I got from terminal was:
Apple clang version 3.1 (tags/Apple/clang-318.0.61) (based on LLVM 3.1svn)
Target: x86_64-apple-darwin11.4.2
Thread model: posix
 
Title: Re: C++11 and SFML 2.0
Post by: Gan on February 16, 2013, 05:34:23 am
Woh there's your problem. Mine's 4.1.
Title: Re: C++11 and SFML 2.0
Post by: MarcuzPwnz on February 16, 2013, 05:40:54 am
Oh jeez, only slightly outdated.. o.o

I have quite a slow connection due to where I live but once the tools have downloaded and I can run cmake and all that again I'll tell you how it went.

Thanks so much for the help, I really appreciate it. :D
Title: Re: C++11 and SFML 2.0
Post by: MarcuzPwnz on February 16, 2013, 08:38:12 am
Finally got it working thanks to you :)

I couldn't just run my SFML project straight away, I had to set the search path and add the linked framework and library but it still works :D

Thanks again, you were a massive help!
Title: Re: C++11 and SFML 2.0
Post by: Alec on May 03, 2013, 12:58:37 pm
Continuing on this theme.  I'd like to build SFML 2.0 on Linux, initially w/ gcc, and ideally w/ make.  I asked on a separate thread about this, and the helpful reply pointed me towards a route using CMake.  Fine.  But I'd like further help on this wrt the use of C++11.  That is, my app code would be built w/ C++11.  Should SFML be built as C++11 (and if so how do I supply the parameter(s) for this?  And for all the libs that SFML uses, should they also be built and installed using a C++11 compiler; or would installation of the dependent libs from packages (probably built w/ C and/or C++ elements)  a) be well-defined, and expected to work, or b) work in practice, but in theory use undefined behaviour, or c) be unreliable at best?

TIA,

Alec
Title: Re: C++11 and SFML 2.0
Post by: Laurent on May 03, 2013, 01:13:20 pm
Isn't -std=c++11 the default, starting from gcc 4.7? i.e. you don't have to do anything to get C++11 support.
If not, you must add -std=c++11 to the CMAKE_CXX_FLAGS CMake variable.

SFML only uses C dependencies, so there's no C++11 problem with them.
Title: Re: C++11 and SFML 2.0
Post by: Alec on May 03, 2013, 02:56:51 pm
Thanks, but my understanding is that although available, C++11 is not the default.  (See: http://gcc.gnu.org/onlinedocs/gcc/Standards.html , which states: "The default, if no C++ language dialect options are given, is -std=gnu++98."  ISTR seeing - but can't find - another authoritative quote stating, effectively, that it was not likely to become so anytime soon.)

And I'm concerned about the binary compat issues.  See e.g.:

http://gcc.gnu.org/ml/gcc-help/2013-03/msg00035.html

http://gcc.gnu.org/wiki/Cxx11AbiCompatibility

Alec