SFML community forums

Help => General => Topic started by: hatfarm on August 15, 2013, 07:42:21 am

Title: Linking in Linux
Post by: hatfarm on August 15, 2013, 07:42:21 am
Hey everyone.  I've been using SFML on Windows for the past 6 months, but I've been using CMake for my build system so that I could ensure that I could build it in Linux at least.  So, I've hit a milestone in my engine and I want to ensure that everything works in Linux as well, because I would like to be able to release Windows/Linux at least.  Anyway, I've fixed all the issues with my code, and I've even got the .a library linking, but unfortunately g++ is having issues linking the libraries linked to by SFML (for instance x11).  I build SFML with these directions:

http://sfmlcoder.wordpress.com/2011/08/16/building-sfml-2-0-with-make-for-gcc/

I added all of the libraries needed, but for whatever reason g++ isn't finding them when it goes to link my application.  Anyone have any ideas on how to possibly resolve this? 
Title: Re: Linking in Linux
Post by: Tank on August 15, 2013, 08:17:24 am
Please post the error messages ld produces. The build commands would also be helpful (make VERBOSE=1).
Title: Re: Linking in Linux
Post by: hatfarm on August 16, 2013, 01:34:24 am
Here's the tail end of the linking errors:

Quote
Texture.cpp:(.text+0x18be): undefined reference to `glGetTexImage'
/home/hatfarm/SFML-2.1//lib/libsfml-graphics-s.a(Texture.cpp.o): In function `sf::Texture::operator=(sf::Texture const&)':
Texture.cpp:(.text+0x1bec): undefined reference to `glDeleteTextures'
/home/hatfarm/SFML-2.1//lib/libsfml-graphics-s.a(TextureSaver.cpp.o): In function `sf::priv::TextureSaver::TextureSaver()':
TextureSaver.cpp:(.text+0x13): undefined reference to `glGetIntegerv'
/home/hatfarm/SFML-2.1//lib/libsfml-graphics-s.a(TextureSaver.cpp.o): In function `sf::priv::TextureSaver::~TextureSaver()':
TextureSaver.cpp:(.text+0x35): undefined reference to `glBindTexture'
/home/hatfarm/SFML-2.1//lib/libsfml-graphics-s.a(GLCheck.cpp.o): In function `sf::priv::glCheckError(char const*, unsigned int)':
GLCheck.cpp:(.text+0x34): undefined reference to `glGetError'
/home/hatfarm/SFML-2.1//lib/libsfml-graphics-s.a(GLCheck.cpp.o): In function `sf::priv::ensureGlewInit()':

The funny thing is, I specifically link to the glew.a library from the extlibs directory.  Any suggestion?  I didn't just sudo apt-get install sfml-dev because I think I read that 2.1 isn't there yet, and I'd like to be working off of 2.1.  Any help here would be greatly appreciated.
Title: Re: Linking in Linux
Post by: Jebbs on August 16, 2013, 02:54:03 am
Here's the tail end of the linking errors:

Quote
Texture.cpp:(.text+0x18be): undefined reference to `glGetTexImage'
/home/hatfarm/SFML-2.1//lib/libsfml-graphics-s.a(Texture.cpp.o): In function `sf::Texture::operator=(sf::Texture const&)':
Texture.cpp:(.text+0x1bec): undefined reference to `glDeleteTextures'
/home/hatfarm/SFML-2.1//lib/libsfml-graphics-s.a(TextureSaver.cpp.o): In function `sf::priv::TextureSaver::TextureSaver()':
TextureSaver.cpp:(.text+0x13): undefined reference to `glGetIntegerv'
/home/hatfarm/SFML-2.1//lib/libsfml-graphics-s.a(TextureSaver.cpp.o): In function `sf::priv::TextureSaver::~TextureSaver()':
TextureSaver.cpp:(.text+0x35): undefined reference to `glBindTexture'
/home/hatfarm/SFML-2.1//lib/libsfml-graphics-s.a(GLCheck.cpp.o): In function `sf::priv::glCheckError(char const*, unsigned int)':
GLCheck.cpp:(.text+0x34): undefined reference to `glGetError'
/home/hatfarm/SFML-2.1//lib/libsfml-graphics-s.a(GLCheck.cpp.o): In function `sf::priv::ensureGlewInit()':

The funny thing is, I specifically link to the glew.a library from the extlibs directory.  Any suggestion?  I didn't just sudo apt-get install sfml-dev because I think I read that 2.1 isn't there yet, and I'd like to be working off of 2.1.  Any help here would be greatly appreciated.

Those are OpenGL functions, not Glew ones. Looks like you aren't linking to OpenGL.

Doesn't linking with the graphics module automatically link your program to OpenGL though?
Title: Re: Linking in Linux
Post by: OniLinkPlus on August 16, 2013, 03:26:05 am
Doesn't linking with the graphics module automatically link your program to OpenGL though?
Not if he's linking statically.
Title: Re: Linking in Linux
Post by: hatfarm on August 16, 2013, 03:33:56 am
I am linking statically, when I tried to use the so.2.1 files, it said that it didn't have a build option (or something like that) for those libraries.  How do I go about linking OpenGL then?  Would I be better off using the dynamic libraries?
Title: Re: Linking in Linux
Post by: Tank on August 16, 2013, 08:29:47 am
-lGL
Title: Re: Linking in Linux
Post by: hatfarm on August 16, 2013, 04:55:06 pm
So, it's not linking any of the libraries that are needed by SFML.  Is there anywhere I can find the "-l"s that I need to build all of it.  Is there any easy way to do that in CMake?  Thanks for all your help.
Title: Re: Linking in Linux
Post by: eXpl0it3r on August 16, 2013, 05:22:03 pm
If you're linking things statically, you'll have to link against SFML and all its dependencies. You can find a list of them here (http://www.sfml-dev.org/tutorials/2.1/compile-with-cmake.php#installing-dependencies).
And no there isn't really an easy way to do it (afaik).
Title: Re: Linking in Linux
Post by: Ixrec on August 16, 2013, 05:40:14 pm
For me, using CMake to build my program (not just SFML) magically solved all of these linking problems. All I had to do was make my program into one of SFML's example programs by modifying the relevant CMake files. It was easy to guess how since those files are so simple.
Title: Re: Linking in Linux
Post by: eXpl0it3r on August 16, 2013, 06:06:31 pm
For me, using CMake to build my program (not just SFML) magically solved all of these linking problems.
Are you linking statically as well? ;)
Dynamic linkage will be much easier, yes.
Title: Re: Linking in Linux
Post by: Ixrec on August 17, 2013, 01:02:02 am
Yes mine's all static (unless you count the audio libraries).
Title: Re: Linking in Linux
Post by: hatfarm on August 17, 2013, 11:11:31 pm
The problem I'm having is that I already wrote my CMake files and didn't have any issues with Windows, but now with Linux I have to link all of these libraries separately.  I've looked through the SFML CMake files and I can't find where the variables are actually defined.  There is, for instance, the "OPENGL_INCLUDE_DIR" but I cannot find where that is defined.  I actually wrote a python script to search for that in all .txt files under the main SFML directory(and subdirectories) and was unable to find it.  Any recommendations?
Title: Re: Linking in Linux
Post by: Laurent on August 17, 2013, 11:19:46 pm
This kind of variables are defined by the find_package(XXX) calls.
Title: Re: Linking in Linux
Post by: Ixrec on August 17, 2013, 11:30:33 pm
That's why I made my program pretend to be one of SFML's examples.  Just go into the examples folder, modify the CMake file there which essentially just lists subfolders (after making a new subfolder for your program), then look at the CMake file that each example program's subfolder has and make a version of it for your program.  The files are pretty self-explanatory.

I have no idea if this was the optimal approach but it was very quick and easy and it hasn't broken for me yet.
Title: Re: Linking in Linux
Post by: hatfarm on August 18, 2013, 04:03:46 am
Alright, doing the example following, I added find_package(OpenGL REQUIRED) and I link it, but now I get an error in CMake that says:

Cannot specify link libraries for target
  "/usr/lib/i386-linux-gnu/libGLU.so" which is not built by this project.

When I try to link ${OPENGL_LIBRARIES}

Any ideas?
Title: Re: Linking in Linux
Post by: Laurent on August 18, 2013, 09:38:42 am
The first argument of target_link_libraries must be the target (your executable), not the libraries to link.
Title: Re: Linking in Linux
Post by: hatfarm on August 18, 2013, 09:57:46 pm
Ah, that wasn't exactly the problem, but that lead me to figure out what the problem was.  The problem was that I copied the code from my executable's CMakeLists.txt into my library, and I didn't change the target name, so it was telling me that EXEC_NAME didn't have a build target, because it was supposed to be LIBR_NAME.  That resolved the linking with SFML issues, now I'm having issues where it's not able to link my library to my executable, but since that's not an SFML issue,  I'll keep that outta here.  Thanks for all your help everyone, much appreciated!
Title: Re: Linking in Linux
Post by: hatfarm on August 19, 2013, 05:59:46 am
Gah!  I resolved the issues with my own linking, but now it's having problems with X11.  I've got this in my CMakeLists.txt files:

IF(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
    find_package(X11 REQUIRED)
    set(LIB_INCLUDES
        ${LIB_INCLUDES}
        ${X11_INCLUDE_DIR})
    set(LIB_LINKS
        ${LIB_LINKS} ${X11_LIBRARIES} )
ENDIF(${CMAKE_SYSTEM_NAME} MATCHES "Linux")

However, I get this error when it goes to link:

/usr/bin/ld: /home/hatfarm/SFML-2.1//lib/libsfml-window-s.a(WindowImplX11.cpp.o): undefined reference to symbol 'XGetWindowAttributes'
/usr/bin/ld: note: 'XGetWindowAttributes' is defined in DSO /usr/lib/gcc/i686-linux-gnu/4.7/../../../i386-linux-gnu/libX11.so so try adding it to the linker command line
/usr/lib/gcc/i686-linux-gnu/4.7/../../../i386-linux-gnu/libX11.so: could not read symbols: Invalid operation
collect2: error: ld returned 1 exit status

I have it print out LIB_LINKS and it shows exactly the libx11.so is found.  I know that LIB_LINKS and LIB_INCS are not the problem, because it doesn't complain about my library or OpenGL.  I know x11 is installed, because I can see that file, and I was able to build SFML.

Anyone have any ideas?