Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Static linking under Linux  (Read 7014 times)

0 Members and 1 Guest are viewing this topic.

igorko

  • Newbie
  • *
  • Posts: 7
    • View Profile
Static linking under Linux
« on: April 10, 2012, 07:35:57 am »
Yes, i have found this topic ;)
http://en.sfml-dev.org/forums/index.php?topic=280

And i have added next code to CmakeLists.txt even before i found it.

if (NOT WIN32 AND SFML_STATIC_LIBRARIES)
list(APPEND LINKS
   pthread
   GL
   X11
   Xrandr
   freetype
   GLEW
   jpeg
   sndfile
   openal
)
endif()

And this fixed linking with static sfml (the game http://sourceforge.net/projects/lechemindeladam/develop) under Ubuntu 10.04. But when i tried to build the same code under Xubuntu 12.04 beta static SFML was built ok, but linking the game with sfml gave "cannot found -lGL". When removing linking with GL i get unresolved external... you know...

SFML used: master branch.
IDE: Code::Blocks.

Code::Blocks under Windows + MinGW doesn't need to link twice with opengl and company.(it's known) but i wonder how does it work: SFML links ok with -lGL but game doesn't.

Maybe it's just because 12.04 is still beta but maybe SFML finds opengl in some tricky way under linux. I didn't find corresponding code in cmake scripts(maybe I just looked too less)

If you will ask why i do need to link with sfml statically i have answer: it's not released yet :/ and i want to have portable binary.
« Last Edit: April 10, 2012, 07:41:43 am by igorko »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Static linking under Linux
« Reply #1 on: April 10, 2012, 08:15:51 am »
You should locate libraries with their FindXxx.cmake module (when available). Some libraries might be located in non-standard directories -- even if it's unlikely to happen on Linux, it's cleaner and better for portability across OSes.
Laurent Gomila - SFML developer

igorko

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Static linking under Linux
« Reply #2 on: April 10, 2012, 11:04:26 am »
How SFML does search for opengl? Don't see that code... Only for GLEW.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Static linking under Linux
« Reply #3 on: April 10, 2012, 11:06:02 am »
In src/SFML/Window/CMakeLists.txt and src/SFML/Graphics/CMakeLists.txt:
Code: [Select]
find_package(OpenGL REQUIRED)
Laurent Gomila - SFML developer

igorko

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Static linking under Linux
« Reply #4 on: April 10, 2012, 12:04:36 pm »
System/CmakeLists.txt

if(UNIX)
    set(SYSTEM_EXT_LIBS ${SYSTEM_EXT_LIBS} pthread)
endif()
if(LINUX)
    set(SYSTEM_EXT_LIBS ${SYSTEM_EXT_LIBS} rt)
endif()

Linux doesn't use pthread? Interested because the same binary(with static sfml) has some problems with libxcb... But rt(dunno what it is) is not listed in dependencies list, only pthread...

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Static linking under Linux
« Reply #5 on: April 10, 2012, 12:08:08 pm »
librt is required for the clock_gettime function, which is used to measure the elapsed time.

pthread is used on Linux, the UNIX variable includes all kind of unixes (Linux, OS X, FreeBSD, ...).
Laurent Gomila - SFML developer

igorko

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Static linking under Linux
« Reply #6 on: April 10, 2012, 06:42:14 pm »
Code: [Select]
||=== Holyspirit, all ===|
/home/igor/SFML/Build/lib/libsfml-audio-s.a(Listener.cpp.o)||In function `sf::Listener::setGlobalVolume(float)':|
Listener.cpp|| undefined reference to `alListenerf'|
/home/igor/SFML/Build/lib/libsfml-audio-s.a(Listener.cpp.o)||In function `sf::Listener::getGlobalVolume()':|
Listener.cpp|| undefined reference to `alGetListenerf'|
/home/igor/SFML/Build/lib/libsfml-audio-s.a(Listener.cpp.o)||In function `sf::Listener::setPosition(float, float, float)':|
Listener.cpp|| undefined reference to `alListener3f'|
/home/igor/SFML/Build/lib/libsfml-audio-s.a(Listener.cpp.o)||In function `sf::Listener::setPosition(sf::Vector3<float> const&)':|
Listener.cpp|| undefined reference to `alListener3f'|
/home/igor/SFML/Build/lib/libsfml-audio-s.a(Listener.cpp.o)||In function `sf::Listener::getPosition()':|
Listener.cpp|| undefined reference to `alGetListener3f'|
/home/igor/SFML/Build/lib/libsfml-audio-s.a(Listener.cpp.o)||In function `sf::Listener::setDirection(float, float, float)':|
Listener.cpp|| undefined reference to `alListenerfv'|
/home/igor/SFML/Build/lib/libsfml-audio-s.a(Listener.cpp.o)||In function `sf::Listener::setDirection(sf::Vector3<float> const&)':|
Listener.cpp|| undefined reference to `alListenerfv'|
/home/igor/SFML/Build/lib/libsfml-audio-s.a(Listener.cpp.o)||In function `sf::Listener::getDirection()':|
Listener.cpp|| undefined reference to `alGetListenerfv'|
/home/igor/SFML/Build/lib/libsfml-audio-s.a(Sound.cpp.o)||In function `sf::Sound::~Sound()':|
Sound.cpp|| undefined reference to `alSourceStop'|
/home/igor/SFML/Build/lib/libsfml-audio-s.a(Sound.cpp.o)||In function `sf::Sound::~Sound()':|
Sound.cpp|| undefined reference to `alSourceStop'|
/home/igor/SFML/Build/lib/libsfml-audio-s.a(Sound.cpp.o)||In function `sf::Sound::Sound(sf::SoundBuffer const&)':|
Sound.cpp|| undefined reference to `alSourcei'|
/home/igor/SFML/Build/lib/libsfml-audio-s.a(Sound.cpp.o)||In function `sf::Sound::Sound(sf::Sound const&)':|
Sound.cpp|| undefined reference to `alSourcei'|
Sound.cpp|| undefined reference to `alGetSourcei'|
Sound.cpp|| undefined reference to `alSourcei'|
/home/igor/SFML/Build/lib/libsfml-audio-s.a(Sound.cpp.o)||In function `sf::Sound::setBuffer(sf::SoundBuffer const&)':|
Sound.cpp|| undefined reference to `alSourceStop'|
Sound.cpp|| undefined reference to `alSourcei'|
/home/igor/SFML/Build/lib/libsfml-audio-s.a(Sound.cpp.o)||In function `sf::Sound::setLoop(bool)':|
Sound.cpp|| undefined reference to `alSourcei'|
/home/igor/SFML/Build/lib/libsfml-audio-s.a(Sound.cpp.o)||In function `sf::Sound::setPlayingOffset(sf::Time)':|
Sound.cpp|| undefined reference to `alSourcef'|
/home/igor/SFML/Build/lib/libsfml-audio-s.a(Sound.cpp.o)||In function `sf::Sound::getLoop() const':|
Sound.cpp|| undefined reference to `alGetSourcei'|
/home/igor/SFML/Build/lib/libsfml-audio-s.a(Sound.cpp.o)||In function `sf::Sound::getPlayingOffset() const':|
Sound.cpp|| undefined reference to `alGetSourcef'|
/home/igor/SFML/Build/lib/libsfml-audio-s.a(Sound.cpp.o)||In function `sf::Sound::operator=(sf::Sound const&)':|
Sound.cpp|| undefined reference to `alSourceStop'|
Sound.cpp|| undefined reference to `alSourcei'|
Sound.cpp|| undefined reference to `alGetSourcei'|
Sound.cpp|| undefined reference to `alSourcei'|
/home/igor/SFML/Build/lib/libsfml-audio-s.a(Sound.cpp.o)||In function `sf::Sound::resetBuffer()':|
Sound.cpp|| undefined reference to `alSourceStop'|
Sound.cpp|| undefined reference to `alSourcei'|
/home/igor/SFML/Build/lib/libsfml-audio-s.a(Sound.cpp.o)||In function `sf::Sound::play()':|
Sound.cpp|| undefined reference to `alSourcePlay'|
/home/igor/SFML/Build/lib/libsfml-audio-s.a(Sound.cpp.o)||In function `sf::Sound::pause()':|
Sound.cpp|| undefined reference to `alSourcePause'|
/home/igor/SFML/Build/lib/libsfml-audio-s.a(Sound.cpp.o)||In function `sf::Sound::stop()':|
Sound.cpp|| undefined reference to `alSourceStop'|
/home/igor/SFML/Build/lib/libsfml-audio-s.a(SoundBuffer.cpp.o)||In function `sf::SoundBuffer::update(unsigned int, unsigned int) [clone .part.18]':|
SoundBuffer.cpp|| undefined reference to `alBufferData'|
/home/igor/SFML/Build/lib/libsfml-audio-s.a(SoundBuffer.cpp.o)||In function `sf::SoundBuffer::saveToFile(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) const':|
SoundBuffer.cpp|| undefined reference to `alGetBufferi'|
SoundBuffer.cpp|| undefined reference to `alGetBufferi'|
/home/igor/SFML/Build/lib/libsfml-audio-s.a(SoundBuffer.cpp.o)||In function `sf::SoundBuffer::getSampleRate() const':|
SoundBuffer.cpp|| undefined reference to `alGetBufferi'|
/home/igor/SFML/Build/lib/libsfml-audio-s.a(SoundBuffer.cpp.o)||In function `sf::SoundBuffer::getChannelCount() const':|
SoundBuffer.cpp|| undefined reference to `alGetBufferi'|
/home/igor/SFML/Build/lib/libsfml-audio-s.a(SoundBuffer.cpp.o)||In function `sf::SoundBuffer::SoundBuffer()':|
SoundBuffer.cpp|| undefined reference to `alGenBuffers'|
/home/igor/SFML/Build/lib/libsfml-audio-s.a(SoundBuffer.cpp.o)||In function `sf::SoundBuffer::SoundBuffer(sf::SoundBuffer const&)':|
SoundBuffer.cpp|| undefined reference to `alGenBuffers'|
SoundBuffer.cpp|| undefined reference to `alGetBufferi'|
SoundBuffer.cpp|| undefined reference to `alGetBufferi'|
/home/igor/SFML/Build/lib/libsfml-audio-s.a(SoundBuffer.cpp.o)||In function `sf::SoundBuffer::~SoundBuffer()':|
SoundBuffer.cpp|| undefined reference to `alDeleteBuffers'|
/home/igor/SFML/Build/lib/libsfml-audio-s.a(SoundBuffer.cpp.o)||In function `sf::SoundBuffer::operator=(sf::SoundBuffer const&)':|
SoundBuffer.cpp|| undefined reference to `alGenBuffers'|
SoundBuffer.cpp|| undefined reference to `alGetBufferi'|
SoundBuffer.cpp|| undefined reference to `alGetBufferi'|
SoundBuffer.cpp|| undefined reference to `alDeleteBuffers'|
/home/igor/SFML/Build/lib/libsfml-audio-s.a(SoundSource.cpp.o)||In function `sf::SoundSource::~SoundSource()':|
SoundSource.cpp|| undefined reference to `alSourcei'|
SoundSource.cpp|| undefined reference to `alDeleteSources'|
/home/igor/SFML/Build/lib/libsfml-audio-s.a(SoundSource.cpp.o)||In function `sf::SoundSource::~SoundSource()':|
SoundSource.cpp|| undefined reference to `alSourcei'|
SoundSource.cpp|| undefined reference to `alDeleteSources'|
/home/igor/SFML/Build/lib/libsfml-audio-s.a(SoundSource.cpp.o)||In function `sf::SoundSource::SoundSource()':|
SoundSource.cpp|| undefined reference to `alGenSources'|
SoundSource.cpp|| undefined reference to `alSourcei'|
/home/igor/SFML/Build/lib/libsfml-audio-s.a(SoundSource.cpp.o)||In function `sf::SoundSource::SoundSource(sf::SoundSource const&)':|
SoundSource.cpp|| undefined reference to `alGenSources'|
||More errors follow but not being shown.|
||Edit the max errors limit in compiler options...|
||=== Build finished: 50 errors, 0 warnings ===|
while project links with openal and sfml audio was built successfully...
« Last Edit: April 11, 2012, 07:22:55 am by igorko »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Static linking under Linux
« Reply #7 on: April 10, 2012, 07:30:42 pm »
Are you really sure that you're linking with OpenAL? Have you checked the linker command line? Can you show your CMake code?
Laurent Gomila - SFML developer

igorko

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Static linking under Linux
« Reply #8 on: April 11, 2012, 10:21:02 pm »
See my first post. It works under Ubuntu 10.04. Now i have tried to add find_package and replaced lib names with vars but no success... Looks like 12.04 is still buggy.

igorko

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Static linking under Linux
« Reply #9 on: May 07, 2012, 08:22:05 pm »
Ok. I have fixed it. First of all seems like

Code: [Select]
g++ app.cpp -o app -lpthread -lX11 -lGL -lXrandr -lfreetype -lsfml-graphics -lsfml-window -lsfml-system
is wrong (at least on ubuntu 12.04)

Here is part of final cmakelists.txt that does work:

Code: [Select]
add_Executable (Holyspirit ${GAME_SOURCES})

# add an option for choosing the build type of SFML(shared or static)
set_option(SFML_STATIC_LIBRARIES TRUE BOOL "TRUE to build SFML as static libraries, FALSE to build it as shared libraries")

# Detect and add SFML
find_package(SFML 2.0 REQUIRED system window graphics network audio)
if(SFML_FOUND)
include_directories(${SFML_INCLUDE_DIR})
list(APPEND LINKS
${GNUWIN_LIBRARY} ${SFML_AUDIO_LIBRARY} ${SFML_NETWORK_LIBRARY} ${SFML_GRAPHICS_LIBRARY} ${SFML_WINDOW_LIBRARY} ${SFML_SYSTEM_LIBRARY})
endif()

#If game doesn't build try to put next IF statement before SFML detection
if (NOT WIN32 AND SFML_STATIC_LIBRARIES)

#This can be used if your distro can't find needed libraries
#find_package(OpenGL REQUIRED) ${OPENGL_gl_LIBRARY}
#find_package(Freetype REQUIRED) ${FREETYPE_LIBRARY}
#find_package(GLEW REQUIRED) ${GLEW_LIBRARY}
#find_package(JPEG REQUIRED) ${JPEG_LIBRARY}
#find_package(X11 REQUIRED) ${X11_LIBRARIES}
#find_package(OpenAL REQUIRED) ${OPENAL_LIBRARY}
#find_package(Sndfile REQUIRED) ${SNDFILE_LIBRARY}

list(APPEND LINKS
#if needed uncomment those
#pthread
#X11
#GL
Xrandr
freetype
GLEW
jpeg
sndfile
openal
)
endif()


target_link_libraries(Holyspirit ${LINKS})

linking to pthread-openal libs BEFORE linking to SFML worked under ubuntu 8.04 without problems. But under ubuntu 12.04 i should link to these libs AFTER linking with SFML. Also if i link after SFML, some libs(commented out) are not needed at all on both systems(but were needed on ubuntu 8.04 in previous case)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Static linking under Linux
« Reply #10 on: May 07, 2012, 08:30:56 pm »
Why don't you use find_package? It's much better than directly using the libraries names, since it will find the exact files that are found on your system -- and complain at configure time if they are not found.
Laurent Gomila - SFML developer