Linux so function name

Linux so function name

After the official website has been packaged, the official website installation instructions are not complete:

  • ./autogen.sh
  • ./configure
  • make
  • make check
  • sudo make install

Use macro definitions to distinguish between windows & linux

Multi-parameter macro definition

Mainly linux:
args. Correspondence ##args // QT will prompt that this representation is a GNU extension
windows:
. corresponds to ##__VA_ARGS__

There are incompatible functions

sprint_s snprintf

Found that some functions are windows platform. Sprint_s is a thread-safe format string function under Windows platform, not a standard C function, so it can’t be used under Linux, but you can use the snprintf function instead.

Control the export function of the linux dynamic library

Under Windows, __declspec(dllexport) is used to declare the interface (function or class) exported by the DLL dynamic library, and __declspec(dllimport) is declared as the interface loaded by the dynamic library. Not available under linux.
Under linux, the GCC help documentation has such a paragraph under the -fvisibility=default|internal|hidden|protected parameter:

  • a superior solution made possible by this option to marking things hidden when the default is public is to make the default hidden and mark things public. This is the norm with DLL’s on Windows and with -fvisibility=hidden and “attribute ((visibility(“default”)))” instead of “__declspec(dllexport)” you get almost identical semantics with identical syntax. This is a great boon to those working with cross-platform projects.
  1. All functions in the source file under linux have a defaultVisibility property, the default is public, which is the default export. If you want to hide, add in the GCC compiler directive-fvisibility=hidden The parameter will change the default public property to hidden.
  2. After the hidden function is exported, all the exports are hidden. In the source code, add __attribute__ ((visibility(«default»)))) before the function to be exported, so that it is still processed by the default public attribute.
Читайте также:  Linux ram and cpu

View file properties:

View the export function:

Use linux dynamic library

Linux provides 4 library functions, a header file dlfcn.h, and two shared libraries (Static library libdl.a and dynamic library libdl.so) Support for dynamic linking.

  • Dlopen: Open the dynamic shared object file and map it to memory, returning its first address
  • Dlsym: pointer to the entry point of the lock request
  • Dlerror: returns NULL or points to a string describing the most recent error
  • Dlclose: Close dynamic shared files

Dynamic loading

Use linux library, the method is consistent with windows.

Dynamically loading cmakelist

When loading dynamically, you need to use the dl library and add the link dl library to the cmakelist.

Static loading

After setting the link to the dynamic library in cmakelist, and including the header file, you can use it directly.

other problems

Qt’s cmakelists project for debug debugging

Add SET (CMAKE_BUILD_TYPE DEBUG) to cmakelist

When using the test demo debug, it is found that the dlopen failure error is a soft connection without log4cpp.so. When the so is generated, the linked log4cpp library has no suffix:

Report relocation R_X86_64_32 against `.rodata’ can not be used when making a shared object

After modifying the link log4cpp.a static library, the error relocation R_X86_64_32 against `.rodata’ can not be used when making a shared object.
The reason is that if you compile the compiled static library into the dynamic library, which is my application scenario, you need to add the compile directive -fPIC when compiling the static library.
So in the cmakelist of the log4cpp source, add instructions:

Читайте также:  Windows update kb2670838 что это

Then do not use configure, directly cmake make, generate a static library, solve.

Источник

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