Cmake: linking shared library

前端 未结 3 644
失恋的感觉
失恋的感觉 2021-02-06 10:40

I\'ve done this before a couple of times, but somehow I\'m stuck this time. I have an executable \"myapp\" and a own shared library \"mylib\". In my cmakelists I have the follow

相关标签:
3条回答
  • 2021-02-06 10:50

    This is a very common question about "make install". Actually, there are 3 ways to link a library to your executable file. First, you may use -l -L flags in simple cases. As Benjamin said you may use LD_LIRARY_PATH and write something like: export LD_LIBRARY_PATH=/usr/local/my_lib. In fact this is not a good way. It's much better to use RPATH. There is a very useful doc page about it. Check it out. Well if you write something like this in your top level CMakeLists.txt, it will solve the problem:

    SET(CMAKE_SKIP_BUILD_RPATH  FALSE)
    SET(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE) 
    SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib64")
    SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
    SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib64")
    
    0 讨论(0)
  • 2021-02-06 11:00

    Add the path of the directory containing the library to the LD_LIBRARY_PATH environment variable, by appanding a new path:

    export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/path/to/the/library/
    

    You can check the library is correctly found with the 'ldd' tool:

    lld ./executable
    

    If the library is not stated as "not found" it is OK and your executable will be executed properly.

    Add the 'export' command to your bashrc to properly set the LD_LIBRARY_PATH variable after each system reboot, otherwise you will have to execute again the 'export' command.

    0 讨论(0)
  • 2021-02-06 11:02

    During the installation of your library and executable, the runtime paths to find the library are stripped from the executable. Therefore your library has to reside in the runtime library search path. For example under Linux, try to set LD_LIBRARY_PATH to the directory that contains the installed library when starting your executable.

    0 讨论(0)
提交回复
热议问题