How to include a shared library in gn

1.2k views Asked by At

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?

1

There are 1 answers

0
Charles Nicholson On

This line looks suspicious:

  lib_dirs = [ "//usr/lib/sampleFile.so" ]

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_dirs

You're giving it a file, not a directory.

Instead, try:

  lib_dirs = [ "/usr/lib" ]

That should change your link line to

g++ -Wl,-rpath=\$ORIGIN/ -Wl,-rpath-link= -L/usr/lib -o main -Wl,--start-group @main.rsp  -Wl,--end-group -lSampleDll.so

Which is maybe closer to what you were expecting; the linker sees the required SampleDll.so and will search /usr/lib for it.