How to use shared library on Linux by C++?

Hi all.
At first, thank for yours Tutorial, read several times, I even understood half of it, but I don't memorized it yet.
My wish - make one webpage, if it succeeds.
My luggage is Debian11, and heap the refers after request: `How to use shared library on Linux by C++?` I realize need find out all this, and it be long time. For this demands to load and unload many apps for tests.
Hence first question- May I establishing packages of libraries into separate folder and then unpack and deploy as wrote its instruction. If I do many errors ,then I clear folder and try something other.
Also please tell me apps for making web-page, that use GNUmake. Of course I'm looking for it myself, but English is bad and I don't know much.
If possible please suggest all what need for create web-page.
I can't answer quickly, but I read everything carefully.
Thanks everyone.
What exactly do you mean with "use" a shared library?

When you compile the source that uses the shared library – doesn't really matter whether the library is "shared" though – the library itself (.so file) is not really involved. Instead, you have to #include the header file (.h or .hpp) that defines the public interface of the library. And, of course, you have to make sure that the required header file is found in the compiler's search path (e.g. by using the -I option of the compiler).

When you link your program, then you link the required library (.so file), by using the -l option of the compiler. For example, in order to link the library "libfoo.so", you would use -lfoo. Also, you have to make sure that the required library file is found in the linker's search path (e.g. by using the -L option of the linker).

Finally, at runtime, the "shared" library (.so file) needs to be available in one of the paths that the operating system's loader is scanning, such as /usr/lib[64] or /usr/local/lib[64]. The directory, from which to load the "shared" library can also be adjusted, at runtime, by using the LD_LIBRARY_PATH environment variable.

Also note:
If both static and shared libraries are found, the linker gives preference to linking with the shared library unless the -static option is used.


Example:
1
2
3
4
5
6
7
8
9
10
# Compile
g++ -I/path/to/library/include -c source1.cpp
g++ -I/path/to/library/include -c source2.cpp

# Link
g++ -L/path/to/library/lib -o program.out source1.o source2.o -lfoo

# Run
export LD_LIBRARY_PATH=/path/to/library/lib
./program.out

...assuming that the "shared" library libfoo.so, which we want to use, resides in directory /path/to/library/lib, and that its associated header files reside in directory /path/to/library/include.

Source code file source1.cpp may look something like this:
1
2
3
4
5
6
7
#include <foo.hpp>   // <-- declares public interface of "libfoo" library

main()
{
    foo::SomeClassFromLibFoo instance;
    instance.doSomething();
}

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

BTW: Optionally, by using the linker option -Wl,-rpath,$ORIGIN, you can create an executable that loads its "shared" libraries from the same directory where the executable file is located.
Last edited on
Hi all.
Thanks to kigar, a lot has become clear, I feel that with the help of practice, tips and guides, I will figure it out. But where to practice and what? I have the sad experience of installing through the "Synaptic", when, without knowledge and experience, I tried to install the necessary programs to create one web page. And after that I could not delete everything unnecessary to the end, now I want to install compressed programs myself in a separate folder and I hope that after all the necessary deployments nothing will go beyond this folder. Am I right? Now I keep empty: / usr / local / bin, / usr / local / games, / usr / games, where to install?

Now the question is, what to install?
I tried to get acquainted with: Nana, SFML, gtkmm, FOX toolkit, wxWidgets, POCO, FLTK, but even now I do not fully understand their difference, and what is needed to create one web page. Nana and SFML seemed to me the most inappropriate, they require "CMake".
POCO, FLTK and FOX toolkit seem to be more reliable.
Tips help a lot, for example George P mentioned http://www.learncpp.com/ somewhere here and I found out an excellent tutorial!
Thanks everyone.
It is not clear what you actually want to do. Can you please clarify?


First your were asking for instructions on how to use "shared" libraries. Since you were asking this on a C++ developer forum, I tried to give you a basic idea on how you'd use a "shared" library in a C++ program.

Now you are talking about "create one web page". But, to create a web-page (i.e. run a web-server) you do not usually need to develop a program yourself. Instead, you'd rather just install a web-server, such as Apache HTTPD or Nginx, then put your HTML+CSS files into the "www root" directory, and that is pretty much it!

In order to install Apache web-server on Debian (or Debian-base distribution) see here:
https://www.digitalocean.com/community/tutorials/how-to-install-the-apache-web-server-on-debian-10

(or see one of the other 100 tutorials about this matter that you'll find on the Internet)


If you need your web-site to be more dynamic than just "static" HTML, the usual choices would include PHP, Perl or Python, which integrate nicely with a web-server like Apache HTTPD or Nginx. In theory, it is also possible to write your own Apache "module" in C/C++, but this is not usually necessary or advisable...

See also:
https://computingforgeeks.com/install-php-on-debian-linux-systen/
Last edited on
Hi all.
I'm interested in C ++, but you must admit that dwelling only on the CLI is stupid, and it is impossible to get acquainted with the GUI without shared libraries and GNUmake! And I don't want to mess with fancy things, so that I can not depend on them later (IDE, etc.)
I would be fine with "just" static "HTML" made with POCO, FLTK, or the FOXtoolkit.
------------------
Why did I disturb your site ?:
your Tutorial is only 1.2MB pdf file, 144 pages, but complete clarity and desire to go further.
post kigar64551 (83) only 2.246 letters - the same.
And all this will be read by many people. C ++ itself enforces clarity and brevity.
----------
newbieg (632) ".. I'm going to guess your language is Arabic", - wrong, Russian.
Thanks everyone.
I have the sad experience of installing through the "Synaptic", when, without knowledge and experience, I tried to install the necessary programs to create one web page. And after that I could not delete everything unnecessary to the end

Isn't the "Synaptic" a front-end to package manager? Manager installs files from packages and knows what belongs to a package. Therefore, it will remove the files that were installed from package, when you uninstall that package.

"just" static "HTML"

That I have written with plain text editor. No idea what the POCO, FLTK, or the FOXtoolkit are, but if any of them have been packaged for your distro, then you should use package manager to install them.

... they require "CMake".

The CMake, GNU Build System, QMake, etc generate "Makefile" from more generic instruction for make so that the Makefile is appropriate for current system. Very convenient.

I'm interested in C ++, but you must admit that dwelling only on the CLI is stupid, and it is impossible to get acquainted with the GUI without shared libraries and GNUmake!

As said, C++ has practically nothing to do with "web pages". GUI desktop is separate from "web pages" too.

Your distro does most likely provide GUI desktop environment. You don't need to write any code in order to use one. If you do write GUI applications, then yes, building them with help of GUI framework (libraries) makes sense. That, however, is no different from building CLI applications; they do use libraries too. Lets take a sample program from tutorials:
1
2
3
4
5
6
7
// my first program in C++
#include <iostream>

int main()
{
  std::cout << "Hello World!";
}

$ g++ -W -Wall -o sample sample.cpp
$ file sample
sample: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=83e387396826789d463cfaa926d9792b751f2209, not stripped
$ ldd sample
	linux-vdso.so.1 (0x00007ffd111d5000)
	libstdc++.so.6 => /lib64/libstdc++.so.6 (0x00007fd989532000)
	libm.so.6 => /lib64/libm.so.6 (0x00007fd9891b0000)
	libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007fd988f98000)
	libc.so.6 => /lib64/libc.so.6 (0x00007fd988bd3000)
	/lib64/ld-linux-x86-64.so.2 (0x00007fd9898c7000)

The (CLI) executable clearly uses multiple dynamically linked libraries.
I would be fine with "just" static "HTML"

Then, as said before, there should be no need to resort to C/C++ programming.

...unless this is for your own education and you want to learn how to implement a simple web-server in C/C++. But, then again, you would be re-inventing the wheel! Also, using a "home-grown" web-server implementation is nothing you would use in a "production" environment – except for very special uses-cases maybe.


If your goal is to just "run" a web-site:

Install a web-server, such as Apache HTTP or Nginxusually via the package manager of your distribution – and that's it! I have already given links to tutorials, that do exactly this, in my previous post.


As for creating and editing your HTML, CSS, JavaScript, etc. pp. files to be served by the web-server, maybe have a look at Visual Studio Code, but there really is a zillion of suitable "code editors" out there:

https://code.visualstudio.com/docs/setup/linux


... they require "CMake".
The CMake, GNU Build System, QMake, etc generate "Makefile" from more generic instruction for make so that the Makefile is appropriate for current system. Very convenient.

It should be noted that you only need Makefiles (and thus tools like CMake, which generate the Makefile), if you actually want/need to build the software yourself, from the source codes.

But: That is not normally required. Instead, you can just install the software, as pre-compiled binaries, from the package manager of your distribution. If the specific software you want to install is "missing" in the package repository of your distribution, maybe that software has a PPA or is offering .deb packages for download.

https://linuxconfig.org/install-packages-from-an-ubuntu-ppa-on-debian-linux


(Nowadays, more and more software is also provided as "containers" via Flatpak, Snap, etc. pp.)
Last edited on
Hi all.
Regarding the last 2 answers.
I know about the ability to download binaries through a file manager and have used it a lot. The results were as follows:
many tens of MVs were loaded in different places of the comp,
the sample of example ran very well,
when going to the site of the creators of this package, it turned out that this version is not supported, and for further work it is necessary to download hundreds more MB of third-party software.
The file manager does not delete everything, something remains, and my experiments-attempts remain. After 3 beautiful pictures-examples, even I realized that I have to start with the basics, and not with "C ++ in 21 days".
I reinstalled the OS to get rid of the garbage, and now I save the empty folders where I myself will install, unpack and deploy the packages. The purpose of this is purely educational, although it will not be a profession.
@keskiverto your example made to me download GCC.pdf,
and before that @kigar forced me GNU make to load.
Thanks everyone.
Downloading binaries "through a file manager" is something very different from installing software packages via the package manager of your Linux distribution! The package manager does not simply download the files, it also puts them where they belong (e.g. executables are installed to /usr/bin, and libraries are installed to /usr/lib). The package manager also runs required "post-install" actions when a software is installed, e.g. create or update configuration files as needed. Finally, the package manager resolves required dependencies.

When you properly use the package manager of your Linux distribution to install software, then you never should have to "install" (e.g. copy or move) any executables files or library files manually. The package manager does all that for you! The files will be installed to the place that the author of the package intended.

The package manager also keeps track of which software packages have been installed. Removing these software packages is just as easy! You simply tell the package manager which package you want to remove and that's it! You do not need to "delete" any files manually. The package manager does that four you!


Note: When a software is removed via the package manager, some files (e.g. configuration files) may be left behind. That is intentional, so you don't loose your stuff! For example, the package manager APT on Debian has a command remove to remove just the installed program files and libraries (but no configuration files, etc. pp.) and it additionally has a command purge to remove everything (including configuration files etc. pp.)

Note²: Sometimes, when you install a software package via the package manager, some additional packages may automatically be pulled in, because they are dependencies of the package you wanted to install. Now, if you later remove the package, those decencies may not automatically be removed too. APT has a command autoremove to remove all packages that were installed as dependencies and are no longer needed!


💡Learn how to properly use the package manager of your Linux distribution + trust the package manager.
Last edited on
The file manager does not delete everything, something remains, and my experiments-attempts remain.

Of course. The package manager removes the files that it did add during install. It definitely should not touch completely unrelated files. In my distro it also leaves any custom config that I made, again because they are "my creation", not from any package.

User data, like web pages, is that: data. Data should be put in clearly separate place from programs. When it is in distinct place, it is also easy to clean out. But, you create data and therefore you have to do the cleanup too.

when going to the site of the creators of this package, it turned out that this version is not supported, and for further work it is necessary to download hundreds more MB of third-party software.

If a Linux distro has packages for software, they do maintain the version that they offer. The distro probably has some reason, why they have a specific version.

If distro does not provide a software, then there might still be third-party repositories that offer the software as package for that distro.

If a distro does not offer (as packages) the softwares that you will use, then consider finding a distro that does. The point of package management is that it helps you manage your system.
keskiverto wrote:
If distro does not provide a software, then there might still be third-party repositories that offer the software as package for that distro.

+1

(BTW: the same applies, if the dirstro does provide the software, but the distro's version is too old)

keskiverto wrote:
If a distro does not offer (as packages) the softwares that you will use, then consider finding a distro that does. The point of package management is that it helps you manage your system.

+1
Last edited on
Hi all.
May be someone answer.

$ gunzip poco-X.Y.tar.gz
$ tar -xf poco-X.Y.tar
$ cd poco-X.Y
$ ./configure
$ gmake -s

I did all except gmake -s, since forgot, ./configure was ~1hour. I neatly copied 'DateTime' and its dependencies from (/usr/games/poco-1.11.1/Foundation/samples/DateTime) into apart folder, and tried look at 'what it is'.
After I did '$ gmake' and got:

user@debian:/usr/games/poco-1.11.1$ gmake
gmake -C /usr/games/poco-1.11.1/Foundation
gmake[1]: Entering directory '/usr/games/poco-1.11.1/Foundation'
gmake[1]: Nothing to be done for 'all'.
gmake[1]: Leaving directory '/usr/games/poco-1.11.1/Foundation'
.............
..........
gmake -C inflate
gmake[2]: Entering directory '/usr/games/poco-1.11.1/Foundation/samples/inflate'
gmake[2]: Nothing to be done for 'all'.
gmake[2]: Leaving directory '/usr/games/poco-1.11.1/Foundation/samples/inflate'
gmake -C DateTime
gmake[2]: Entering directory '/usr/games/poco-1.11.1/Foundation/samples/DateTime'
** Compiling src/DateTime.cpp (debug, shared)
g++ -Iinclude -I/usr/games/poco-1.11.1/CppUnit/include -I/usr/games/poco-1.11.1/Foundation/include -I/usr/games/poco-1.11.1/Encodings/include -I/usr/games/poco-1.11.1/XML/include -I/usr/games/poco-1.11.1/JSON/include -I/usr/games/poco-1.11.1/Util/include -I/usr/games/poco-1.11.1/Net/include -std=c++14 -Wall -Wno-sign-compare -DPOCO_BUILD_HOST='"'debian'"' -DPOCO_TARGET_OSNAME='"'Linux'"' -DPOCO_TARGET_OSARCH='"'i686'"' -D_XOPEN_SOURCE=600 -D_REENTRANT -D_THREAD_SAFE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE -DPOCO_HAVE_FD_EPOLL -g -D_DEBUG -fPIC -c src/DateTime.cpp -o /usr/games/poco-1.11.1/Foundation/samples/DateTime/obj/Linux/i686/debug_shared/DateTime.o
src/DateTime.cpp:13:10: fatal error: LocalDateTime.h: No such file or directory
13 | #include "LocalDateTime.h"
| ^~~~~~~~~~~~~~~~~
compilation terminated.
gmake[2]: *** [/usr/games/poco-1.11.1/build/rules/compile:60: /usr/games/poco-1.11.1/Foundation/samples/DateTime/obj/Linux/i686/debug_shared/DateTime.o] Error 1
gmake[2]: Leaving directory '/usr/games/poco-1.11.1/Foundation/samples/DateTime'
gmake[1]: *** [Makefile:19: projects] Error 2
gmake[1]: Leaving directory '/usr/games/poco-1.11.1/Foundation/samples'
gmake: *** [Makefile:137: Foundation-samples] Error 2
user@debian:/usr/games/poco-1.11.1$ .

I, of course, did not undestand nothing, should parse this later, but one question: really 'gmake' gave errors becaus I neatly copied 'DateTime'?
Thanks everyone.
New question? Please use a new thread.
Topic archived. No new replies allowed.