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

Author Topic: Strange behaviour of linker  (Read 1610 times)

0 Members and 1 Guest are viewing this topic.

GetterSetter

  • Newbie
  • *
  • Posts: 18
    • View Profile
Strange behaviour of linker
« on: December 24, 2019, 09:51:18 pm »
Hi everyone!
My OS: Ubuntu 18.04
SFML version: 2.5.1
Firstly I want to write that maybe it isn't a problem of SFML, but my research has shown that SFML influences linker.
Code (this may not be right, but it doesn't matter in the context of this problem):
#include <SFML/Network.hpp>
#include <SFML/Graphics.hpp>
#include <string>
#include <iostream>
int main()
{
    sf::TcpSocket socket;
    sf::TcpListener socketListen;
    if (socketListen.listen(53000) != sf::Socket::Done)
    {
        std::cerr<<"Error while connecting to port\nAborted.";
        return 1;    
    }

    if (socketListen.accept(socket) != sf::Socket::Done)
    {
        std::cerr<<"Error while accepting\nAborted.";
        return 1;          
    }

    sf::Packet packet;
    std::string message = "Hi! Welcome to my server. I hope you'll enjoy it.";
    packet<<message;
    socket.send(packet);
    socket.receive(packet);
    packet>>message;
    std::cout<<message;
    return 0;
}
Ok, I can compile it without warnings and errors. I use the follow options:
g++ -std=c++17 -o networkingS  -I/home/name/SFML-2.5.1/include -L/home/name/SFML-2.5.1/lib -Wl,-rpath=/home/name/SFML-2.5.1/lib networking.cpp  -lsfml-network -lsfml-system -lsfml-graphics
Then I tried to start the program and I got output:
./networkingS: error while loading shared libraries: libsfml-system.so.2.5: cannot open shared object file: No such file or directory
 
Ok, I used ldd:
 linux-vdso.so.1 (0x00007ffc239ce000)
    libsfml-graphics.so.2.5 => /home/name/SFML-2.5.1/lib/libsfml-graphics.so.2.5 (0x00007f5dbfa98000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f5dbf6a7000)
    libsfml-system.so.2.5 => not found
We can see that linker can find path to the graphics module, but can't do it for the system module (and window too, as I have known later)
Ok, I thought "It's environmental problem, but stop, all my applications that use RenderWindow work well!"
I used ldd for them and I got perfect output
linux-vdso.so.1 (0x00007fffb99fe000)
    libsfml-graphics.so.2.5 => /home/name/SFML-2.5.1/lib/libsfml-graphics.so.2.5 (0x00007f08d885c000)
    libsfml-window.so.2.5 => /home/name/SFML-2.5.1/lib/libsfml-window.so.2.5 (0x00007f08d8633000)
    libsfml-system.so.2.5 => /home/name/SFML-2.5.1/lib/libsfml-system.so.2.5 (0x00007f08d8427000)
I'm stuck and don't know what to do with this.
I also tried to use LD_LIBRARY_PATH and it helped me, but I want to know, why some my applications work without setting LD_... and other can't do it.
P.S. sorry if there are grammar mistakes, English isn't my native language.

UPD: I don't know why, but this code works:
#include <SFML/Network.hpp>
#include <SFML/Graphics.hpp>
#include <string>
#include <iostream>
int main()
{
   sf::TcpSocket socket;
   sf::Socket::Status status = socket.connect("127.0.0.1", 53000);
   if (status != sf::Socket::Done)
   {
      std::cerr<<"Error while connecting.\nAborted.\n";
      return 1;
   }

    sf::Packet packet;
    std::string message;
    socket.receive(packet);
    packet>>message;
    std::cout<<message;
    std::cin>>message;
    packet.clear();
    packet<<message;
    socket.send(packet);
    return 0;
}
 

Oh, I forgot to give ldd output of code above:
        libsfml-network.so.2.5 => /home/name/SFML-2.5.1/lib/libsfml-network.so.2.5 (0x00007fefb6d62000)
        libsfml-system.so.2.5 => /home/name/SFML-2.5.1/lib/libsfml-system.so.2.5 (0x00007fefb6b56000)
 

It's becoming more complicated for me...
« Last Edit: December 25, 2019, 11:09:56 am by GetterSetter »

GetterSetter

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: Strange behaviour of linker
« Reply #1 on: December 25, 2019, 01:21:20 pm »
I fixed it. It was problem of modules of SFML. They haven't got rpath inside

ldd for libsfml-network.so.2.5
        linux-vdso.so.1 (0x00007ffcf1ee5000)
        libsfml-system.so.2.5 => not found
        libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f14f50f5000)
 

So that I used patchelf:
patchelf --set-rpath /home/name/SFML-2.5.1/lib /home/name/SFML-2.5.1/lib/libsfml-network.so.2.5
 

ldd after:
linux-vdso.so.1 (0x00007ffdb2a83000)
        libsfml-system.so.2.5 => /home/name/SFML-2.5.1/lib/libsfml-system.so.2.5 (0x00007f02875f2000)
        libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f0287269000)
 

And as I've already told it fixed my problem.
I hope this topic will be useful for descendants.