I have a BUILD.gn in which I want to include a shared library which is located in usr/lib
.
I have taken reference from this topic How to include a shared library in chromium's gn file?
But the .so file is not getting linked with the main function. Below is the BUILD.gn code:-
executable("check") {
sources = [ "check.cpp" ]
deps = [
":SampleCheck",
]
lib_dirs = [ "//usr/lib/SampleFile.so" ]
libs = [ "SampleFile" ]
}
shared_library("SampleCheck") {
sources = [
"SampleCheck.h", // Header file for functions
]
}
But, when executing this I am getting an error:-
ninja: Entering directory `out'
[0/1] Regenerating ninja files
[1/1] LINK main
FAILED: main
g++ -Wl,-rpath=\$ORIGIN/ -Wl,-rpath-link= -L../usr/lib/SampleFile.so -o main -Wl,--start-group @main.rsp -Wl,--end-group -lSampleDll.so
/usr/bin/ld: cannot find -lSampleDll.so
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
I have surveyed many websites but I am not getting where am i wrong?
This line looks suspicious:
GN uses
//
to denote the root of the GN project, as marked by the presence of a.gn
file.lib_dirs
is aware of the absolute filesystem to allow constructions like yours; you can just use a single forward slash to indicate the root filesystem.Additionally, the intent of
lib_dirs
is to add directories to the linker's search path: https://gn.googlesource.com/gn/+/master/docs/reference.md#var_lib_dirsYou're giving it a file, not a directory.
Instead, try:
That should change your link line to
Which is maybe closer to what you were expecting; the linker sees the required
SampleDll.so
and will search/usr/lib
for it.