SFML community forums

Help => General => Topic started by: Symphonym on January 13, 2013, 05:51:59 pm

Title: [Ubuntu/Linux] Need some help understanding linking and CMake.
Post by: Symphonym on January 13, 2013, 05:51:59 pm
I'm currently experimenting with some stuff in Ubuntu, and thought I'd try SFML as well. I installed the extra required dependencies, downloaded the latest SFML release and compiled etc.

So I've got SFML up and running now using KDevelop4 (Which I choose over Eclipse C++ due to some annoying bugs). As KDevelop4 uses CMake to run it's applications, I've got a CMakeLists.txt that looks like this:

cmake_minimum_required(VERSION 2.8)
project(sfml)

set(SFML_INCLUDE_DIR "${PROJECT_BINARY_DIR}/libs/sfml/include/")
set(SFML_LIBRARY_DIR "${PROJECT_BINARY_DIR}/libs/sfml/lib/")

include_directories(${SFML_INCLUDE_DIR})

add_executable(sfml Engine/Foobar.cpp main.cpp)

find_library(SFML_AUDIO NAMES sfml-audio HINTS ${SFML_LIBRARY_DIR} NO_DEFAULT_PATH)
find_library(SFML_GRAPHICS NAMES sfml-graphics HINTS ${SFML_LIBRARY_DIR} NO_DEFAULT_PATH)
find_library(SFML_WINDOW NAMES sfml-window HINTS ${SFML_LIBRARY_DIR} NO_DEFAULT_PATH)
find_library(SFML_SYSTEM NAMES sfml-system HINTS ${SFML_LIBRARY_DIR} NO_DEFAULT_PATH)
find_library(SFML_NETWORK NAMES sfml-network HINTS ${SFML_LIBRARY_DIR} NO_DEFAULT_PATH)

target_link_libraries(sfml
        ${SFML_AUDIO}
        ${SFML_GRAPHICS}
        ${SFML_WINDOW}
        ${SFML_SYSTEM}
        ${SFML_NETWORK})

install(TARGETS sfml RUNTIME DESTINATION bin)


add_subdirectory(Engine)
 

So I have a folder called "libs" in the same directory as my executable, where I keep the SFML libraries and includes. So I proceed to build this, and run it, getting a nice little SFML window.. BUT.. here's what I don't understand:

Title: Re: [Ubuntu/Linux] Need some help understanding linking and CMake.
Post by: Laurent on January 13, 2013, 06:34:49 pm
Short answer:

1. On Linux, libraries are loaded from the paths known by the library loader (ld). The executable's path (".") is not included in ld's search paths by default.

2. It's not recommanded to package dependencies with the application. Linux manages shared libraries so well that it's easier to just install those dependencies directly on the target system. It's also safer because each Linux installation is potentially different; installing things from the target machine ensures that the correct versions are installed.
Title: Re: [Ubuntu/Linux] Need some help understanding linking and CMake.
Post by: Symphonym on January 13, 2013, 07:05:28 pm
Short answer:

1. On Linux, libraries are loaded from the paths known by the library loader (ld). The executable's path (".") is not included in ld's search paths by default.

2. It's not recommanded to package dependencies with the application. Linux manages shared libraries so well that it's easier to just install those dependencies directly on the target system. It's also safer because each Linux installation is potentially different; installing things from the target machine ensures that the correct versions are installed.

Just some question that arose upon reading this:

1. How can I then add/change locations known to the library loader? Is it the command "-L" for Gcc? If so, how would that translate into CMake? Or do you usually don't want libraries around the .exe in Linux, package manager ftw?

2. Altough the SFML lib files (.so) should be exported along with the executable right? Since the only package which can be downloaded for sfml is "libsfml-dev" (afaik) and it's for 1.6.

3. Just another thing. Lets say it is possible to change "ld" paths (Which I would assume it is). So I specify the path "~/Desktop/project/build/libs" for example. But it's very unlikely that all users downloading my program will have this path, so I would assume the easiest way is to add the .exe directory to the "ld", but then again, how do I do that?
Title: Re: [Ubuntu/Linux] Need some help understanding linking and CMake.
Post by: Laurent on January 13, 2013, 08:30:17 pm
1. We're talking about the libraries that are loaded when you launch the executable, it has nothing to do with compiling or linking. You can temporarily change ld's search paths by adding paths to the LD_LIBRARY_PATH environment variable. To make permanent changes, you'll have to use Google, I'm not a Linux expert.

2. Yep. But once SFML 2 is released and included in the official repositories, this will no longer be necessary.

3. What you can do is to write a script that adds "." to the ld search paths and then launches the executable. There's another solution: if you use rpath (there are CMake variables for this) you can embed the relative path to the libraries directly into the executable.