SFML community forums

Help => General => Topic started by: shadowmouse on November 13, 2015, 03:27:47 pm

Title: Linking on Linux
Post by: shadowmouse on November 13, 2015, 03:27:47 pm
I've just switched from using Windows 10 to using Ubuntu and installed libsfml-dev using the Ubuntu Software Centre. However I'm confused, as the package that is installed contains only header files and when I attempt to link to the libraries in codeblocks, it says it cannot find -lsfml-graphics-s etc. Am I missing something? Do you have to compile the library yourself if you download it from the software centre (option 1 in the tutorial)? Sorry if it's a stupid question.
Title: Re: Linking on Linux
Post by: SeriousITGuy on November 13, 2015, 03:38:05 pm
I don't know what's exactly in the package libsfml-dev, but normally a -dev package only contains the developement headers.

It is best you compile it yourself from source, it is easy and since version 2.2 very straight forward. This way you have development headers and library files correctly installed.
Title: Re: Linking on Linux
Post by: shadowmouse on November 13, 2015, 03:58:40 pm
Okay, will do. I've done that before on windows so I presume the process is the same using CMake on Linux? Also, do I compile the code in the dev package I downloaded, or do I download the github code linked on the SFML downloads page?
Title: Re: Linking on Linux
Post by: Rosme on November 13, 2015, 04:26:53 pm
Clone the repository and build that one. Most of the time, the package in the official packages are out-dated anyway.
Title: Re: Linking on Linux
Post by: Spirro on November 13, 2015, 09:15:34 pm
I've just switched from using Windows 10 to using Ubuntu and installed libsfml-dev using the Ubuntu Software Centre. However I'm confused, as the package that is installed contains only header files and when I attempt to link to the libraries in codeblocks, it says it cannot find -lsfml-graphics-s etc. Am I missing something? Do you have to compile the library yourself if you download it from the software centre (option 1 in the tutorial)? Sorry if it's a stupid question.

Generally speaking, on linux, when you install a dev package it installs the headers and binaries, but seems as though that didn't happen in your case.  You can look at the dev package you installed and find the matching binary package and install it as well, or as Roseme suggested, compile and install it yourself.  Information on that is found here:
http://www.sfml-dev.org/tutorials/2.3/start-linux.php

and here:

http://www.sfml-dev.org/tutorials/2.3/compile-with-cmake.php

Just be sure to fully read the first page as installing from source tends to put libraries in non-standard locations, so to use what you have compiled you will need to set environment variables properly so the system can find what it needs to run your programs.
Title: Re: Linking on Linux
Post by: shadowmouse on November 13, 2015, 10:58:23 pm
Well as I'm used to Windows I have no problem with setting it up to find the libraries wherever I installed them. Thanks for the info and links.
Title: Re: Linking on Linux
Post by: shadowmouse on November 14, 2015, 05:27:36 pm
I've installed the libraries using cmake and checked and it made all the files and things, but Codeblocks says it can't find -lopengl32 -lwinmm or -lgdi32, and I can't find any of those in the Ubuntu package manager and CMake built it, indicating that I had installed all the necessary libraries. What am I doing wrong?
EDIT: I forgot to say, but I'm linking statically and I built the project in CMake as a Codeblocks- Unix Makefile.
Title: Re: Linking on Linux
Post by: Spirro on November 14, 2015, 07:49:04 pm
I've installed the libraries using cmake and checked and it made all the files and things, but Codeblocks says it can't find -lopengl32 -lwinmm or -lgdi32, and I can't find any of those in the Ubuntu package manager and CMake built it, indicating that I had installed all the necessary libraries. What am I doing wrong?
EDIT: I forgot to say, but I'm linking statically and I built the project in CMake as a Codeblocks- Unix Makefile.

I'm guessing you are using a 64 bit install.  With your system saying it needs opengl32 and gdi32 you will have to install the 32bit versions.  For opengl it should be the mesa 32 bit drivers.  gdi I'd have to look(on windows atm)  I forget what winmm was when I installed dependencies, but if you install catfish from your repo you can use it as a gui to search your computer for those files to see if they are actually there and you need to export links to them if they are.
Title: Re: Linking on Linux
Post by: shadowmouse on November 14, 2015, 08:05:54 pm
Yes I do have the 64 bit install, which I hadn't realised would be a problem. I'll have a look into catfish, and what do you mean by the mesa 32 bit drivers?
Title: Re: Linking on Linux
Post by: Spirro on November 14, 2015, 10:09:37 pm
Yes I do have the 64 bit install, which I hadn't realised would be a problem. I'll have a look into catfish, and what do you mean by the mesa 32 bit drivers?

Some programs on linux require their 32 bit versions to run.  For example, I don't know if you use steam on linux or not, but the client is 32bit so even on a 64 bit install it pulls in 32 bit versions of several libs so it will work properly.  Mesa is one of the video driver sets and it has two versions.  64 bit and 32 bit.  From what you last posted as needing, opengl32 and gdi32 that tells me you need the 32 bit versions of those libs.  You'll just have to check your repo for 32 bit versions of those drivers, and install them as well.  Again, I'm guessing you need the 32 bit mesa drivers, but check your hardware to be sure of what particular video driver you need.  For gdi do the same.
Title: Re: Linking on Linux
Post by: shadowmouse on November 14, 2015, 10:33:35 pm
I found a guide to getting opengl working and installed the mesa drivers and apparently on Linux it's just called GL, so I replace opengl32 in the linking options with GL and it has got rid of the can't find -lopengl32 message, but I can't find anything about getting gdi or winmm.
Title: Re: Linking on Linux
Post by: dabbertorres on November 14, 2015, 11:00:47 pm
gdi and winmm are Windows only.
Title: Re: Linking on Linux
Post by: Spirro on November 14, 2015, 11:19:11 pm
gdi and winmm are Windows only.

I never had the problems shadowmouse is having, but if the above is the case then imo shadowmouse should toss his compilation from before and redo it making 100% sure he's correctly followed instructions on the pages I linked earlier in the thread.

Edit:  Also be sure the proper toolchain is being used for compilation.  I'm sure it was, but if windows versions of libs are being asked for then perhaps a MinGW crosscompilation tool chain was used?  I don't know, but check something along those lines as well.
Title: Re: Linking on Linux
Post by: shadowmouse on November 14, 2015, 11:23:12 pm
Firstly, I've been working off those and going over them carefully, I'm fully aware of how precise you have to be. Secondly, I've got rid of gdi and winmm from the linker and that's got rid of those errors, I now have only a single error which is undefined reference to XGetXCBConnection. I've gone back through the libraries that you have to install for Linux and rechecked all the ones to do with XCB and I'm fairly sure I'm not missing any. Any ideas? And thanks everyone for all the help, I know how annoying the constant I can't set it up posts can be.
Title: Re: Linking on Linux
Post by: Spirro on November 14, 2015, 11:26:58 pm
Firstly, I've been working off those and going over them carefully, I'm fully aware of how precise you have to be. Secondly, I've got rid of gdi and winmm from the linker and that's got rid of those errors, I now have only a single error which is undefined reference to XGetXCBConnection. I've gone back through the libraries that you have to install for Linux and rechecked all the ones to do with XCB and I'm fairly sure I'm not missing any. Any ideas? And thanks everyone for all the help, I know how annoying the constant I can't set it up posts can be.

A quick search found this: http://manpages.ubuntu.com/manpages/maverick/man3/XGetXCBConnection.3.html

Quote
The  XGetXCBConnection  function  returns the XCB connection associated
       with an Xlib  Display.   Clients  can  use  this  XCB  connection  with
       functions  from  the  XCB  library,  just  as  they  would  with an XCB
       connection created with XCB.  Callers of this  function  must  link  to
       libX11-xcb and a version of Xlib built with XCB support.

Maybe that helps.
Title: Re: Linking on Linux
Post by: shadowmouse on November 14, 2015, 11:33:42 pm
Well I've got libx11-xcb1, libx11-xcb1dbg, libx11-xcb-dev and libxcb1 installed according to my package manager, as well as a load of the extensions and a few other ones as well. I can't find anything else in the package manager under either xcb, x11, x11-xcb or xlib that might be helpful.
Title: Re: Linking on Linux
Post by: Spirro on November 14, 2015, 11:38:34 pm
If you read the quote I posted again it states you have to link against:
Quote
libX11-xcb and a version of Xlib built with XCB support

Are you checking to make sure your libraries match both criteria?
Title: Re: Linking on Linux
Post by: shadowmouse on November 14, 2015, 11:44:43 pm
Yep, just checked and they all mention both xlib and xcb.
Title: Re: Linking on Linux
Post by: Spirro on November 14, 2015, 11:48:26 pm
I don't think I have anything else I can add here then, unless what I am reading is that you need to link to 2 libraries, not just one.  That's something you'll need to investigate to see if there is another lib file to add to your linker settings from one of those two areas.
Title: Re: Linking on Linux
Post by: Spirro on November 15, 2015, 01:57:02 am
Ok so being the anal personally I am I just had to investigate more.  I remembered trying to static link on linux before so I tried again and of course failed.  I investigated a lot further and here is where I am stopping for now after looking at it a couple of hours.  I do no use an ubuntu variant anymore.  Previously I used mint, but run arch now.  I tried staticly compiling the source on this page:
sfml-graphics-s;sfml-window-s;sfml-system-s;OSMesa;GL;freetype;xcb-image;jpeg;udev;pthread;xcb-randr;xcb;X11;X11-xcb

I do get it to compile and link, but not work entirely.  I get the following message from the command console:

Quote
Warning: The created OpenGL context does not fully meet the settings that were requested
Requested: version = 1.1 ; depth bits = 0 ; stencil bits = 0 ; AA level = 0 ; core = false ; debug = false
Created: version = 0.0 ; depth bits = 24 ; stencil bits = 0 ; AA level = 0 ; core = false ; debug = false
sfml-graphics requires support for OpenGL 1.1 or greater
Ensure that hardware acceleration is enabled if available

I didn't investigate further because I'm hungry and it's been a while so stopping to eat and may look at it later tonight.  But for your information, this is what I send to the linker to get it to compile:

Quote
sfml-graphics-s;sfml-window-s;sfml-system-s;OSMesa;GL;freetype;xcb-image;jpeg;udev;pthread;xcb-randr;xcb;X11;X11-xcb

Hopefully this is enough information for someone better informed than I to point out the problem.  My guess is I need to check and see if I have hardware accelleration enabled and check to see why when I link I am using opengl 0.0(if that's actually true?).

Edit:  Forgot to mention that the window opens and displays at the settings dictated, but the shape is not displayed.  Hence the notation in the command window by SFML.
Title: Re: Linking on Linux
Post by: shadowmouse on November 15, 2015, 11:59:17 am
I linked all the libraries that you have done (I had previously only linked the ones that it says to in the CodeBlocks tutorial), and now this has happened:

(click to show/hide)

These are my linker settings: (http://s4.postimg.org/dm6i830pp/linker_Settings.png)
Title: Re: Linking on Linux
Post by: zsbzsb on November 15, 2015, 12:57:05 pm
And what is the exact problem now? (post complete error messages)
Title: Re: Linking on Linux
Post by: shadowmouse on November 15, 2015, 01:13:11 pm
I did, it's under the spoiler
Title: Re: Linking on Linux
Post by: dabbertorres on November 15, 2015, 07:20:11 pm
You've mixed 32bit and 64bit libraries at some point it looks like. Previously at some point though it seems. I'd do a full clean and rebuild.
Title: Re: Linking on Linux
Post by: Spirro on November 15, 2015, 07:27:28 pm
First, let me restate I am running arch so to get things to work some, if not all, of the linker options I listed before will have different names on ubuntu distibutions.  The method I used to get things as far as I did was to compile with options:
Quote
sfml-graphics-s;sfml-window-s;sfml-system-s;freetype;jpeg;
I believe(booted to windows on the reply.)  The one difference is I probably had a mesa lib after sfml-system-s.  What would happen here  are lots of unresolved references so I would grab the one at the top of the list and search on it.  Once I found what library that reference belonged to I would include it by using this page:
http://www.sfml-dev.org/tutorials/2.3/compile-with-cmake.php
as reference to give me a good guess at where in the link order to put the added library for linking.  Now the shape was not drawn properly on my last attempt last night, but the program compiled and gave me a symptom to look in to to fix it.

Second, those errors look a lot like one of the beginning points I made in that it seems the wrong toolchain is being used.  i386 architecture tends to be 32bit and the linker is, I think, trying to use a 32bit lib with 64bit libs.  That's something you need to look in to.  If you can, start from scratch and use the method above to see if you can follow the chain of errors to figure out the right libs to include and see if that works out, but you do still need to look at the tool chain.

Personally I never liked any full ubuntu distributions.  If you like that one then get more info on it.  Particularly information on compiling from source and linking.

There is also a tool for linux called 'ldd'  look it up.  With that tool you can run it on the various SFML .so files and it will list out the dependencies that library has.  That will help you figure out what you need to link.

Edit:  dabbertorres posted while I was typing this up and backed up my thoughts that you need to check your tool chain and linked libs to find out what they are doing.
Title: Re: Linking on Linux
Post by: shadowmouse on November 15, 2015, 07:30:08 pm
That seems to have got it working (full rebuild) except for the fact that I was relying on C++14, which the version of g++ that came with Ubuntu doesn't support, so I'll have to find out how to update that (EDIT: found out how) and then rebuild the libraries using CMake. Thanks for the information on ldd Spirro. Would it be possible to get the fact that winmm and gdi32 are only necessary on Windows and that all the other libraries have to be linked put into the codeblocks tutorial, seeing as codeblocks is multiplatform so I don't think it's right to make the tutorial only for Windows.
Title: Re: Linking on Linux
Post by: Spirro on November 15, 2015, 08:11:51 pm
That seems to have got it working (full rebuild) except for the fact that I was relying on C++14, which the version of g++ that came with Ubuntu doesn't support, so I'll have to find out how to update that (EDIT: found out how) and then rebuild the libraries using CMake. Thanks for the information on ldd Spirro. Would it be possible to get the fact that winmm and gdi32 are only necessary on Windows and that all the other libraries have to be linked put into the codeblocks tutorial, seeing as codeblocks is multiplatform so I don't think it's right to make the tutorial only for Windows.
winmm and gdi32 ARE Windows only.  If you want the tutorial updated for linux you will need to learn how to make a fully working compile on your distribution and submit it for review to one of the maintainers of SFML and see what happens.

I believe the reasoning behind not providing a library listing for static linking on linux is that static linking on linux is more frowned upon than on any other OS.

One of the big reasons I did not like using the ubuntu distributions is that the tool chains were a few generations behind the most current up to date tool chain(speaking mostly of gcc here, but also cmake, git, g++, etc...).  That's why I moved to arch.  There are tutorials out there to upgrade the default gcc compiler chain, but I never got them to fully work properly after upgrading.

And if you do not mind, please post what you linked to get a fully working static link on ubuntu.  I got it almost fully working.  I just need to investigate how opengl is being linked to fix the display of the 'shape' in the example tutorial code.
Title: Re: Linking on Linux
Post by: shadowmouse on November 15, 2015, 08:18:17 pm
I've got it working now: this is the build log
(click to show/hide)
, and these are my linking settings: (http://s10.postimg.org/5fs1stdix/linker_Settings1.png)Please tell me if you need anything else. I had to do quite a lot of terminalling to finally get it to switch to g++-5, but it's finally compiling with -std-c++14.
Title: Re: Linking on Linux
Post by: Spirro on November 15, 2015, 08:31:40 pm
Thank you for posting that.  It looks to me like you didn't change your linker libs from the previous post you made so the difference between arch and ubuntu is my problem.  I'll investigate it further later.

Last thing I would say is you seem to want to make a guide for using CodeBlocks.  I personally don't like CodeBlocks and if I need to use an IDE on linux I use CodeLite for a couple of reasons.

For one, CodeLite's abilities are less intrusive to me than those of CodeBlocks.  Talking about things like code completion mainly.

Second, in order to use CodeLite you need to be more familiar with your tool chain.  To me this is a good thing.  CodeBlocks automates a lot of the tool chain functions by making checkboxes to turn on/off options.  For CodeLite you need to know the command line usage of the tool chain and put those commands into CodeLite's compilation settings.  Imo this is better.  Knowing your tool chain and how it operates helps immensely in diagnosing problems that come up through compilation and allows you to use the command line when small code or quick and dirty testing code is created and a full IDE just isn't necessary.

The CodeLite does maintain a PPA for ubuntu distributions so you should be able to easily add that to your repository list to get their current build to have a look at it.
Title: Re: Linking on Linux
Post by: shadowmouse on November 15, 2015, 08:35:21 pm
All I was saying is that there is a guide for CodeBlocks which lists what libraries you have to link to when linking statically and I was just asking that it be updated to say things like (Windows only) next to winmm and gdi32 or putting things like X11-xcb (Linux only). I just think that a library for a multiplatform IDE should give information for more than one platform.
Title: Re: Linking on Linux
Post by: jcowgill on November 17, 2015, 12:26:00 am
I've just switched from using Windows 10 to using Ubuntu and installed libsfml-dev using the Ubuntu Software Centre. However I'm confused, as the package that is installed contains only header files and when I attempt to link to the libraries in codeblocks, it says it cannot find -lsfml-graphics-s etc. Am I missing something? Do you have to compile the library yourself if you download it from the software centre (option 1 in the tutorial)? Sorry if it's a stupid question.

Yes libsfml-dev itself only contains the header files, but when you installed it it should have pulled in the library packages (libsfml-window2.3v5 etc) which contain the actual libraries. However, libsfml-dev does not contain any static libraries which are what you are trying to use here. If you remove the "-s" from your libraries it should work out of the box (hopefully).

If there is demand for it, I can probably add the static libraries to libsfml-dev. It's just a little "unclean" since I would have to build SFML twice to do it.

(I'm assuming you're using 15.10 / Wily here - if not, don't use libsfml-dev since its out of date)
Title: Re: Linking on Linux
Post by: shadowmouse on November 17, 2015, 12:31:07 am
I'm using 14.04 LTS and I've installed using the source code download on the downloads page and CMake.
Title: Re: Linking on Linux
Post by: SeriousITGuy on November 19, 2015, 11:15:42 am
Another side question after following your efforts: Are you compiling statically?
I'm asking cause that is not what is commonly done on a *nix-system, dynamic linking is far more common due to how libraries are handled on *nix-systems ( normally installed under /usr/lib or /usr/local/lib via package manager or make) or providing the needed libs alongside your binary and setting library path yourself via a shell script to start your application. (One of the major differences in the Linux philosophy, that's why static libs are rarely provided by binary packages)

Also one side note, do not upgrade to 14.04.2 or 14.04.3 with the new hwe-stack, this breaks some dependencies in aptitude and you are no longer able to compile sfml because of a missing opengl dependency. In 15.10 this works again, and you get a much newer kernel (4.2, especially important for newer nvidia or amd graphic cards) and GCC 5.2.0.
I cannot use 14.04 anymore because of my new graphics card (Nvidia GTX970) which requires kernel 3.17 or newer.

As a new linux aspirant I hope this helps you ;)
Title: Re: Linking on Linux
Post by: shadowmouse on November 19, 2015, 05:58:50 pm
Yes I am compiling statically because that's what I always did on Windows and it makes things easier for me because I don't have to transfer external files. I'm on 14.04 LTS so I'll make sure to look out for .02 or .03. I only got 14.04 because that was the one that it said is generally recommended and seeing as it's my first Linux OS I thought I''d follow the guidance.
Title: Re: Linking on Linux
Post by: Jesper Juhl on November 19, 2015, 09:35:28 pm
On Linux I'd generally advice dynamic linking for the least amount of headaches.