Force Cmake to Use the Full Library Path

Force CMake to use the full library path

Turning my comments into an answer

I was able to reproduce your problem - even without having the exact same environment - and found two possible solutions:

  1. You set policy CMP0060 to NEW

    cmake_policy(SET CMP0060 NEW)

    The NEW behavior for this policy is to link libraries by full path even if they are in implicit link directories.

  2. You can create a intermediate imported library and use IMPORTED_LOCATION (see [CMake] TARGET_LINK_LIBRARIES with full path libraries)

    add_library(curl UNKNOWN IMPORTED)
    set_property(TARGET curl PROPERTY IMPORTED_LOCATION "${CURL_LIBRARIES}")
    target_link_libraries(curl_ex curl)

    This worked for me, but according to CMake imported library behaviour you may need to also set IMPORTED_IMPLIB.

Background

Please check the setting of CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES because the paths listed there are taken as "implicit" search paths and full library paths are replaced accordingly (see cmComputeLinkInformation::CheckImplicitDirItem() and UnixPaths.cmake)

message("CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES: ${CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES}")

How do I add a library path in cmake?

The simplest way of doing this would be to add

include_directories(${CMAKE_SOURCE_DIR}/inc)
link_directories(${CMAKE_SOURCE_DIR}/lib)

add_executable(foo ${FOO_SRCS})
target_link_libraries(foo bar) # libbar.so is found in ${CMAKE_SOURCE_DIR}/lib

The modern CMake version that doesn't add the -I and -L flags to every compiler invocation would be to use imported libraries:

add_library(bar SHARED IMPORTED) # or STATIC instead of SHARED
set_target_properties(bar PROPERTIES
IMPORTED_LOCATION "${CMAKE_SOURCE_DIR}/lib/libbar.so"
INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_SOURCE_DIR}/include/libbar"
)

set(FOO_SRCS "foo.cpp")
add_executable(foo ${FOO_SRCS})
target_link_libraries(foo bar) # also adds the required include path

If setting the INTERFACE_INCLUDE_DIRECTORIES doesn't add the path, older versions of CMake also allow you to use target_include_directories(bar PUBLIC /path/to/include). However, this no longer works with CMake 3.6 or newer.

how do i specify libraries's path to cmake which are used by target_link_libraries

I found out that this issue can be solved by two methods (first method is similar to this)

second method is by adding below two lines of code to your cmakelist.txt file before calling target_link_directories

add_library( curl SHARED IMPORTED)
set_property(TARGET curl PROPERTY IMPORTED_LOCATION "${CURL_LIBRARY}")

where CURL_LIBRARY is the path for libcurl.so specified in toolchain.cmake
unfortunately i didn't find any solution to solve this issue without touching cmakelist.txt.

How to force llvm cmake to use only given path to libs?

I found a solution that worked out for me.
Find out what a path contains needed libraries (in my case it is /home/my_user/local/lib64 and then run
LD_LIBRARY_PATH=/home/my_user/local/lib64 make!

How can I specify library path when using Meson?

I see two possible approaches to solve your problem.

  • the first solution uses LIBRARY_PATH, which is different from LD_LIBRARY_PATH as explained later.
  • the second solution uses a modified meson file to directly pass options to the linker. Optionally, it also uses rpath that eliminates the need to modify LD_LIBRARY_PATH afterward.

    1. First solution

When building your project the linker use LIBRARY_PATH (and not LD_LIBRARY_PATH)

LIBRARY_PATH is used by gcc before compilation to search directories
containing static and shared libraries that need to be linked to your
program.

LD_LIBRARY_PATH is used by your program to search directories
containing shared libraries after it has been successfully compiled
and linked.

further details: LD_LIBRARY_PATH vs LIBRARY_PATH

Maybe you can try

export LIBRARY_PATH=/opt/conda/:$LIBRARY_PATH

before running meson to build your project.


  1. Second solution

Modifying your meson file and use rpath (optional)

An alternative to the previous first solution is to directly modify your Meson file to pass some options to the linker.

Here is something I used in the past you can easily adapt to your problem:

#
# blaspp
#
blaspp_lib = 'blaspp'
blaspp_lib_dir = '/opt/slate/lib'
blaspp_header_dir = '/opt/slate/include'

blaspp_dep = declare_dependency(
link_args : ['-L' + blaspp_lib_dir, '-l' + blaspp_lib],
include_directories : include_directories(blaspp_header_dir))

executable('test_blaspp',
'test_blaspp.cpp',
build_rpath : blaspp_lib_dir,
install_rpath : blaspp_lib_dir,
dependencies : [blaspp_dep])
  • declare_dependency(...) defines options to pass to the linker (this replaces the need to define LIBRARY_PATH in the first solution)

  • executable(...) defines rpath. This is an optional step that embeds the extra library path information directly into the executable. If you use this, you will not have to modify the LD_LIBRARY_PATH when running your executable.

Further details: https://amir.rachum.com/blog/2016/09/17/shared-libraries/ (have a look at the "rpath and runpath" section) and see wikipedia: https://en.wikipedia.org/wiki/Rpath



Related Topics



Leave a reply



Submit