Linking shared, static, and dynamic library

307 views Asked by At

I built and installed the source code of spidermonkey, I want to link its library and its include folder with my C++ application, so I wrote this command on linux

 g++ -I/home/SpiderMonkey/js-1.8.5/js/src/dist/include 
 -L/home/SpiderMonkey/js1.8.5/js/src/dist/lib -lmozjs185  helloworld.cpp -o

but it gives me undefined reference error, so what is the problem with this command: the path which the src code was built in is home/SpiderMonkey/js1.8.5/src and when I installed the src code, its libs and include folder exist now in usr/local/lib and usr/local/include/js respectively, so which path should I use in the command to compile my hellowrorld.cpp. Also, I found 3 libraries with the same name but with different extension:

libname.so, libname.so.1.0, libname.so.1.0.0 and libname.a 

which library should I use and what are the differences?

1

There are 1 answers

4
Lightness Races in Orbit On BEST ANSWER

Put the libraries after your main.cpp on the compilation command. The linker determines which symbols to pull out of those libraries based on the objects it's already seen, left to right.

As for your addendum question:

  • libname.a is for static linking.

  • libname.so, libname.so.1.0 and libname.so.1.0.0 are all the same file, just with names that give you an increasing level of version information granularity.

Which to use is beyond the scope of this answer, but you can find plenty of information about that already existing on the web.