ไฟล์ DLL ย่อมาจาก dynamic-linked library เป็นไฟล์ที่สร้างและสั่งการด้วยภาษา C++ โดยไฟล์ DLL นี้ทำให้แชร์ เก็บ และเซฟโค้ดได้ง่ายขึ้น บทความวิกิฮาวนี้จะแนะนำวิธีการสร้างไฟล์ DLL ด้วย Visual Studio , แอพใน Windows หรือ Visual Studio ใน Mac ให้คุณเอง อย่าลืมติ๊ก “Desktop Development with C++” ตอนติดตั้ง ถ้ามี Visual Studio อยู่แล้ว แต่ไม่ได้ติ๊กช่องนี้ ให้เปิดไฟล์ติดตั้งใหม่ แล้วติ๊กก่อน
ขั้นตอน
-
เปิด Visual Studio. ปกติจะอยู่ใน Start Menu หรือโฟลเดอร์ Applications โดยไฟล์ DLL นั้นเป็นข้อมูล library เป็นแค่ส่วนหนึ่งของ project ปกติต้องมีแอพอื่นร่วมด้วย ถึงจะเปิดได้
- คุณดาวน์โหลด Visual Studio เวอร์ชั่น Windows ได้ที่ https://docs.microsoft.com/en-us/visualstudio/install/install-visual-studio?view=vs-2019
- คุณดาวน์โหลด Visual Studio เวอร์ชั่น Mac ได้ที่ https://docs.microsoft.com/en-us/visualstudio/mac/installation?view=vsmac-2019
- บทความวิกิฮาวนี้จะใช้โค้ดของ Microsoft อธิบายการสร้างไฟล์ DLL
-
คลิก File . ปกติจะอยู่ด้านบนของพื้นที่ project (Windows) หรือยาวไปตามด้านบนของหน้าจอ (Macs)
-
คลิก New และ Project . หน้าต่าง dialog box “Create a New Project” จะเปิดขึ้นมา
-
ตั้งค่า Language , Platform และ Project Type . จะเป็นตัวกำหนดเทมเพลต project ที่จะโผล่ขึ้นมา
- คลิก Language เพื่อขยายเมนูลงมา แล้วคลิก C++
-
คลิก Platform เพื่อขยายเมนูลงมา แล้วคลิก Windows .
-
คลิก Project Type เพื่อขยายเมนูลงมา แล้วคลิก Library .
-
คลิก Dynamic-link Library (DLL) . เพื่อเน้นตัวเลือกด้วยสีฟ้า แล้วคลิก Next เพื่อไปต่อ
-
พิมพ์ชื่อใน Name Box ของ project. เช่น พิมพ์ “MathLibrary” ในช่อง เป็นชื่อตัวอย่าง
-
คลิก Create . เท่านี้ก็สร้าง project DLL เรียบร้อย
-
เพิ่มไฟล์ header ใน DLL. ทำได้โดยคลิก “ Add New Item ” ใน “ Project ” ในแถบเมนู
- เลือก Visual C++ ในเมนูทางซ้ายของ dialog box
- เลือก Header file (.h) ตรงกลาง dialog box
- พิมพ์ชื่อ “MathLibrary.h” ในช่อง name ล่างตัวเลือกเมนูต่างๆ
- คลิก Add แล้วจะได้ไฟล์ header ว่าง
-
พิมพ์โค้ดต่อไปนี้ในไฟล์ header ว่าง.
- นี่เป็นโค้ดตัวอย่างจากเว็บช่วยเหลือของ Microsoft
// MathLibrary.h - Contains declarations of math functions #pragma once #ifdef MATHLIBRARY_EXPORTS #define MATHLIBRARY_API __declspec(dllexport) #else #define MATHLIBRARY_API __declspec(dllimport) #endif // The Fibonacci recurrence relation describes a sequence F // where F(n) is { n = 0, a // { n = 1, b // { n > 1, F(n-2) + F(n-1) // for some initial integral values a and b. // If the sequence is initialized F(0) = 1, F(1) = 1, // then this relation produces the well-known Fibonacci // sequence: 1, 1, 2, 3, 5, 8, 13, 21, 34, ... // Initialize a Fibonacci relation sequence // such that F(0) = a, F(1) = b. // This function must be called before any other function. extern "C" MATHLIBRARY_API void fibonacci_init ( const unsigned long long a , const unsigned long long b ); // Produce the next value in the sequence. // Returns true on success and updates current value and index; // false on overflow, leaves current value and index unchanged. extern "C" MATHLIBRARY_API bool fibonacci_next (); // Get the current value in the sequence. extern "C" MATHLIBRARY_API unsigned long long fibonacci_current (); // Get the position of the current value in the sequence. extern "C" MATHLIBRARY_API unsigned fibonacci_index ();
-
เพิ่มไฟล์ CPP ใน DLL. ทำได้โดยคลิก Add New Item ใน “Project” ในแถบเมนู
- เลือก “ Visual C++ ” ในเมนูทางซ้ายของ dialog box
- เลือก “ C++ File (.cpp) ” ตรงกลาง dialog box
- พิมพ์ชื่อ “MathLibrary.cpp” ในช่อง name ล่างตัวเลือกเมนูต่างๆ
- คลิก Add เพื่อสร้างไฟล์ว่าง
-
พิมพ์โค้ดต่อไปนี้ในไฟล์ว่าง.
- นี่เป็นโค้ดตัวอย่างจากเว็บช่วยเหลือของ Microsoft
// MathLibrary.cpp : Defines the exported functions for the DLL. #include "stdafx.h" // use pch.h in Visual Studio 2019 #include <utility> #include <limits.h> #include "MathLibrary.h" // DLL internal state variables: static unsigned long long previous_ ; // Previous value, if any static unsigned long long current_ ; // Current sequence value static unsigned index_ ; // Current seq. position // Initialize a Fibonacci relation sequence // such that F(0) = a, F(1) = b. // This function must be called before any other function. void fibonacci_init ( const unsigned long long a , const unsigned long long b ) { index_ = 0 ; current_ = a ; previous_ = b ; // see special case when initialized } // Produce the next value in the sequence. // Returns true on success, false on overflow. bool fibonacci_next () { // check to see if we'd overflow result or position if (( ULLONG_MAX - previous_ < current_ ) || ( UINT_MAX == index_ )) { return false ; } // Special case when index == 0, just return b value if ( index_ > 0 ) { // otherwise, calculate next sequence value previous_ += current_ ; } std :: swap ( current_ , previous_ ); ++ index_ ; return true ; } // Get the current value in the sequence. unsigned long long fibonacci_current () { return current_ ; } // Get the current index position in the sequence. unsigned fibonacci_index () { return index_ ; }
-
คลิก Build ในแถบเมนู. ปกติจะอยู่เหนือพื้นที่ project (Windows) หรือยาวไปตามด้านบนของหน้าจอ (Macs)
-
คลิก Build Solution . คลิกแล้วจะเห็นข้อความประมาณด้านล่างนี้
- ถ้าสร้างไฟล์ DLL สำเร็จ จะขึ้นตรงนี้ แต่ถ้า error ก็จะขึ้นบอกให้แก้ไข [1] X แหล่งข้อมูลอ้างอิง
1 >------ Build started : Project : MathLibrary , Configuration : Debug Win32
1 > MathLibrary . cpp 1 > dllmain . cpp 1 > Generating Code ... 1 > Creating library C : \ Users \ username \ Source \ Repos \ MathLibrary \ Debug \ MathLibrary . lib and object C : \ Users \ username \ Source \ Repos \ MathLibrary \ Debug \ MathLibrary . exp 1 > MathLibrary . vcxproj -> C : \ Users \ username \ Source \ Repos \ MathLibrary \ Debug \ MathLibrary . dll 1 > MathLibrary . vcxproj -> C : \ Users \ username \ Source \ Repos \ MathLibrary \ Debug \ MathLibrary . pdb ( Partial PDB ) ========== Build : 1 succeeded , 0 failed , 0 up - to - date , 0 skipped ==========โฆษณา
ข้อมูลอ้างอิง
โฆษณา