Talk about the implementation of the platform C ++ dynamic connection library

xiaoxiao2021-03-02  36

I have explored the dynamic library of Solaris, HP, Windows. For the Unix platforms such as Soloaris, HP, mainly Make files, and Windows also requires a resort.

This column mainly implements a dynamic library and a running program for calling dynamic libraries. The dynamic library includes myTime.h, mytime.cpp, and DLLT1.CPP is required under Windows, this contains DLLMAIN portfral functions of the dynamic library,

Stdafx.cpp, stdafx.h is a file related to the precompilation. Runners include Dy.cpp including calls to dynamic libraries.

Let's first talk about the compilation instructions under UNIX.

One. Under the HP platform

ACC compiler that comes with HP under HP

Rm * .orm * .slacc z mytime.cpp // z Generates the * .o file required for dynamic library

ACC -B -O libmy.sl mytime.o // - b generates dynamic libraries, -o libmy.sl means that the name of the generated dynamic library is libmy.slacc dy.cpp libmy.sl // is generated with Dy.cpp libmy.sl The executable program, default is A.out, or you can also use -O to specify name

two. Under the Solaris platform

Use the CC with Workshop under SOLOARIS.

Cc -c mytime.cpp // - c generate dynamic library * .o file cc -g -o libmy.so mytime.o -b generated dynamic library, named libmy.socc -bdynamic -o myee libmy.so DY .cpp // Generate an executable MYEE

Ln -s /export/Home/HURH/TDLL/LIBMY.SO / LIB // Solaris Before running, use root users to configure dynamic library libmy.so

Let's talk about the problem that the dynamic library under Windows needs to be considered and compiled with VC.

Mainly talk about the statement of dynamic library header file myTime.h

#ifndef mytime_h # define mytime_h

#ifndef WIN32 #define DLLT_API __declspec (dllexport) #else #ifdef JESTERDLL #define DLLT_API __declspec (dllexport) #else #define DLLT_API __declspec (dllimport) #pragma comment (lib, "dllt1.lib") # endif # endif

Struct datetype {int year; int mon; int day;};

CHAR HOUR; CHAR Sec;;

INT DLLT_API GETDATE (DateType * D); int DLLT_API GETTIME (TIMETYPE * T);

Class DLLT_API GQLASS {public: int ineid; void setneid; gqlass (); ~ gqlass ();

#ENDIF

Macro Win32, JesterDLL needs to be specified in the dynamic library compilation. The class or function that needs to be exported for dynamic libraries requires modification __declspec (DLLEXPORT), and for the reference to __declspec (dllimport), which is the purpose of our joint win32, JesterDLL, the name of the dynamic library is DLLT1. DLL If you have #pragma comment (lib, "dllt1.lib"), you don't need to contact the DLLT1 ​​library in the Make file.

Attached source code:

//mytime.cpp#include"stdafx.h"#include "time.h" #include "mytime.h" int getdate (datetype * d) {long Ti; struct TM * TM; TIME (& Ti); TM = LOCALTIME (& Ti); D-> Year = TM-> TM_Year 1900; D-> MON = TM-> TM_MON 1; D-> day = TM-> TM_MDAY; returnography;}

INT getTime (TimetyPE * T) {long Ti; StructTM * TM; TIME (& Ti); TM = localtime (& Ti); T-> Hour = TM-> TM_HOUR; T-> min = TM-> TM_MIN; T- > sec = tm-> tm_sec; return 0;} gqlass :: gqlass () {ineid = 0;} gqlass :: ~ gqlass () {

}

Void gqlass :: setneid {ineid = neid;

//dy.cpp

#include "stdafx.h" #include "stdio.h" #include "../dllt1/mytime.h" int main () {datetype d; timetype t; void * dp; char * error

PUTS ("Dynamic Link Library Application Demonstration");

GetDate (& D); / * Call this shared function * / printf ("Current Date:% 04D-% 02D-% 02D / N", D.Year, D.MON, D.DAY;

GetTime (& T); / * Call this shared function * / printf ("Current time:% 02D:% 02D:% 02D / N", T. Hour, t.min, t.sec); GQLASS GQ; Printf (" Before setneid gq.ineid =% d / n ", gq.ineID); gq.setneid (123); Printf (" "after setneid gq.ineid =% d / n", gq.ineID);

Return 0;}

转载请注明原文地址:https://www.9cbs.com/read-31599.html

New Post(0)