Jump to content

  • Log In with Google      Sign In   
  • Create Account


link libSDL2.a with cmake


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 Maxjen   Members   -  Reputation: 191

Like
0Likes
Like

Posted 10 July 2013 - 06:51 AM

Hi,

 

I'm trying to compile a simple SDL2 application with cmake under Linux. My CMakeLists.txt looks like this:

project(Test)
cmake_minimum_required(VERSION 2.8)
aux_source_directory(. SRC_LIST)

find_package(OpenGL)

add_executable(${PROJECT_NAME} ${SRC_LIST})

#target_link_libraries(${PROJECT_NAME} /usr/local/lib/libSDL2.so ${OPENGL_gl_LIBRARY})
target_link_libraries(${PROJECT_NAME} /usr/local/lib/libSDL2.a pthread dl rt ${OPENGL_gl_LIBRARY})

It works immediately if I link it to libSDL2.so, but if I link it to libSDL2.a I have to add pthread, dl and rt or it gives me undefined reference errors like /usr/bin/ld: /usr/local/lib/libSDL2.a(SDL_syssem.o): undefined reference to symbol 'sem_getvalue@@GLIBC_2.2.5'. Can somebody explain why this is?

And is it correct that if I link my application to libSDL2.a that I can execute the binary file on any Linux installation without requiring SDL2 to be installed?



Sponsor:

#2 Karsten_   Members   -  Reputation: 1203

Like
1Likes
Like

Posted 10 July 2013 - 07:48 AM

Hi,

 

Basically the '.a' file is a static link library, meaning it pretty much only contains an archive of the SDL '.o' object files.

The '.so' file is a shared library meaning that it drags in its other dependent libraries as required at runtime.

 

If you do an 'ldd libSDL2.so', you will see that it lists all the SDL dependencies such as pthread etc...

 

However, if using Linux (rather than *BSD), you should usually explicitly specify linked dependencies even though the .so file already has them due to DSO linking changes in newer Linux distros. (http://fedoraproject.org/wiki/UnderstandingDSOLinkChange)

 

And, yes, if you statically link "everything" into your binary, it should mean that you can take it across to other Linux distros without having to get your players to install packages. Otherwise you would need to also carry across the .so files and set the LD_LIBRARY_PATH or something for your application to use them.

Linux is always in such a state of flux though that this isn't always guaranteed to work however, if the version of libc differ for example. Unfortunately certain libc versions also require an older or newer version of the Linux kernel.

Again, do a 'ldd' of your output application to see exactly what external libraries it depends on.


Edited by Karsten_, 10 July 2013 - 07:52 AM.

Mutiny - Open-source C++ Unity re-implementation.
Defile of Eden 2 - FreeBSD and OpenBSD binaries of our latest game.


#3 Maxjen   Members   -  Reputation: 191

Like
0Likes
Like

Posted 10 July 2013 - 10:20 AM

Thanks, that was very helpful!

 

I did ldd libSDL2.so and it listed these dependencies.

$ ldd libSDL2.so
    linux-vdso.so.1 =>  (0x00007fff507fe000)
    libm.so.6 => /lib64/libm.so.6 (0x00007fa64d693000)
    libdl.so.2 => /lib64/libdl.so.2 (0x00007fa64d48f000)
    libpthread.so.0 => /lib64/libpthread.so.0 (0x00007fa64d272000)
    librt.so.1 => /lib64/librt.so.1 (0x00007fa64d06a000)
    libc.so.6 => /lib64/libc.so.6 (0x00007fa64ccb2000)
    /lib64/ld-linux-x86-64.so.2 (0x0000003b9b400000)

However, I am still unsure if I have to add the dependencies that I haven't specified in my CMakeLists.txt so that it still works with those DSO linking changes. As mentioned I had to add libpthread, libdl, and librt. Must I now add libm, libc, linux-vdso and ld-linux-x86-64?
 



#4 Bregma   Crossbones+   -  Reputation: 4361

Like
1Likes
Like

Posted 10 July 2013 - 12:31 PM

Thanks, that was very helpful!

 

I did ldd libSDL2.so and it listed these dependencies.

$ ldd libSDL2.so
    linux-vdso.so.1 =>  (0x00007fff507fe000)
    libm.so.6 => /lib64/libm.so.6 (0x00007fa64d693000)
    libdl.so.2 => /lib64/libdl.so.2 (0x00007fa64d48f000)
    libpthread.so.0 => /lib64/libpthread.so.0 (0x00007fa64d272000)
    librt.so.1 => /lib64/librt.so.1 (0x00007fa64d06a000)
    libc.so.6 => /lib64/libc.so.6 (0x00007fa64ccb2000)
    /lib64/ld-linux-x86-64.so.2 (0x0000003b9b400000)

However, I am still unsure if I have to add the dependencies that I haven't specified in my CMakeLists.txt so that it still works with those DSO linking changes. As mentioned I had to add libpthread, libdl, and librt. Must I now add libm, libc, linux-vdso and ld-linux-x86-64?
 

If you want a purely static binary (no DSOs at all) then yes, you'd need to link all those libraries (save libc and the DSO interpreter, ld-linux-x86-64.so.2 and linux-vdso.so.1).  You would want to make sure you pass the relevant -static switches to the linker to get it to pick up the static versions of the runtime (libc).

 

I'd like to point out that you'll probably have trouble.  One of the dependencies is libdl, the dynamic library loader, which implies libSDL2 load plugins dynamically.  That will probably fail since there is no DSO interpreter to resolve the symbols at load time.  You're in for a world of pain, trying to do something the GNU system was designed explicitly to avoid.


Stephen M. Webb
Professional Free Software Developer

#5 Maxjen   Members   -  Reputation: 191

Like
0Likes
Like

Posted 10 July 2013 - 04:12 PM

I think right now I am only linking libSDL2 statically. Will I still have trouble?

project(Test)
cmake_minimum_required(VERSION 2.8)

set(SRC_LIST main.cpp)

find_package(OpenGL)

#SDL2 plus dependencies
set(SDL2
    /usr/local/lib/libSDL2.a
    pthread
    dl
    rt
    m
)

include_directories("${PROJECT_SOURCE_DIR}/include/")

add_executable(${PROJECT_NAME} ${SRC_LIST})
target_link_libraries(${PROJECT_NAME} ${SDL2} ${OPENGL_gl_LIBRARY})

I would like to be able to for example send the binary file to friends so they can test it and I think it would be too much to ask them to install SDL2 because on most distributions it can't be easily installed with the package manager. But it would be perfectly fine to link everything else dynamically.


Edited by Maxjen, 10 July 2013 - 04:13 PM.


#6 Karsten_   Members   -  Reputation: 1203

Like
1Likes
Like

Posted 10 July 2013 - 06:17 PM

If I recall correctly, there was a compile time option for SDL_image to either load in libpng.so dynamically at runtime or to statically compile in support (in which case zlib and libpng source files are also needed during compile time of the library). Perhaps there is a similar option in SDL2?

 

But yeah, it is a shame SDL2 is taking so long to reach the package repos / ports collections. I also find version 2 to be very poorly documented so I am staying away for a while longer ;)


Edited by Karsten_, 10 July 2013 - 06:19 PM.

Mutiny - Open-source C++ Unity re-implementation.
Defile of Eden 2 - FreeBSD and OpenBSD binaries of our latest game.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS