How to Add a Library Path in Cmake

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.

Setting Cmake Library paths without Editing CmakeLists

Try to use CMAKE_LIBRARY_PATH

Semicolon-separated list of directories specifying a search path for the find_library() command. By default it is empty, it is intended to be set by the project. See also CMAKE_SYSTEM_LIBRARY_PATH and CMAKE_PREFIX_PATH.

src: https://cmake.org/cmake/help/latest/variable/CMAKE_LIBRARY_PATH.html

set CMAKE_GENERATOR=Visual Studio 15 2017 Win64
cmake -H. -Bbuild -G "%CMAKE_GENERATOR%" "-DCMAKE_PREFIX_PATH=path_to_samlib"

CMake Include paths - library that depend on external library

In the top-level:

cmake_minimum_required(VERSION 3.21)
project(project)

option(BUILD_SHARED_LIBS "Build shared libs instead of static" ON)

add_subdirectory(libA)
add_subdirectory(libB)

add_executable(App main.cpp)
target_link_libraries(App PRIVATE libA libB)

In libA:

add_library(libA src.cpp)

# Use PUBLIC below if libA exposes libB's types in its API.
target_link_libraries(libA PRIVATE libB)

In libB:

add_library(libB foo.cpp)
target_include_directories(
libB PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>"
)

PUBLIC and INTERFACE properties on targets propagate to their linkees. Thus, when we set up the include directories on libB, any target linking to it will get its source directory added it its include path.

We guard the include directory with $<BUILD_INTERFACE:...> so that if you ever install() the library somewhere it might be re-used in another CMake build, it won't remember details of your specific file system.

How to make CMake use environment variable LD_LIBRARY_PATH and C_INCLUDE_DIRS

It is not fully clear what you intend to do with these variables. Here are some possibilities:

  1. Inside a CMake script you can read environment variables using the syntax $ENV{<VARIABLE_NAME>}. So in your CMakeLists.txt you can have something like

    message( "Found environment variable LD_LIBRARY_PATH=$ENV{LD_LIBRARY_PATH}" )
  2. If you want to add the location contained in this variable to be available to your CMake target executables and libraries then you can use the link_directories() command as

    link_directories( $ENV{LD_LIBRARY_PATH} )
  3. Or if you have someone else's project and you want to instruct CMake to look for libraries in some additional directories you can use CMAKE_PREFIX_PATH or CMAKE_LIBRARY_PATH. For example to pass these variables in a command line you could do

    cmake -D CMAKE_PREFIX_PATH=/path/to/custom/location

Pass static library name (-I) and its path (-L) to cmake from command line

Well, there are CMAKE_EXE_LINKER_FLAGS, CMAKE_STATIC_LINKER_FLAGS, CMAKE_MODULE_LINKER_FLAGS and CMAKE_SHARED_LINKER_FLAGS variables, but in most cases you don't need to specify these.

If the project you are building require some libraries, you have to list them in target_link_libraries() call somewhere in CMakeLists.txt.

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