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.

Answer from ar31 on Stack Overflow
🌐
CMake
cmake.org β€Ί cmake β€Ί help β€Ί latest β€Ί command β€Ί link_directories.html
link_directories β€” CMake 4.3.0-rc3 Documentation
Adds the paths in which the linker should search for libraries. Relative paths given to this command are interpreted as relative to the current source directory, see CMP0015. The command will apply only to targets created after it is called. Added in version 3.13: The directories are added ...
Discussions

Library and include path
Hello, I am creating a library, and the associated test. Here is the files structure : ── MyLibrary β”œβ”€β”€ CMakeLists.txt β”œβ”€β”€ include β”‚ └── x.h β”œβ”€β”€ src β”‚ β”œβ”€β”€ CMakeLists.txt β”‚ β”œβ”€β”€ x.cpp β”‚ β”œβ”€β”€ y.h β”‚ └── y.cpp └── test β”œβ”€β”€ ... More on discourse.cmake.org
🌐 discourse.cmake.org
0
0
November 28, 2023
How do I add a lib library path to a subpart of LLVM?
Question I tried compiling llvm15 on Ubuntu with AArch64 architecture. However, I encountered the following problems: 1. /usr/bin/ld: cannot find -lgcc_s, some of the errors are as follows: [build] [73/4456 0% :: 1.026] Linking CXX static library lib/clang/15.0.1/lib/aarch64-unknown-linux-... More on discourse.cmake.org
🌐 discourse.cmake.org
0
0
July 15, 2023
How do i add my library, include and other files needed for statictic linking in vscode with c++ and cmake?
In this lib/cmake folder, there should be the CMake config file of SFML if installation of this lib has been properly done. The idea is to: add find_package(SFML REQUIRED) in your CMakeLists, and link appropriate sfml targets to your application with target_link_libraries() (for example target_link_libraries(myapp PRIVATE sfml-system sfml-window sfml-graphics)). add sfml install directory to CMAKE_PREFIX_PATH when you call cmake configuration of your project, so that above find_package(SFML REQUIRED) can find CMake config file of your SFML installation. Obviously you should revisit your project tree, you don't want to mix up SFML include and lib dirs in root dir of your project. Ideally it should be externally installed. But if you really want to vendor SFML binaries in your project (highly not cross-platform design), at least it should be properly isolated in a dedicated folder. More on reddit.com
🌐 r/cmake
2
0
May 31, 2024
Linking Header Only Library not working as expected

When you link app, don't use INTERFACE, use PUBLIC (or PRIVATE).

More on reddit.com
🌐 r/cmake
8
4
June 8, 2018
🌐
CMake
cmake.org β€Ί cmake β€Ί help β€Ί latest β€Ί guide β€Ί tutorial β€Ί Adding a Library.html
Step 2: Adding a Library β€” CMake 4.3.0 Documentation
Step 3: Adding Usage Requirements for a Library Β· Show Source Β· index Β· next | previous | CMake 4.3.0 Β» Β· Documentation Β» Β· CMake Tutorial Β» Β· Step 2: Adding a Library Β· Β© Copyright 2000-2026 Kitware, Inc. and Contributors.
🌐
CMake
cmake.org β€Ί cmake β€Ί help β€Ί latest β€Ί command β€Ί add_library.html
add_library β€” CMake 4.3.0-rc3 Documentation
Additional usage requirements may be specified in INTERFACE_* properties. An UNKNOWN library type is typically only used in the implementation of Find Modules. It allows the path to an imported library (often found using the find_library() command) to be used without having to know what type of library it is.
🌐
CMake
cmake.org β€Ί cmake β€Ί help β€Ί latest β€Ί variable β€Ί CMAKE_LIBRARY_PATH.html
CMAKE_LIBRARY_PATH β€” CMake 4.3.0-rc2 Documentation
There is also an environment variable CMAKE_LIBRARY_PATH, which is used as an additional list of search directories.
🌐
DaniWeb
daniweb.com β€Ί programming β€Ί software-development β€Ί threads β€Ί 437984 β€Ί cmake-include-path-cmake-library-path
c++ - CMAKE Include Path / CMAKE Library Path?? [SOLVED] | DaniWeb
If you change search paths after a first configure, clear the cache (delete CMakeCache.txt or the build directory) before re-configuring. ... On Windows, quote paths containing spaces. You can also use <PackageName>_ROOT to hint find_package for that specific package. If you truly need to hard-code custom locations, add HINTS/PATHS to your project’s find calls rather than changing global defaults. Docs: find_package, find_library, and CMAKE_PREFIX_PATH.
🌐
CMake Discourse
discourse.cmake.org β€Ί code
Library and include path - Code - CMake Discourse
November 28, 2023 - Hello, I am creating a library, and the associated test. Here is the files structure : ── MyLibrary β”œβ”€β”€ CMakeLists.txt β”œβ”€β”€ include β”‚ └── x.h β”œβ”€β”€ src β”‚ β”œβ”€β”€ CMakeLists.txt β”‚ β”œβ”€β”€ x.cpp β”‚ β”œβ”€β”€ y.h β”‚ └── y.cpp └── test β”œβ”€β”€ CMakeLists.txt β”œβ”€β”€ test.h └── test.cpp and where is what I do in the CMakeLists.txt inside the tesy directory : target_sources(${LIB_PROJECT_NAME} PUBLIC test.h test.c...
Find elsewhere
🌐
CMake
cmake.org β€Ί cmake β€Ί help β€Ί v3.0 β€Ί variable β€Ί CMAKE_LIBRARY_PATH_FLAG.html
CMAKE_LIBRARY_PATH_FLAG β€” CMake 3.0.2 Documentation
This documents an old version of CMake. Click here to see the latest release. The flag to be used to add a library search path to a compiler.
🌐
CMake
cmake.org β€Ί cmake β€Ί help β€Ί latest β€Ί command β€Ί find_library.html
find_library β€” CMake 4.3.0-rc3 Documentation
When a full path to a framework is used as a library, CMake will use a -framework A, and a -F<fullPath> to link the framework to the target. Added in version 3.28: The library found can now be a .xcframework folder.
🌐
CMake
cmake.org β€Ί cmake β€Ί help β€Ί latest β€Ί command β€Ί target_link_libraries.html
target_link_libraries β€” CMake 4.3.0 Documentation
Added in version 3.13: The <target> doesn't have to be defined in the same directory as the target_link_libraries call. ... A library target name: The generated link line will have the full path to the linkable library file associated with the target.
🌐
CMake Discourse
discourse.cmake.org β€Ί usage
How do I add a lib library path to a subpart of LLVM? - Usage - CMake Discourse
July 15, 2023 - Question I tried compiling llvm15 on Ubuntu with AArch64 architecture. However, I encountered the following problems: 1. /usr/bin/ld: cannot find -lgcc_s, some of the errors are as follows: [build] [73/4456 0% :: 1.026] Linking CXX static library lib/clang/15.0.1/lib/aarch64-unknown-linux-gnu/libclang_rt.fuzzer_no_main.a [build] FAILED: lib/clang/15.0.1/lib/aarch64-unknown-linux-gnu/libclang_rt.fuzzer_no_main.a [build] : && /usr/bin/cmake -E remove lib/clang/15.0.1/lib/aarch64-unknown-linux-...
🌐
CMake
cmake.org β€Ί cmake β€Ί help β€Ί v3.21 β€Ί guide β€Ί tutorial β€Ί Adding a Library.html
Step 2: Adding a Library β€” CMake 3.21.7 Documentation
To do this we change the end of the top-level CMakeLists.txt file to look like the following: ... if(USE_MYMATH) add_subdirectory(MathFunctions) list(APPEND EXTRA_LIBS MathFunctions) list(APPEND EXTRA_INCLUDES "${PROJECT_SOURCE_DIR}/MathFunctions") endif() # add the executable add_executable(Tutorial tutorial.cxx) target_link_libraries(Tutorial PUBLIC ${EXTRA_LIBS}) # add the binary tree to the search path for include files # so that we will find TutorialConfig.h target_include_directories(Tutorial PUBLIC "${PROJECT_BINARY_DIR}" ${EXTRA_INCLUDES} )
🌐
ICS
ics.com β€Ί blog β€Ί find-and-link-libraries-cmake
Find and Link Libraries with CMake | ICS
June 21, 2023 - To link, you can add zlib to your list. This will not ship the library when deploying your application. I’ll talk more on this topic later in our blog series. When find_package is called, CMake checks several directories for a find<PACKAGENAME>.cmake or <PACKAGE>Config.cmake. When found it will be used to set all the internal variables that Cmake will use for the library. Package search starts in the CMAKE_PREFIX_PATH ...
🌐
Reddit
reddit.com β€Ί r/cmake β€Ί how do i add my library, include and other files needed for statictic linking in vscode with c++ and cmake?
r/cmake on Reddit: How do i add my library, include and other files needed for statictic linking in vscode with c++ and cmake?
May 31, 2024 -

So i understand that i need to link my header files which are located in the include folder i also understand that i need to link the lib files in the lib folder.

But i am stuck.

For example if i want to include sfml and use it in my project do i just need to do the following add_library(root/lib) and include_directories(root/include/SFML).

This is my hieracy:

🌐
CMake
cmake.org β€Ί cmake β€Ί help β€Ί v3.0 β€Ί command β€Ί link_directories.html
link_directories β€” CMake 3.0.2 Documentation
Note that this command is rarely necessary. Library locations returned by find_package() and find_library() are absolute paths. Pass these absolute library file paths directly to the target_link_libraries() command. CMake will ensure the linker finds them.
🌐
CMake
cmake.org β€Ί cmake β€Ί help β€Ί v3.22 β€Ί guide β€Ί tutorial β€Ί Adding a Library.html
Step 2: Adding a Library β€” CMake 3.22.6 Documentation
The last few lines of the top-level CMakeLists.txt file should now look like: ... # add the MathFunctions library add_subdirectory(MathFunctions) # add the executable add_executable(Tutorial tutorial.cxx) target_link_libraries(Tutorial PUBLIC MathFunctions) # add the binary tree to the search path for include files # so that we will find TutorialConfig.h target_include_directories(Tutorial PUBLIC "${PROJECT_BINARY_DIR}" "${PROJECT_SOURCE_DIR}/MathFunctions" )
🌐
CMake
cmake.org β€Ί cmake β€Ί help β€Ί v3.25 β€Ί guide β€Ί tutorial β€Ί Adding a Library.html
Step 2: Adding a Library β€” CMake 3.25.3 Documentation
At this point, we have seen how to create a basic project using CMake. In this step, we will learn how to create and use a library in our project. We will also see how to make the use of our library optional. To add a library in CMake, use the add_library() command and specify which source ...
🌐
Blogger
tiebing.blogspot.com β€Ί 2015 β€Ί 03 β€Ί cmake-add-include-path-and-library-path.html
Journey of Life: cmake add include path and library path
March 12, 2015 - Cross Compile to ARM To cross compile to an embedded target, add the following lines to you CMakeLists.txt at the very top (after VERSION) SET(CMAKE_SYSTEM_NAME Linux) SET(CMAKE_C_COMPILER arm-none-linux-gnueabi-gcc) Add include paths include_directories("${PROJECT_SOURCE_DIR}/../" "${PROJECT_SOURCE_DIR}/build/" "${PROJECT_SOURCE_DIR}") Add a library FIND_LIBRARY(json json-c json) 1st parameter: output, The variable that will hold the results of FIND_LIBRARY 2nd parameter: the library to look for (in this case, libjson-c.so) 3rd parameter: the path to look, starting with ${PROJECT_SOURCE_DIR} The returned varilable can be used in the following statement to add the library to link time: TARGET_LINK_LIBRARIES(${json}) Assign a variable Β·
🌐
CMake
cmake.org β€Ί cmake β€Ί help β€Ί latest β€Ί command β€Ί target_link_directories.html
target_link_directories β€” CMake 4.2.3 Documentation
Each item specifies a link directory and will be converted to an absolute path if necessary before adding it to the relevant property. Repeated calls for the same <target> append items in the order called. If BEFORE is specified, the content will be prepended to the relevant property instead of being appended. Arguments to target_link_directories may use generator expressions with the syntax $<...>. See the cmake-generator-expressions(7) manual for available expressions.