How do I add a library path in cmake?

C++Cmake

C++ Problem Overview


I have 2 folders "inc" and "lib" in my project which have headers and static libs respectively. How do I tell cmake to use those 2 directories for include and linking respectively?

C++ Solutions


Solution 1 - C++

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.

Solution 2 - C++

might fail working with link_directories, then add each static library like following:

target_link_libraries(foo /path_to_static_library/libbar.a)

Solution 3 - C++

You had better use find_library command instead of link_directories. Concretely speaking there are two ways:

  1. designate the path within the command

    find_library(NAMES gtest PATHS path1 path2 ... pathN)

  2. set the variable CMAKE_LIBRARY_PATH

    set(CMAKE_LIBRARY_PATH path1 path2)
    find_library(NAMES gtest)

the reason is as flowings: > Note This command is rarely necessary and should be avoided where there are other choices. Prefer to pass full absolute paths to > libraries where possible, since this ensures the correct library will > always be linked. The find_library() command provides the full path, > which can generally be used directly in calls to > target_link_libraries(). Situations where a library search path may be > needed include: Project generators like Xcode where the user can > switch target architecture at build time, but a full path to a library > cannot be used because it only provides one architecture (i.e. it is > not a universal binary). > > Libraries may themselves have other private library dependencies that > expect to be found via RPATH mechanisms, but some linkers are not able > to fully decode those paths (e.g. due to the presence of things like > $ORIGIN). > > If a library search path must be provided, prefer to localize the > effect where possible by using the target_link_directories() command > rather than link_directories(). The target-specific command can also > control how the search directories propagate to other dependent > targets.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiongrasevskiView Question on Stackoverflow
Solution 1 - C++ar31View Answer on Stackoverflow
Solution 2 - C++Oleg KokorinView Answer on Stackoverflow
Solution 3 - C++Lincoln YesireView Answer on Stackoverflow