Dynamically loaded libraries linux

C++ Dynamic Shared Library on Linux

This is a follow-up to Dynamic Shared Library compilation with g++. I’m trying to create a shared class library in C++ on Linux. I’m able to get the library to compile, and I can call some of the (non-class) functions using the tutorials that I found here and here. My problems start when I try to use the classes that are defined in the library. The second tutorial that I linked to shows how to load the symbols for creating objects of the classes defined in the library, but stops short of using those objects to get any work done. Does anyone know of a more complete tutorial for creating shared C++ class libraries that also shows how to use those classes in a separate executable? A very simple tutorial that shows object creation, use (simple getters and setters would be fine), and deletion would be fantastic. A link or a reference to some open source code that illustrates the use of a shared class library would be equally good. Although the answers from codelogic and nimrodm do work, I just wanted to add that I picked up a copy of Beginning Linux Programming since asking this question, and its first chapter has example C code and good explanations for creating and using both static and shared libraries. These examples are available through Google Book Search in an older edition of that book.

I’m not sure I understand what you mean by «using» it, once a pointer to the object is returned, you could use it like you use any other pointer to an object.

The article I linked to shows how to create a function pointer to an object factory function using dlsym. It doesn’t show the syntax for creating and using objects from the library.

You will need the header file describing the class. Why do you think you have to use «dlsym» instead of just letting the OS find and link the library at load time? Let me know if you need a simple example.

@nimrodm: What’s the alternative to using «dlsym»? I’m (supposed to be) writing 3 C++ programs that will all use the classes defined in the shared library. I also have 1 Perl script that will use it, but that’s a whole other problem for next week.

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Читайте также:  Можно ли обновить линукс

Dynamic loading c++ from multiple Linux libraries

License

Vladimir-Novick/Dynamic-Loading-Linux-Libraries

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

Dynamic loading c++ from multiple Linux libraries

Dynamically loaded libraries are libraries that are loaded at times other than during the startup of a program. They’re particularly useful for implementing plugins or modules, because they permit waiting to load the plugin until it’s needed.

1) 1.1 Install standard Nodejs ( v10.x )

 $ sudo apt update $ sudo apt install nodejs $ sudo npm install --global npm@latest $ node -v 

1.2 Installing Node.js with Apt Using a NodeSource PPA ( v12, v14, and v16 )

1.2.1 install nodeJs v16 These PPAs have more versions of Node.js available than the official Ubuntu repositories. Node.js v12, v14, and v16 are available as of the time of writing. $ cd ~ $ curl -sL https://deb.nodesource.com/setup_16.x -o nodesource_setup.sh $ sudo apt update $ sudo bash nodesource_setup.sh $ sudo apt-get install -y nodejs $ sudo npm install --global npm@latest $ node -v 1.2.1 install the Yarn package manage: $ curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --dearmor | sudo tee /usr/share/keyrings/yarnkey.gpg >/dev/null $ sudo apt-get update && sudo apt-get install yarn 

2) install node-gyp to NodeJs

 $ sudo npm install --global npm@latest $ sudo npm install --global node-gyp@latest $ sudo npm config set node_gyp $(npm prefix -g)/lib/node_modules/node-gyp/bin/node-gyp.js $ python3 --version $ python3 -m pip --version 

3) install Python v3.8, or v3.9

 3.1.1 - install: $ sudo apt-get install software-properties-common $ sudo add-apt-repository ppa:deadsnakes/ppa $ sudo apt-get update $ sudo apt-get install python3.8 3.1.2 - check python $ python3 --version $ python3 -m pip --version 3.2 install Setuptools & Pip The two most crucial third-party Python packages are setuptools and pip. Python v3.8 include pip by default. To see if pip is installed, open a command prompt and run $ command -v pip3 

4) development tools to build native addons:

 4.1 GCC $ sudo apt-get update $ sudo apt-get install gcc g++ make 4.2 build-essential $ sudo apt-get update $ sudo apt install build-essential 4.3 Check G++ version $ g++ --version 

5) Installing Visual Studio Code

 5.2) for Ubuntu-Desktop $ sudo apt update $ sudo apt install software-properties-common apt-transport-https wget $ wget -q https://packages.microsoft.com/keys/microsoft.asc -O- | sudo apt-key add - $ sudo add-apt-repository "deb [arch=amd64] https://packages.microsoft.com/repos/vscode stable main" $ sudo apt install code $ sudo apt update $ sudo apt upgrade 5.1) install remote wsl to VS code to WSL https://code.visualstudio.com/docs/remote/wsl-tutorial 
 6.1) Git is likely already installed in your Ubuntu 20.04 server $ git --version Output git version 2.25.1 $ sudo apt update $ sudo apt install git 6.2) Setting Up Git $ git config --global user.name "Your Name" $ git config --global user.email "youremail@domain.com" $ git config --list $ nano ~/.gitconfig 
 $ ssh-keygen -t ed25519 ( empty for no passphrase) $ nano /home/vlad/.ssh/id_ed25519.pub ( copy contents and insert to website gitlab : preferenses -> user settings -> SSH keys ) 

Setting up WSL to Visual Studio 2019

Читайте также:  Linux server web интерфейс

 sudo apt update sudo apt install g++ gdb make ninja-build rsync zip sudo dpkg-reconfigure openssh-server sudo /usr/sbin/sshd -Dddde output: debug2: load_server_config: filename /etc/ssh/sshd_config debug2: load_server_config: done config len = 371 debug2: parse_server_config_depth: config /etc/ssh/sshd_config len 371 debug2: /etc/ssh/sshd_config line 13: new include /etc/ssh/sshd_config.d/*.conf debug2: /etc/ssh/sshd_config line 13: no match for /etc/ssh/sshd_config.d/*.conf debug3: /etc/ssh/sshd_config:15 setting Port 2828 debug3: /etc/ssh/sshd_config:16 setting AddressFamily any debug3: /etc/ssh/sshd_config:17 setting ListenAddress 127.0.0.1 debug3: /etc/ssh/sshd_config:58 setting PasswordAuthentication yes debug3: /etc/ssh/sshd_config:63 setting ChallengeResponseAuthentication no debug3: /etc/ssh/sshd_config:86 setting UsePAM yes debug3: /etc/ssh/sshd_config:91 setting X11Forwarding yes debug3: /etc/ssh/sshd_config:95 setting PrintMotd no debug3: /etc/ssh/sshd_config:113 setting AcceptEnv LANG LC_* debug3: /etc/ssh/sshd_config:116 setting Subsystem sftp /usr/lib/openssh/sftp-server debug1: sshd version OpenSSH_8.2, OpenSSL 1.1.1f 31 Mar 2020 debug1: private host key #0: ssh-rsa . debug1: private host key #1: ecdsa-sha2-. debug1: private host key #2: ssh-ed25519 . debug1: rexec_argv[0]='/usr/sbin/sshd' debug1: rexec_argv[1]='-Dddde' debug3: oom_adjust_setup debug1: Set /proc/self/oom_score_adj from 0 to -1000 debug2: fd 3 setting O_NONBLOCK debug1: Bind to port 2828 on 127.0.0.1. Bind to port 2828 on 127.0.0.1 failed: Address already in use. Cannot bind any address. 
 sudo systemctl status sshd sudo systemctl daemon-reload if got this error: System has not been booted with systemd as init system (PID 1). Can't operate. Failed to connect to bus: Host is down try it: sudo apt-get update && sudo apt-get install -yqq daemonize dbus-user-session fontconfig sudo daemonize /usr/bin/unshare --fork --pid --mount-proc /lib/systemd/systemd --system-unit=basic.target exec sudo nsenter -t $(pidof systemd) -a su - $LOGNAME snap version sudo dpkg-reconfigure openssh-server sudo systemctl enable sshd sudo systemctl status sshd sudo service ssh start 

install WSL2 (Windows Subsystem for Linux 2) on Windows 10

 https://pureinfotech.com/install-windows-subsystem-linux-2-windows-10/#install_wsl_command_1909_windows10 

Visual studio 2019 Connect to WSL

 sudo apt install g++ gdb make ninja-build rsync zip https://docs.microsoft.com/en-us/cpp/linux/configure-a-linux-project?view=msvc-170 Requirements: Windows 10 May 2020 (2004), Windows 10 May 2019 (1903), or Windows 10 November 2019 (1909) or later A computer with Hyper-V Virtualization support Step by Step explanation: https://www.ceos3c.com/wsl-2/install-wsl2-with-windows-terminal/ 

In your WSL instance, sudo nano /etc/wsl.conf

inser with the following contents:

Remote Debug Support in Visual Studio 2019

 https://devblogs.microsoft.com/cppblog/remote-debug-support-in-visual-studio-2019/ 

#INSTALL OPENSSH-SERVER IN WSL

First, install OpenSSH server inside your Linux Distro:

 sudo apt install openssh-server [sudo] password for scott: Reading package lists. Done Building dependency tree Reading state information. Done openssh-server is already the newest version (1:7.6p1-4ubuntu0.3). 0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded. 

sudo nano /etc/ssh/sshd_config

 . STUFF ABOVE THIS. Port 2828 #AddressFamily any ListenAddress 127.0.0.1 #ListenAddress :: 

#Using Node-Gyp on Linux code

 npm install -g node-gyp or npm install -g node-gyp node-pre-gyp 

About

Dynamic loading c++ from multiple Linux libraries

Читайте также:  Start selenium server linux

Источник

dynamically loading static library?

A static library is more or less just a collection of object files. If you want to use a static library in a program, you have to link the executable with it. The executable will then contain the static library (or the parts that you used).

If you want to load a static library at runtime using dlopen , you will have to first create a dynamic library libfoo.so containing it.

That workaround implies that I would have to create a shared library from a static library if I want to dynamically load it. This means that loading a static library dynamically is not possible and that only shared libraries can be used for dynamical loading? If so, than the quote I’ve stated from the source is not correct.

Opening a .a file using dlopen does not work (tested on Ubuntu 10.04). With the following example program:

dlopen error=/usr/lib/libz.a: invalid ELF header lib_handle=(nil) 

while when using /usr/lib/libz.so instead, I get:

dlopen error=(null) lib_handle=0x19d6030 

so the same code works for a shared object.

A .a is an archive containing one or more .o elf objects. Readelf and objdump won’t parse them. You must use ar to xtract the .o files from the archive. It is important to realize that if you are willing to spend the time writing and debugging a variant of load_elf() that can wrap one or more static libraries in a HAL you can load them dynamically and provide clients with a way to introspect their call entry points. This is nonstandard, and I can already feel the literati twitching like The Walking Jed. However, the ELF contains enough information to drop a library into a runtime environment and give properly coded client functions a way to discover the interface to the functions provided, and call them. This isn’t rocket science. It is simply tedious. An important concept here is that a developer who provides the .a archive and a include suite with the idea that they are restricting your use of the libraries, is just being annoying. It is not a serious impediment to using the library, or discovering how it does it’s job.

Linked

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.14.43533

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

Оцените статью
Adblock
detector