簡體   English   中英

當有一個.dll文件和一個header文件時如何制作一個.lib文件

[英]How to make a .lib file when have a .dll file and a header file

我正在嘗試在 visual studio 中創建一個應用程序,它將能夠訪問一個已經存在的 .dll 文件。 我需要應用程序來調用例程。 我還有一個 header 文件已經存在。

我一直在研究 inte.net,發現我需要創建一個 .lib 文件。 查看此處的類似問題,我找到了一個鏈接: http://support.microsoft.com/kb/131313但是我無法按照說明進行操作。

鏈接中的信息說要制作一個 DEF 文件(我在別處讀到這需要編譯為同名的 DLL,但不確定那個名字是什么,與 .dll 文件同名?)。 但我不明白第一個方向,'Use DUMPBIN /EXPORTS'。 然后我需要“存根”功能,然后是一些與 .OBJ 文件有關的東西(我不知道這些文件是什么)。

是否有任何類似於上面的鏈接且易於遵循的分步說明?

您將需要Microsoft Visual C++ 2010 Express (或任何其他 MSVC 命令行工具來源)和 DLL。

腳步:

  1. dumpbin /EXPORTS yourfile.dll > yourfile.exports
  2. 將所需函數的名稱從yourfile.exports粘貼到新的yourfile.def文件中。 在此文件的頂部添加一行帶有單詞EXPORTS的行。
  3. VC\\bin目錄( lib.exe和其他編譯工具所在的目錄)運行以下命令。

 vcvars32.bat

 lib /def:yourfile.def /out:yourfile.lib

或 x64 版本

 lib /def:yourfile.def /machine:x64 /out:yourfile64.lib

您應該生成兩個文件: yourfile.libyourfile.exp

您可以使用 Digital Mars 的IMPLIB工具。 它可以僅使用 dll 創建一個 lib 文件,而無需任何 .def 文件。

下載鏈接是http://ftp.digitalmars.com/bup.zip

命令行是:

implib.exe /s mydll.lib mydll.dll

有一種更簡單的方法來創建.def文件。 在 web 中搜索gendef utility (如果您有Mingw-64 ,您可能已經擁有它)。 一旦你擁有它,只需 go 到命令行並輸入,

gendef myfile.dll

它將創建一個myfile.def 之后,只需使用lib.exe創建myfile.lib文件,如 John 所解釋的那樣。

這是使用Cygwin的解決方案:

  • 安裝gendefmingw64-x86_64-binutils Cygwin 包
  • 在 Cygwin 控制台中運行:
     gendef.exe awesome.dll x86_64-w64-mingw32-dlltool.exe -d awesome.def -l awesome.lib
  • 你在同一個目錄中得到awesome.lib

VS 2019 linker 使用此文件鏈接得很好。

第一類

#define YOURPROJECT_API _declspec(dllexport)

無效你的()

cpp

#include "pch.h"
#include "YOURPROJECT.H"
void yourf() {}

然后包括他們

linker->input->def file then inherit from parent or project default

編譯

Ещеодинвариант,спомощьюpowershell,dumpbin和lib。
的照片: http ://xbb.uz/dev/Gjenjeracija-.lib-iz-DLL-s-pomoshhju-Visual-Studio

$pattern = "\s+([A-Z0-9]+)\s+[A-Z0-9]+\s+[A-Z0-9]{8} (.*)";
$platform = "x86";

if($args.length)
{
    if($args[1])
    {
        $platform = $args[1];
    }

    $dll = [System.IO.Path]::GetFilename($args[0]);
    $def = [System.IO.Path]::ChangeExtension($dll, "def");
    $lib = [System.IO.Path]::ChangeExtension($dll, "lib");

    Write-Host ("Generating " + $def + " file…");
    $content="EXPORTS`n";
    &"dumpbin" "/exports" $args[0] | select-string $pattern | %{$null = $_.Line -match $pattern; $str='{0,9}{1,-30} @{2}' -f " ", $matches[2], $matches[1]; $content+=($str+"`n"); }
    $content | Out-File $def -Encoding Ascii;

    Write-Host ("Generating " + $lib + " file…");
    &"lib" ("/def:" + $def) ("/out:" + $lib) ("/machine:" + $platform) | out-null;

    Write-Host ("");
}
else
{
    Write-Host "Start powershell from VisualStudio commandline then use this script.";
    Write-Host "Script takes two parameters: dll filename and platform.";
    Write-Host "example: .\GenerateLibFromDll.ps1 hello.dll `"x64`"";
    Write-Host "or to process all dlls in the dir: gci *.dll | foreach {&`".\GenerateLibFromDll.ps1`" $_.Name `"x64`"}";;
}

我可能有答案。 我在創建需要 .dll 文件的 .exe 控制台應用程序時這樣做了。 我也遇到了這個問題。 當我嘗試使用 IMPLIB 應用程序時,它找不到任何導出文件。 您需要添加一個#ifdef FILENAME_EXPORTS (用您的 .dll 文件名替換 FILENAME)並創建一個_API 以下是#ifdef export api 命令的代碼:

#ifdef FILENAME_EXPORTS
#define FILENAME_API __declspec(dllexport)
#else
#define FILENAME_API __declspec(dllimport)
#endif

現在您已經定義了導出 API,您需要將它應用到 .dll 項目的頭文件中的所有函數。 例如:

void FILENAME_API function();

正常聲明您的導出函數,但在聲明器類型和函數名稱之間包含 API。

在.dll 項目的.cpp 文件中定義函數,不需要聲明中的API。

這是文件名.h 和filename.cpp的示例,其中包含所有代碼。

// Code for filename.h
#pragma once

// Define your Export API
#ifdef FILENAME_EXPORTS
#define FILENAME_API __declspec(dllexport)
#else
#define FILENAME_API __declspec(dllimport)
#endif

// Declare your functions with your API

void FILENAME_API function1();
void FILENAME_API function2();
void FILENAME_API function3();

-------------------------------------------------------------------------------------------
// Code for filename.cpp
#include <iostream>
#include "pch.h"
#include "filename.h"
using namespace std;

void function1()
{
    cout << "Hello Function1!";
}

void function2()
{
    cout << "Hello Function2!";
}

void function3()
{
    cout << "Hello Function3!";
}

現在,當您編譯項目時,您應該會在保存編譯文件的文件夾中看到 .dll、.lib 和 .exp 文件。 現在您可以將 .exe 文件與 .lib 文件鏈接起來。 別客氣!

您可以通過導出應用程序代碼中引用的 __declspec(dllexport) 在 .dll 文件中定義的函數/類,而不是創建 .def,從 .dll 文件創建 .lib 文件。

例如(偽代碼)

用於創建 X.dll 文件的項目(例如,X 是一個 dll 名稱):

啊:

// Function declaration
__declspec(dllexport) void  foo(void);

A.cpp:

// Function definition 
#include <A.h>
void foo(void) {
; // definition
}

如果您在 Visual Studio 中構建上述 dll 項目,那么編譯器將生成X.dllX.lib [已通過 __declspec(dllexport) 導出函數foo ]。

應用程序.cpp:

// Load time dynamic linking:
// Application should include X.lib (not X.dll) in the project setting
 #include <A.h>
 int main() {
 foo();
 return 0;
}

如需進一步研究,請參閱以下鏈接以獲得更好的理解:

http://www.codeproject.com/Articles/28969/HowTo-Export-C-classes-from-a-DLL#CppMatureApproach

http://msdn.microsoft.com/en-us/library/ms686923(v=vs.85).aspx

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM