Add Library Search Path to Clang

Add library search path to clang

Is it OK to add it as environment variable?

This should work:

export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/lib

Notice, it's LIBRARY_PATH, not LD_LIBRARY_PATH.

On the other hand, if that doesn't work for you, you should compile with the flag:

-L/usr/local/lib

And that should be sufficient, too.

EDIT: Btw, I don't know why you're using back-slashes instead of slashes... that needs explanation. Use slashes always. Even on Windows.

how to add include paths to clang globally

There are also some environment variables which Clang looks at for include paths. For c++, they would be CPATH (both C and C++) and CPLUS_INCLUDE_PATH (C++ only) (And LIBRARY_PATH for the linker). So you can add something like this to your shell startup file if you are using bash or similar:

export CPLUS_INCLUDE_PATH="${CPLUS_INCLUDE_PATH:+${CPLUS_INCLUDE_PATH}:}<dir>"

And you could also just alias clang++ with clang++ -I<dir>.

clang++ library not found even when providing library path

Normally when you give libraries to link with the -l flag, you omit the lib prefix. For example: "-lglfw3" links the file "libglfw3.a". It looks like you should change your -llibglfw3 option.

How to specifiy library search path with clang

It's just -L, the Clang man pages are apparently still horridly incomplete.

Setting path to Clang library in CMake

The variable is Clang_DIR.

Just in case, I attach a minimalistic example of CMakeLists.txt file as well.

cmake_minimum_required(VERSION 3.12)

# Find CMake file for Clang
find_package(Clang REQUIRED)

# Add path to LLVM modules
set(CMAKE_MODULE_PATH
${CMAKE_MODULE_PATH}
"${LLVM_CMAKE_DIR}"
)

# import LLVM CMake functions
include(AddLLVM)

include_directories(${LLVM_INCLUDE_DIRS})
include_directories(${CLANG_INCLUDE_DIRS})

add_definitions(${LLVM_DEFINITIONS})
add_definitions(${CLANG_DEFINITIONS})

add_llvm_executable(myTool main.cpp)
set_property(TARGET myTool PROPERTY CXX_STANDARD 11)
target_link_libraries(myTool PRIVATE clangTooling)

How to query the default include paths of clang++?

You are looking for option -v. Compiling with clang++ -c file.cc -v will print among other things:

#include "..." search starts here:
#include <...> search starts here:
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/../../../../include/c++/4.9

etc.

How to specify preference of library path?

Add the path to where your new library is to LD_LIBRARY_PATH (it has slightly different name on Mac ...)

Your solution should work with using the -L/my/dir -lfoo options, at runtime use LD_LIBRARY_PATH to point to the location of your library.

Careful with using LD_LIBRARY_PATH - in short (from link):

..implications..:

Security: Remember that the directories specified in LD_LIBRARY_PATH get searched before(!) the standard locations? In that
way, a nasty person could get your application to load a version of a
shared library that contains malicious code! That’s one reason why
setuid/setgid executables do neglect that variable!

Performance: The link loader has to search all the directories specified, until it finds the directory where the shared library
resides – for ALL shared libraries the application is linked against!
This means a lot of system calls to open(), that will fail with
“ENOENT (No such file or directory)”! If the path contains many
directories, the number of failed calls will increase linearly, and
you can tell that from the start-up time of the application. If some
(or all) of the directories are in an NFS environment, the start-up
time of your applications can really get long – and it can slow down
the whole system!

Inconsistency: This is the most common problem. LD_LIBRARY_PATH forces an application to load a shared library it wasn’t linked
against, and that is quite likely not compatible with the original
version. This can either be very obvious, i.e. the application
crashes, or it can lead to wrong results, if the picked up library not
quite does what the original version would have done. Especially the
latter is sometimes hard to debug.

OR

Use the rpath option via gcc to linker - runtime library search path, will be used
instead of looking in standard dir (gcc option):

-Wl,-rpath,$(DEFAULT_LIB_INSTALL_PATH)

This is good for a temporary solution. Linker first searches the LD_LIBRARY_PATH for libraries before looking into standard directories.

If you don't want to permanently update LD_LIBRARY_PATH you can do it on the fly on command line:

LD_LIBRARY_PATH=/some/custom/dir ./fooo

You can check what libraries linker knows about using (example):

/sbin/ldconfig -p | grep libpthread
libpthread.so.0 (libc6, OS ABI: Linux 2.6.4) => /lib/libpthread.so.0

And you can check which library your application is using:

ldd foo
linux-gate.so.1 => (0xffffe000)
libpthread.so.0 => /lib/libpthread.so.0 (0xb7f9e000)
libxml2.so.2 => /usr/lib/libxml2.so.2 (0xb7e6e000)
librt.so.1 => /lib/librt.so.1 (0xb7e65000)
libm.so.6 => /lib/libm.so.6 (0xb7d5b000)
libc.so.6 => /lib/libc.so.6 (0xb7c2e000)
/lib/ld-linux.so.2 (0xb7fc7000)
libdl.so.2 => /lib/libdl.so.2 (0xb7c2a000)
libz.so.1 => /lib/libz.so.1 (0xb7c18000)


Related Topics



Leave a reply



Submit