Saturday, February 2, 2013

Building SFML 2 with C++11 on OS X

It's not a very difficult thing to do, but clear, simple instructions can be useful. Thankfully, there's a good Getting Started guide with official documentation for building SFML (and you really should read it). If, after reading that, you want things spelled out even more... here you go.

Step 1: Prepare Xcode

Make sure you have Xcode downloaded and installed. Make sure you have your command line tools set up. You do this by doing Xcode -> Preferences -> Downloads and making sure you have Command Line Tools downloaded and installed.

Step 2: Prepare Cmake

I won't go into what Cmake is, but download and install the latest version. SFML needs it.

Step 3: Download SFML 2

Go to SFML's download page and download the SFML source code (either the SFML 2.0 source code or the latest snapshot of the Git repository).

Step 4: Open a Terminal

Hopefully you've got the Terminal sitting on your dock, but in case not it can be found under Applications -> Utilities -> Terminal.app.

Step 5: Get Ready to Build

Next, do the following in your terminal:

cd <sfml folder>
mkdir build
cd build

In the first line, replace <sfml folder> with the path to the folder where you downloaded and extracted the SFML source to. mkdir build just creates the directory build where we can do our dirty work, and cd build moves into that folder.

Step 6: Run Cmake

Now run the following (you can copy and paste):

cmake -G 'Unix Makefiles' \
      -DCMAKE_OSX_ARCHITECTURES='i386;x86_64' \
      -DCMAKE_CXX_COMPILER='/usr/bin/clang++' \
      -DCMAKE_C_COMPILER='/usr/bin/clang' \
      -DSFML_BUILD_FRAMEWORKS='ON' \
      -DSFML_INSTALL_XCODE4_TEMPLATES='ON' \
      -DCMAKE_CXX_FLAGS='-stdlib=libc++' \
      -DCMAKE_C_FLAGS='-stdlib=libc++' ../

This runs Cmake and tells it to do a few things. Particularly, it's saying (going down the options one by one):

  • Use Makefiles instead of Xcode
  • Build for x86 and x86_64 (i.e. 32-bit and 64-bit)
  • Use clang++ for compiling the C++ code
  • Use clang for compiling the C code
  • Build as frameworks (instead of just dynamic libraries)
  • Install the Xcode SFML templates (so you can easily create SFML projects)
  • Link to the libc++ standard library implementation (necessary for C++11)

This doesn't actually build SFML, but it sets everything up so we can run the next two commands...

Step 7: Build!

Now run:

make -j4

make is a fancy program that will actually do all the building of SFML for you. The -j4 option just says "Use 4 threads to do this." If you don't want to use multiple threads to build (which is sloooow), you can omit this option. If you want to use more threads, like 8, you can change it to -j8 (I personally use 8 threads).

Step 8: Install!

The last step:

sudo make install

This will prompt you for your password. Give it your password. It'll place all the SFML files in their final resting place on your system, and it needs some elevated privileges to write the files to the necessary folders. If you're curious where it's writing the files to, just look at the output. Anyway, now you're done!

Bonus Step: Making an Xcode Project

What good is building and installing SFML if you don't use it? We'll set up a simple project now. Open Xcode. Then create a new project. Then select the SFML App template:

In the next screen, give your project a name. Make sure to say you want to use C++11! The "C++ Compiler and Standard Library" option does not default to Clang and C++11. You have to set this yourself. Assuming you followed my instructions and made SFML a universal framework for 32-bit and 64-bit, the rest of the options can stay the same.

You should now be able to run the created project!

16 comments:

  1. This tutorial would be more helpful if it included the library dependencies and how to install them. I had to install PCL1.6 and then Eigen, but Eigen is proving difficult to "install" and find by CMAKE - really wish there was a non non-sense "layman" version of getting CMAKE to find Eigen so SFML will build.

    ReplyDelete
    Replies
    1. For one, I do provide binaries for SFML for OS X, should that be of any interest. However, if you really want to build it yourself and are struggling with dependencies, I'm completely puzzled. SFML doesn't depend on PCL or Eigen (and I've never downloaded, built, or installed them). Are you sure you aren't mixing things up?

      Delete
  2. I followed the tutorial and it almost worked, except when I launch the Xcode Template "SFML App" I have a build error saying:

    "ditto: can't get real path for source
    couldn't copy /Library/Frameworks/freetype.framework to /Users/vanpet/Library/Developer/Xcode/DerivedData/engine-aflrprlylygpwtcgjaleylrwvizo/Build/Products/Debug/engine.app/Contents/Frameworks/freetype.framework
    Command /bin/sh failed with exit code 2"

    Any idea what is happening?

    ReplyDelete
    Replies
    1. Interesting... I'll look into it! My computer is currently gettings its logic board replaced, so I won't be able to try anything until tomorrow, but I promise I will when I get it back.

      From what I'm understanding from that error, it sounds like the FreeType frameworks didn't install correctly. Try doing:

      ls /Library/Frameworks | grep freetype

      And tell me what it prints out. If it prints out nothing, it means you're missing the FreeType framework (which would be weird, as the install scripts are supposed to install it).

      Also, can I ask what version of OS X and Xcode you're using?

      Delete
    2. Alright, I just got my computer back today (a day early), and I've completely removed the SFML frameworks (and its dependencies) and followed this tutorial from the start, and everything is working just fine for me. My setup is:

      OS X: 10.8.3
      Clang: Apple LLVM version 4.2 (clang-425.0.24) (based on LLVM 3.2svn)
      Xcode: 4.6.1
      SFML: 5c46daa (the latest version from the git master branch, at time of writing)

      What does your setup look like?

      Delete
  3. My setup is the same: OS 10.8.3
    Xcode: 4.6.1
    SFML: 5c46daa
    Clang: Apple LLVM version 4.2 (clang-425.0.27) (based on LLVM 3.2svn)
    Target: x86_64-apple-darwin12.3.0
    Thread model: posix

    Grep freetype gives nothing, as you said... indeed when I use finder, I don't see any trace of freetype... but I think I remember I saw it once... It must have been erased by mistake... I have no idea how to reinstall that? Does it come with SFML or do I have to download it somewhere else?

    (the weird thing is now that I tried again to build it gives me an error than FreeType isn't found at all)

    ReplyDelete
    Replies
    1. Weird. FreeType should be built already in the SFML (it comes with SFML, you can find it in under SFML/extlibs/libs-osx/Frameworks). I'm not sure how you downloaded the source. If you checked it out with git, you can do a "git status" to see if you've changed anything. If you see "freetype.framework" under the "SFML/extlibs/libs-osx/Frameworks" folder, you can just run "sudo make install" again and it should, in theory, copy it into place.

      You can also try downloading the SFML source again to get a clean copy and rebuilding it, just in case your download of SFML is messed up.

      Delete
  4. Ok, I'll try that and keep you informed. I indeed use GitHub for the latest source...

    ReplyDelete
    Replies
    1. Update: Nothing worked so I switched to my old windows PC. Maybe I'll try again on my Mac some day, but I didn't really like Xcode anyway.

      Delete
    2. Thanks for posting an update!

      It would be really nice to try and figure this issue out, as it seems you aren't the only one having an issue. If you ever get the time, it would be very helpful if you created a thread on the SFML forums (so more people see it) detailing your process. If something is indeed wrong in SFML it would be ideal to get it figured out and fixed.

      Delete
  5. I'm getting the same error when following the instructions on SFML-Dev.org.. FreeType isn't installed in the /Library/Frameworks either. Wonder if the GitHub source is borked?

    ReplyDelete
    Replies
    1. Hmmm, this is puzzling! I build from source all the time and have never had this problem. Try checking cmake_install.cmake and looking for a line that looks like:

      FILE(INSTALL DESTINATION "/Library/Frameworks" TYPE DIRECTORY FILES "/Users/mjbshaw/Projects/RetinaSFML/extlibs/libs-osx/Frameworks/freetype.framework")

      But where "/Users/mjbshaw/Projects/RetinaSFML" is your path to the SFML source. Make sure that /extlibs/libs-osx/Frameworks/freetype.framework exists.

      Beyond that though, it's tough to say. It might be easier to discuss things on the SFML forums (and maybe get feedback from others), so I'd suggest making a post on the SFML forums detailing your exact process and error: http://en.sfml-dev.org/forums/index.php

      Delete
  6. Just have one error: "Shell Script Invocation Error, Command /bin/sh failed with exit code 2"

    Any idea?

    ReplyDelete
  7. Hey, I followed this (and many other) tutorials, word for word, and I still get >20 errors when I open the template. Even once i delete all my code in the main block, theres a linker error. Any suggestions?

    ReplyDelete
    Replies
    1. Can you post:

      The exact errors messages
      Your version of OS X
      Your version of Xcode
      The compiler you are using (both to build SFML and to build your project)
      Your compiler settings

      Delete

Note: Only a member of this blog may post a comment.