❶ c語言寫的程序怎麼樣生成.dll文件
這個過程和C語言怎麼寫沒什麼大關聯,主要是編譯器的編譯設置,IDE對內置的編譯核心進行了相應的設置,才將代碼轉換成DLL而不是EXE,這個問題我糾結了好久,最終還是妥協了IDE,在IDE里選擇建立DLL工程,IDE在編譯這個工程的時候就會自動進行相應的設置(准確的說是應用了DLL相關的命令)這樣,編譯出來的就是DLL了.
❷ dll文件如何用C語言生成
用 vc 6.0 下的cl.exe 和 link.exe工具,請讀下文:
聲明:下面這篇文章不是我寫的,源自:一個叫,有容乃大 的博客
如何手工編寫動態鏈接庫(windows dll)
1.本例介紹在命令行(Console)環境下製作dll的方法
2.讀者動手前,請確保在windows中安裝有編譯、鏈接工具和必要的函數庫文件。
3.本例使用C語言實現.
4.本例中使用路徑均為我機器上的絕對路徑,讀者需根據實際情況調整。
工具要求:
Microsoft的編譯器cl.exe
MIcrosoft鏈接器link.exe
dll製作步驟:
1.編寫dll函數實現源代碼hello.c
#include
int say_hello(char* name)
{
printf( "hello %s\n ", name);
return 1;
}
2.編寫dll函數輸出定義文件hello.def.
LIBRARY hello
EXPORTS
say_hello @1
3.編譯dll源碼,生成dll,lib文件.
3.1 新建命令行窗口
3.2 設置PATH �0�7 INCLUDE �0�7 LIB 3個環境變數.
SET PATH=K:\vcnet\vc7\bin;%PATH%
SET INCLUDE=K:\vcnet\vc7\include;%INCLUDE%
SET LIB=K:\vsnet\Vc7\lib;%LIB%
3.3 編譯hello.c
cd K:\Source\dllsample (hello.c和hello.def所在目錄)
cl /c hello.c
3.4 鏈接hello.obj,生成hello.dll,hello.lib兩個文件.
link /def:hello.def /dll hello.obj
4.測試dll函數.
4.1 編寫測試代碼 test.c
extern int say_hello(char* name);
int main(int argc,char** argv)
{
say_hello( "robbie ");
return 0;
}
4.2 編譯測試代碼test.c
cl /c test.c
4.3 鏈接test.obj和 hello.lib,生成可執行文件test.exe
link test.obj hello.lib
4.4 運行test.exe,屏幕輸出:
hello robbie
至此,一個dll構造完畢.
下面是我自己的一點補充:
如果要在c++下,或者win32 mfc下使用標准c寫的dll,必須把上面的聲明
extern int say_hello(char* name);改成:extern "C " int say_hello(char* name);
❸ c怎麼生成dll文件
在VC++中選擇新建一個Win32 Dynamic-Link Library。需要建立一個c/c++ head file和一個c/c++ source file並加入工程。頭文件中內容為輸出函數的聲明,源文件中內容為DllMain函數和輸出函數的定義。下面是一個最簡單的例子。
//dlldemo.h
#ifdef __cplusplus
#define EXPORT extern "C" __declspec(dllexport)
#else
#define EXPORT __declspec(dllexport)
#endif
EXPORT void CALLBACK DllFoo(void) ;
//dlldemo.c
#include <windows.h>
#include "dlldemo.h"
int WINAPI DllMain (HINSTANCE hInstance, DWORD fdwReason, PVOID pvReserved)
{
return TRUE ;
}
EXPORT void CALLBACK DllFoo(void)
{
MessageBox(NULL,TEXT("This function is exported from a DLL"),TEXT("DllFoo"),MB_OK) ;
return ;
}
頭文件預處理中的__declspec是微軟增加的「C擴展類存儲屬性」(C Extended Storage-Class Attributes),它指明一個給出的實例被存儲為一種微軟特定的類存儲屬性,可以為thread,naked,dllimport或dllexport. [MSDN原文:The extended attribute syntax for specifying storage-class information uses the __declspec keyword, which specifies that an instance of a given type is to be stored with a Microsoft-specific storage-class attribute (thread, naked, dllimport, or dllexport).] 輸出函數必須指明為CALLBACK。 DllMain是dll的入口點函數。也可以不寫它。DllMain必須返回TRUE,否則系統將終止程序並彈出一個「啟動程序時出錯」對話框。 編譯鏈接後,得到動態鏈接庫文件dlldemo.dll和輸入庫文件dlldemo.lib。
❹ 用C語言可以寫dll文件嗎應該怎麼寫呢
dll文件?
把C寫在TXT(記事本里)然後把後綴TXT改成DLL,嘿嘿
不知道對不對,樓下的你來回答!
❺ C語言程序怎麼編譯成dll文件供其他語言調用
C程序編譯成dll文件只不過是在要公開的介面函數聲明前面加上幾個特定的修飾符而已。
下面是個例子,用dev-cpp建了個dll的默認文檔
/*dll.h文件*/
#ifndef _DLL_H_
#define _DLL_H_
#if BUILDING_DLL
# define DLLIMPORT __declspec (dllexport)
#else /* Not BUILDING_DLL */
# define DLLIMPORT __declspec (dllimport)
#endif /* Not BUILDING_DLL */
DLLIMPORT void HelloWorld (void);
#endif /* _DLL_H_ */
/*dllmain.c文件*/
/* Replace "dll.h" with the name of your header */
#include "dll.h"
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
DLLIMPORT void HelloWorld ()
{
MessageBox (0, "Hello World from DLL!/n", "Hi", MB_ICONINFORMATION);
}
BOOL APIENTRY DllMain (HINSTANCE hInst /* Library instance handle. */ ,
DWORD reason /* Reason this function is being called. */ ,
LPVOID reserved /* Not used. */ )
{
switch (reason)
{
case DLL_PROCESS_ATTACH:
break;
case DLL_PROCESS_DETACH:
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
}
/* Returns TRUE on success, FALSE on failure */
return TRUE;
}
具體請參考jilei08124的CSDN博客
❻ c語言寫dll動態鏈接 簡單例子
先用vc6建立一dll工程"dlltest"
// dlltest.cpp : Defines the initialization routines for the DLL.
//
#include "stdafx.h"
#include "dlltest.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//
// Note!
//
// If this DLL is dynamically linked against the MFC
// DLLs, any functions exported from this DLL which
// call into MFC must have the AFX_MANAGE_STATE macro
// added at the very beginning of the function.
//
// For example:
//
// extern "C" BOOL PASCAL EXPORT ExportedFunction()
// {
// AFX_MANAGE_STATE(AfxGetStaticMoleState());
// // normal function body here
// }
//
// It is very important that this macro appear in each
// function, prior to any calls into MFC. This means that
// it must appear as the first statement within the
// function, even before any object variable declarations
// as their constructors may generate calls into the MFC
// DLL.
//
// Please see MFC Technical Notes 33 and 58 for additional
// details.
//
/////////////////////////////////////////////////////////////////////////////
// CDlltestApp
BEGIN_MESSAGE_MAP(CDlltestApp, CWinApp)
//{{AFX_MSG_MAP(CDlltestApp)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDlltestApp construction
CDlltestApp::CDlltestApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
/////////////////////////////////////////////////////////////////////////////
// The one and only CDlltestApp object
CDlltestApp theApp;
/////////////////////////////////////////////////////////////////////////////
// CDlltestApp initialization
BOOL CDlltestApp::InitInstance()
{
if (!AfxSocketInit())
{
AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
return FALSE;
}
return TRUE;
}
//myfunc.cpp
#include "stdafx.h"
extern "C" __declspec(dllexport)
int WINAPI func_test1(int a,int b)
{
int c;
c=a+b;
return c;
}
; dlltest.def : Declares the mole parameters for the DLL.
LIBRARY "dlltest"
DESCRIPTION 'dlltest Windows Dynamic Link Library'
EXPORTS
; Explicit exports can go here
func_test1 @1
❼ c語言怎麼調用dll文件
1、新建DLLTest文件夾,在該文件夾中新建source文件夾。
注意事項:
C語言能以簡易的方式編譯、處理低級存儲器。C語言是僅產生少量的機器語言以及不需要任何運行環境支持便能運行的高效率程序設計語言。
❽ 如何用VC編寫dll文件
VC編寫dll文件
1、VC++->New->Project->Win32 Dynamic_Link Library;
2、選擇創建一個空工程;
首先DLL需要一個頭文件,所以新建一個CC++ Header File
這個頭文件中必須包含想要輸出的變數和函數,頭文件必須定義用於輸出的任何符號和數據結構。
例如 :
證明調用動態鏈接庫里的 Add函數成功。
❾ c語言寫的程序怎麼樣生成.dll文件
創建一個vc項目,同時創建關聯一個動態鏈接庫項目最後生成的就是一個dll與exe文件,運行時缺一不可
❿ 用C語言編寫DLL
使用微軟的vc或者vs,用c語言編程就是了。創建工程的時候會問你是創建什麼樣的工程,比如控制台程序,mfc程序,動態庫(dll)等。
c和c++只是編程語言,動態庫依賴於操作系統,在哪個平台下就依賴於哪個平台的動態庫生成工具。linux下是so文件,用gcc編譯器就可以生成。