Qlibrary class reference (QT loads external Library)

Source: Internet
Author: User

Qlibrary class reference
[Qtcore module]
This class loads the shared library at runtime.
# Include <qlibrary>
Inherit qobject
Note: All functions can be reentrant.
Public type:
Enum loadhint {resolveallsymbolshint, exportexternalsymbolshint, loadarchivememberhint}
Flags loadhints

Attribute:
• Filename: qstring
• Loadhints: loadhints
Common functions:

Qlibrary (qobject * parent = 0)

Qlibrary (const qstring & filename, qobject * parent = 0)
Qlibrary (const qstring & filename, int vernum, qobject * parent = 0)
Qlibrary (const qstring & filename, const qstring & version, qobject * parent = 0)
~ Qlibrary ()

Qstring errorstring () const

Qstring filename () const

Bool isloaded () const

Bool load ()

Loadhints () const

Void * resolve (const char * symbol)

Void setfilename (const qstring & filename)
Void setfilenameandversion (const qstring & filename, int versionnumber)
Void setfilenameandversion (const qstring & filename, const qstring & Version)
Void setloadhints (loadhints hints)

Bool unload ()

Static public members:
Bool islibrary (const qstring & filename)

Void * resolve (const qstring & filename, const char * symbol)
Void * resolve (const qstring & filename, int vernum, const char * symbol)
Void * resolve (const qstring & filename, const qstring & version, const char * symbol)
Detailed description:
An instance of a qlibrary object operates on a separate shared library, which we call lib or DLL. A qlibrary provides a platform-independent function for accessing the shared library. You can upload a file name in the constructor or explicitly set it to use setfilename (). When a library is loaded, qlibrary searches for all the specific system library paths, unless the file name has an absolute path. If the file cannot be found, qlibrary tries to modify the name with the suffix of different platforms, such as "so" on UNIX, "dylib" on Mac, or "DLL" on Windows and Symbian. In this way, they can only be found by the name of the shared library, so the same code will work on different platforms.
The most important function is load (), which dynamically loads library files. Isloaded () is used to check whether the load is successful. Resolve () is used to break down objects in a library. If the library has not been loaded, the resolve () function will load the library in the dark. Multiple qlibrary instances can be used to enter the same database. Once loaded, the database is stored in the memory until the application ends. You can try to uninstall a library and use unload (). However, if other instances are using the same library, this function will fail, unload () is called only for all instances ().
A typical application of qlibrary is to break down an output symbol to call the C function represented by this symbol. This is called "explicit connection", which is different from "implicit linking ).
Note: In Symbian, only stddll is created when the database is created. They can use the two decomposition symbols. Otherwise, the serial number must be used. In Symbian, the library path is ignored, and the default library path is always used.
The following code snippet loads a library and breaks down the symbol "mysymbol". Call the function if everything succeeds. If an error occurs, the library file does not exist or the symbol is not defined, the function pointer will be 0 and will not be called.
Qlibrary mylib ("mylib ");
Typedef void (* myprototype )();
Myprototype myfunction = (myprototype) mylib. Resolve ("mysymbol ");
If (myfunction)
Myfunction ();
This symbol does not need to be output as a C function and works for resolve. This means that the function will be wrapped in an external "C" block if the library is compiled using a C ++ compiler. On Windows, this requires the use of a dllexport macro. Check resolve () to see how it works. For convenience, there is a static resolve () function, you can use it, if you only want to call a function without explicitly loading the library.
Typedef void (* myprototype )();
Myprototype myfunction =
(Myprototype) qlibrary: Resolve ("mylib", "mysymbol ");
If (myfunction)
Myfunction ();
Member function file:
Enum qlibrary: loadhint
Flags qlibrary: loadhints
This joint description may imply that when the database is loaded, it can be used to change the way the database is processed. These values indicate how the symbols are decomposed when the database is loaded, and are indicated by using setloadhints ().
Content value description
Qlibrary: resolveallsymbolshint 0x01 all symbols are decomposed when the database is loaded, not when resolve is called.
Qlibrary: exportexternalsymbolshint 0x02 outputs undecomposed or global symbols so that they can be decomposed when other dynamic loading occurs in the future.
Qlibrary: loadarchivememberhint 0x04 allows the library file name to sign a special object file in a file. If this suggestion is given, the library file name contains a path, which is the path of an archive file, followed by an archive member ,.
The loadhints type is a type definition of qflags. It stores an or combined loadhint value.
Property file:
Filename: qstring
This attribute has the name of the file in the database.
We recommend that you omit the file suffix because qlibrary will automatically find the file using the best suffix.
When a library is loaded, qlibrary searches for all the path of the specific system library.
For example, after the "Gl" library of the UNIX platform is loaded successfully, filename () will return "libgl. so ". if the file name is "/usr/lib/libgl", filename () will return "/usr/lib/libgl. so"
Note: In Symbian, the file name path is ignored.
Channel functions:
Qstring filename () const
Void setfilename (const qstring & filename)
Loadhints: loadhints
This attribute tells the load () function how to load data.
You can give some hints about how to break down symbols. Generally, symbols cannot be decomposed during loading, but are decomposed only when resolve () is called. You can set the resolveallsymbolshint attribute. If the platform supports this attribute, it will be decomposed during loading.
Setting exportexternalsymbolshint will break down the global symbols during loading.
If loadarchivememberhint is set, the file name consists of two parts. A path, which represents an archive file, followed by the archive member. For example, the file name libgl. A (shr_64.o) indicates that the library shr_64.o is a member of libgl.. This only supports the AIX platform
The loading suggestion is platform independent. If you use it, you may need to preset some conditions based on the platform.
By default, these labels are not set. Therefore, the library will be loaded and use the lazy signal decomposition. The Global symbols will not be output to other dynamic loading libraries.
Channel functions:
Loadhints () const
Void setloadhints (loadhints hints)
Member function file;
Qlibrary: qlibrary (qobject * parent = 0)
Qlibrary: qlibrary (const qstring & filename, qobject * parent = 0)
Build a library object, use a given parent, and file name
We recommend that you ignore the suffix of the file name.
Qlibrary: qlibrary (const qstring & filename, int vernum, qobject * parent = 0)
Currently (main version) vernum is ignored in windows and Symbian.
Qlibrary: qlibrary (const qstring & filename, const qstring & version, qobject * parent = 0)
Currently (full version) versions are ignored in windows and Symbian.
Qlibrary ::~ Qlibrary ()
Qstring qlibrary: errorstring () const
Bool qlibrary: islibrary (const qstring & filename) [Static]
Platform valid suffixes
Windows. dll
Unix/Linux. So
Aix.
HP-UX. SL,. So (HP-uxi)
Mac OS X. dylib,. Bundle,. So
Bool qlibrary: isloaded () const
Bool qlibrary: load ()
Because resolve () always calls this function first and breaks down the symbols. Therefore, you do not need to call it explicitly. You can use this function when you want to load the database in advanced mode.
Void * qlibrary: Resolve (const char * symbol)
Returns the address of the output symbol.
Typedef int (* avgfunction) (INT, INT );

Avgfunction AVG = (avgfunction) Library-> resolve ("AVG ");
If (avg)
Return AVG (5, 8 );
Else
Return-1;
In the window, you must explicitly compile the output function from the DLL using _ declspec (dllexport ).
Extern "C" my_export int AVG (int A, int B)
{
Return (A + B)/2;
}

# Ifdef q_ws_win
# Define my_export _ declspec (dllexport)
# Else
# Define my_export
# Endif
Void * qlibrary: Resolve (const qstring & filename, const char * symbol) [Static]
Void * qlibrary: Resolve (const qstring & filename, int vernum, const char * symbol) [Static]
Void * qlibrary: Resolve (const qstring & filename, const qstring & version, const char * symbol) [Static]
Void qlibrary: setfilenameandversion (const qstring & filename, int versionnumber)
Void qlibrary: setfilenameandversion (const qstring & filename, const qstring & Version)
Bool qlibrary: Unload ()

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.