SFML и Linux

Введение

Этот урок первый который вы должны прочитать если используете SFML на Linux. Здесь описано как установить SFML и скомпилировать проект который его использует.

Установка SFML

Есть несколько способов установить SFML на Linux:

  • Установка из репозитория вашего дистрибутива
  • Загрузить скомпилированный SDK и вручную скопировать файлы
  • Загрузить исходники, скомпилировать и установить

Первый способ предпочтительнее; если версия SFML которую вы хотите установить доступна в официальном репозитории ,  значит установите SFML используя ваш пакетный менеджер . К примеру, в Debian вы должны выполнить следующую команду:

Option 3 requires more work: you need to ensure all of SFML’s dependencies including their development headers are available, make sure CMake is installed, and manually execute some commands. This will result in a package which is tailored to your system.
If you want to go this way, there’s a dedicated tutorial on building SFML yourself.

Finally, option 2 is a good choice for quick installation if SFML is not available as an official package. Download the SDK from thedownload page, unpack it and copy the files to your preferred location: either a separate path in your personal folder (like/home/me/sfml), or a standard path (like /usr/local).

If you already had an older version of SFML installed, make sure that it won’t conflict with the new version!

Компиляция SFML программы

In this tutorial we’re not going to talk about IDEs such as Code::Blocks or Eclipse. We’ll focus on the commands required to compile and link an SFML executable. Writing a complete makefile or configuring a project in an IDE is beyond the scope of this tutorial — there are better dedicated tutorials for this.
If you’re using Code::Blocks, you may refer to the Code::Blocks tutorial for Windows; many things should be similar. You won’t have to set the compiler and linker search paths if you installed SFML to one of your system’s standard paths.

First, create a source file. For this tutorial we’ll name it “main.cpp”. Put the following code inside the main.cpp file:

 

Now let’s compile it:

In case you installed SFML to a non-standard path, you’ll need to tell the compiler where to find the SFML headers (.hpp files):

Here, <sfml-install-path> is the directory where you copied SFML, for example /home/me/sfml.

You must then link the compiled file to the SFML libraries in order to get the final executable. SFML is made of 5 modules (system, window, graphics, network and audio), and there’s one library for each of them.
To link an SFML library, you must add “-lsfml-xxx” to your command line, for example “-lsfml-graphics” for the graphics module (the “lib” prefix and the “.so” extension of the library file name must be omitted).

If you installed SFML to a non-standard path, you’ll need to tell the linker where to find the SFML libraries (.so files):

We are now ready to execute the compiled program:

If SFML is not installed in a standard path, you need to tell the dynamic linker where to find the SFML libraries first by specifying LD_LIBRARY_PATH:

If everything works, you should see this in a new window:

start-linux-app

Буду благодарен, если поделитесь: