Sorting out the basic syntax of C + +: link loading Library - shared library of Linux

Shared Library for Linux

The shared library under Linux is a common ELF shared object.

The version update of the shared library should ensure the compatibility of the binary interface ABI (Application Binary Interface)

name

libname.so.x.y.z

x: Major version number. Libraries with different major version numbers are incompatible and need to be recompiled

y: Minor version number, high version number backward compatible with low version number

z: Release the version number without changing the interface, which is fully compatible

route

Most open source systems, including Linux, follow the FHS (File Hierarchy Standard) standard, which specifies how to store system files, including various directory structures, organizations and functions.

/lib: the most critical and basic shared library of the system, such as dynamic linker, C language runtime, mathematics library, etc

/usr/lib: it is used to store the key libraries required by non system operation, mainly development libraries

/usr/local/lib: stores libraries that are not very relevant to the operating system itself, mainly libraries for third-party applications

The dynamic linker will  / lib,/usr/lib   And by  / etc/ld.so.conf   Find the shared library in the directory specified by the configuration file

environment variable

LD_LIBRARY_PATH: temporarily change the shared library lookup path of an application without affecting other applications

LD_PRELOAD: specifies some shared libraries or even target files that are preloaded

LD_DEBUG: opens the debugging function of the dynamic linker

Compilation of so shared library

Writing shared libraries using CLion

Create a shared library called MySharedLib

CMakeLists.txt

cmake_minimum_required(VERSION 3.10)
project(MySharedLib)

set(CMAKE_CXX_STANDARD 11)

add_library(MySharedLib SHARED library.cpp library.h)

library.h

#ifndef MYSHAREDLIB_LIBRARY_H
#define MYSHAREDLIB_LIBRARY_H

// Print Hello World!
void hello();

// Summation using variable template parameters
template <typename T>
T sum(T t)
{
    return t;
}
template <typename T, typename ...Types>
T sum(T first, Types ... rest)
{
    return first + sum<T>(rest...);
}

#endif

library.cpp

#include <iostream>
#include "library.h"

void hello() {
    std::cout << "Hello, World!" << std::endl;
}

Use of so shared library (called by executable project)

Using CLion to call a shared library

Create an executable project called TestSharedLib

CMakeLists.txt

cmake_minimum_required(VERSION 3.10)
project(TestSharedLib)

# C++11 compilation
set(CMAKE_CXX_STANDARD 11)

# Header file path
set(INC_DIR /home/xx/code/clion/MySharedLib)
# Library file path
set(LIB_DIR /home/xx/code/clion/MySharedLib/cmake-build-debug)

include_directories(${INC_DIR})
link_directories(${LIB_DIR})
link_libraries(MySharedLib)

add_executable(TestSharedLib main.cpp)

# Link MySharedLib Library
target_link_libraries(TestSharedLib MySharedLib)

main.cpp

#include <iostream>
#include "library.h"
using std::cout;
using std::endl;

int main() {

    hello();
    cout << "1 + 2 = " << sum(1,2) << endl;
    cout << "1 + 2 + 3 = " << sum(1,2,3) << endl;

    return 0;
}

results of enforcement

Hello, World!
1 + 2 = 3
1 + 2 + 3 = 6

That's all for today's sharing. We should learn C + + well~

Write at the end: for those who are ready to learn C/C + + programming, if you want to better improve your core programming ability (internal skill), you might as well start now!

C language c + + programming learning and communication circle, QQ group: 829164294[ Click to enter WeChat official account: C language programming learning base

Sorting and sharing (source code, project practice video, project notes, basic introductory tutorial)

Welcome to change careers and learn programming partners. Use more materials to learn and grow faster than you think!

Programming learning video sharing:

 

 

Tags: C++ Linux

Posted on Sat, 30 Oct 2021 06:36:12 -0400 by jcampbell1