Does Solaris have functions related to Windows DLL?

Source: Internet
Author: User

Example
Oh, of course. Many mechanisms of window are UNIX-based.
For example, system (32) *. dll is a UNIX-like/usr/lib/*. So
UNIX/usr/lib/*. So is the Unix dynamic library)
It is used for dynamic link of the program. Otherwise,/usr/lib/*. A is a static library and the program is compiled.
Link the function to the target file.

In fact, the XXX of CC-o yyy yyy. O-LXXX tells CC to find/usr/lib/libxxx. So ..
YYY. O declares that the function in libxxx. So is called. When the target file YYY is successfully linked
Call the libxxx. So function. For CC-o yyy yyy. O libxxx. A, it's different.
The default connection method of the CC-l parameter is dynamic link, that is, the link symbol is used, and the function entity is not linked.
The connection method can be man lD.

Now that there is a dynamic library, there must be related functions. window has loadlibrary,
Even Solaris has dlopen, that is, dynamic library open. Window can make mud.
Soil version DLL, even Solaris has long allowed the mud to be produced by itself. So Haha, the following details how to call
The functions in the dynamic library libxxx do not need to specify-LXXX in CC.

Dlopen format:

# Include
Void * dlopen (const char * pathname, int mode );
Return a void * handle; otherwise, return null.

Pathname is the dynamic library to be opened by the mud. If the library declaration links to other libraries
Libraries are dependent, and all associated libraries are opened.
Group ).

Mode is the open mode:

Rtld_lazy: After the dynamic library is opened, only the data address reference in the library is relocated instead of the function reference,
Function reference is located only when the function is activated. It is lazy, but saves the cost ;)
Rtld_now: Compared with the above, when the dynamic library is opened, all function references are relocated.

Rtld_global: The Global symbols in the dynamic library can be relocated by all other libraries.
Rtld_local: The Global symbols used to open the dynamic library can only be referenced by the same group of libraries.
Rtld_group: only the symbols of the related group can be relocated ??
Rtld_parent: the symbol in the object called by dlopen is visible to the dlopen object.
Rtld_world :... I cannot understand what I translate ;(

In short, an rtld_lazy is enough ;)

Then we get the relocated data or function reference:
# Include
Void * dlsym (void * handle, const char * name)
Obviously, handle is the return value of dlopen, and name is the variable or function in the dynamic library to be referenced.
Number name. Return the signed address after the relocation. If the relocation fails, return null.

Finally, close the dynamic library: int dlclose (void * handle ),
I can understand it at a glance, so I am too lazy to explain it ;)

The following is an example of adding a perceptual knowledge. This example calls the function in the dynamic library client. So.
Int client_request (char *), which returns 0 or-1 and is set according to different errors.
String err_info (also defined in client. So ):

# Include
# Include
# Include

# Define true 0
# Define false-1

Main ()
{
Char Buf [64];
Void * handle;/* dynamic library handle */
Char * err_info;/* a variable in the dynamic library to be referenced */
INT (* client_request) (char *);/* a function to be referenced */

/* Open the dynamic library client. So */
If (handle = dlopen ("client

/* Obtain the reference of the function name client_request */
If (client_request =
(INT (*) (char *) dlsym (handle, "client_request") = NULL ){
Perror ("dlsym client_request ");
Exit (-1 );
}

/* Get the reference of the variable err_info */
If (err_info =
(Char *) dlsym (handle, "err_info") = NULL ){
Perror ("dlsym err_info ");
Exit (-1 );
}
For (;;){
Gets (BUF);/* read the command string from standard input */
If (strcmp (BUF, "exit") = true ){
Dlclose (handle);/* close the dynamic library */
Return 0;
}
Printf ("Request: % Sn", Buf );
Client_request (BUF);/* call the function in the dynamic library */
Printf ("ask: % Sn", err_info);/* reference the variable in the dynamic library */
}

}

Finally, the compilation problem occurs. How can we compile it into a. So file? It's easy to use lD or Cc-g, for example:
CC-G yyy. So yyy. O others. O-LDL
What if yyy. O references functions of other dynamic libraries? Then CC-G...-l is used.
For example, if yyy. O references the socket function
CC-G yyy. So yyy. O others. O-LDL-lsocket.

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.