0

I have a c++ program, which contains two classes. one of them is using libssh and some of its functions and another one is for calculating cpu usage. there is a link of how I a built and added libssh:libssh's functions couldn't be found on qt
my program works fine. now I want to build a .so library out of it to use in other programs. first I made two .o file like this:

 gcc -c -fPIC info.cpp -o info.o
 gcc -c -fPIC cpuusage.cpp -o cpuusage.o

and I made a .so from them:

gcc -shared -o libsmc.so info.o cpuusage.o

whenever I want to use libsmc.so, I include info.h, but the problem is that libssh functions cannot be found. I think I have to add libssh statically to my project. but I don't know how to!
Ps:I read this explanation :Using a shared library in another shared library , but this is for linking shared libraries that have been used in a program via command line, I don't wanna compile program with command line and want to link libraries constantly.

fa7eme
  • 73
  • 10
  • I found my answer after 2 days in here :https://stackoverflow.com/questions/19424494/linking-a-shared-library-with-another-shared-lib-in-linux – fa7eme Jul 30 '20 at 19:04

1 Answers1

0

To build a C/C++ software using external libraries, I would really recommend to use a build system instead of typing commands manually.

The most used build system for C++ is CMake (https://cmake.org/), which is well supported by Qt, but there are many other build systems existing. Another is QMake, which is Qt's build system. If you are using an IDE, like QtCreator or Microsoft Visual Studio, CMake is integrated as well. There are plenty of tutorials and example to use CMake for a project (e.g. https://mirkokiefer.com/cmake-by-example-f95eb47d45b1), even though the learning curve is not as steep as I would want.

But if you still want to use command line (or to debug the command line generated by CMake): When linking against a library, you need to:

  1. Give the include path to the compiler, i.e. the path where the .h of the external library can be found. With gcc, this is done with -I, e.g. "-I/usr/lib/mylib/include".
  2. Give the library folder and name to the linker, i.e. the path where to find the compiled library, as well as its name. With gcc, this is done with -L for the path and -l for the name. Check the gcc manual for more details about these commands.

And if you want to use CMake, then you can use the functions:

target_include_directories(..)

and

target_link_libraries(..)
Alexandre
  • 498
  • 2
  • 8
  • I know what you mean, but my question isn't about using a library in my code, it is about that I have made a shared library from my code that it itself uses another shared library named `libssh`, I want to use my own library anywhere, but it has dependencies to `libssh`. I want to get rid of those dependencies, meanwhile I am using `libssh` in my code. maybe it means that I should using `libssh` statically, not dynamically. but I don't know how to. – fa7eme Jul 28 '20 at 15:36
  • 1
    I think you have two options: a) To get rid of the dependency, yes you need to link statically. But this is not always allowed, you need to check the license of libssh to know whether this is allowed. To link statically, I think you should give gcc a parameter -l with the full path to the lib .a (linux) or .lib (Windows). b) If the license if not OK for static linking, then accept the dependency and distribute libssh with your software. This is very common as well. If you dont put libssh include in your .h but only in .cpp, users wont need the includes of libssh. – Alexandre Jul 28 '20 at 16:15