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

Author Topic: [Ubuntu/Linux] Need some help understanding linking and CMake.  (Read 3417 times)

0 Members and 1 Guest are viewing this topic.

Symphonym

  • Newbie
  • *
  • Posts: 32
    • View Profile
[Ubuntu/Linux] Need some help understanding linking and CMake.
« 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:

  • If I move my executable, as well as the lib folder far away from eachother, I can still run the executable without problem. I assume it uses the libs located in /usr/local/lib to link to, but shouldn't it search for libraries in the folder I specified in CMake? Or look for them in the same directory  as the executable is in?
  • So I think everything is fine and send my friend the executable as well as the SFML libraries, but he's missing those extra dependencies that you had to install for Linux (according to tutorial). So I would assume I have to export those libraries to him as well or tell him to download them through the package manager? I'm fairly new to Linux and Ubuntu, but this seems like a kinda unuserfriendly way to send around a program
  • If the above seems kinda cluttered, a quick summary: Executables look for their libs in /usr/local/libs or in the directory the .exe is in? Does the person I send the program to need all the extra linux dependencies too, if he's running Linux? Why don't Windows for example need those extra dependencies?
« Last Edit: January 13, 2013, 05:55:52 pm by Symphonym »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: [Ubuntu/Linux] Need some help understanding linking and CMake.
« Reply #1 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.
Laurent Gomila - SFML developer

Symphonym

  • Newbie
  • *
  • Posts: 32
    • View Profile
Re: [Ubuntu/Linux] Need some help understanding linking and CMake.
« Reply #2 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?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: [Ubuntu/Linux] Need some help understanding linking and CMake.
« Reply #3 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.
Laurent Gomila - SFML developer