码迷,mamicode.com
首页 > 其他好文 > 详细

CodeBlocks静态链接与动态链接设置

时间:2015-05-31 09:35:46      阅读:277      评论:0      收藏:0      [点我收藏+]

标签:交叉编译   mingw   

静态库和动态库的区别

1.静态库

之所以称之为"静态库",是因为在链接阶段,会将汇编生成的目标文件.o与引用到的库一起链接打包到可执行文件中。因此对应的链接方式称为静态链接。
从本质上来说,一个静态库可以简单看成是一组目标文件(.o/.obj文件)的集合,静态库与汇编生成的目标文件(.o/.obj)一起链接为可执行文件。
静态库(后缀为.a/.lib)和.o文件格式相似。即很多目标文件经过压缩打包后形成的一个文件

静态库特点总结:

1. 静态库对函数库的链接是放在编译时期完成的
2. 程序在运行时与函数库再无瓜葛,移植方便,因为代码已经嵌入到程序里面了,可以直接跟着程序走,不存在对外部文件的依赖
3. 浪费空间和资源,因为所有相关的目标文件与牵涉到的函数库被链接合成一个可执行文件,会增加原本程序的空间

GCC编译、使用静态库

静态库的后缀是.a(并没有强制规定),它的产生分两步

1. 由源文件编译生成一堆.o,每个.o里都包含这个编译单元的符号表
2. ar命令将很多.o转换成.a,成为静态库,从这点也可以看出来,库是很多.o文件的集合


编译好静态库文件之后,我们就可以在其他程序中使用静态库文件中的函数了

1. 只需要在使用到这些公用函数的源程序中包含这些公用函数的原型声明(include对应的头文件)
2. 然后在用gcc命令生成目标文件时指明静态库名
3. gcc将会从静态库中将公用函数连接到目标文件中
4. 注意,gcc会在静态库名前加上前缀lib,然后追加扩展名.a得到的静态库文件名来查找静态库文件,因此,我们在写需要连接的库时,只写名字就可以,如libhello.a的库,只写: -lhello

2.动态库

动态库文件名命名规范和静态库文件名命名规范类似,也是在动态库名增加前缀lib,但其文件扩展名为.so(.dll)。例如:我们将创建的动态库名为myhello,则动态库文件名就是libmyhello.so。

使用库是重用代码的一种绝佳方式。 您不必在自己创建的每个程序中重新实现同一例程,而只需对这些例程写入一次,然后从需要该功能的应用程序引用它们即可。 通过将代码放入 DLL,您节省在引用它的每个应用程序的空间,而且,您可以更新 DLL,而无需重新编译所有应用程序。

动态链接库 (DLL) 是作为共享函数库的可执行文件。动态链接提供了一种方法,使进程可以调用不属于其可执行代码的函数。DLL 还有助于共享数据和资源。多个应用程序可同时访问内存中单个 DLL 副本的内容。
动态链接与静态链接的不同之处在于它允许可执行模块(.dll 文件或 .exe 文件)仅包含在运行时定位 DLL 函数的可执行代码所需的信息。在静态链接中,链接器从静态链接库获取所有被引用的函数,并将库同代码一起放到可执行文件中。
使用动态链接代替静态链接有若干优点。DLL 节省内存,减少交换操作,节省磁盘空间,更易于升级,提供售后支持,提供扩展 MFC 库类的机制,支持多语言程序,并使国际版本的创建轻松完成。

DLL 的类型
当您在应用程序中加载 DLL 时,可以使用两种链接方法来调用导出的 DLL 函数。这两种链接方法是加载时动态链接和运行时动态链接。

动态库一般会有对应的导入库,方便程序静态载入动态链接库,否则你可能就需要自己LoadLibary调入DLL文件,然后再手工GetProcAddress获得对应函数了。有了导入库,你只需要链接导入库后按照头文件函数接口的声明调用函数就可以了。
加载时动态链接(load-time dynamic linking)

在加载时动态链接中,应用程序像调用本地函数一样对导出的 DLL 函数进行显式调用。要使用加载时动态链接,请在编译和链接应用程序时提供头文件 (.h) 和导入库文件 (.lib)。当您这样做时,链接器将向系统提供加载 DLL 所需的信息,并在加载时解析导出的 DLL 函数的位置。
运行时动态链接(run-time dynamic linking)
在运行时动态链接中,应用程序调用 LoadLibrary 函数或 LoadLibraryEx 函数以在运行时加载 DLL。成功加载 DLL 后,可以使用 GetProcAddress 函数获得要调用的导出的 DLL 函数的地址。在使用运行时动态链接时,无需使用导入库文件。


所谓静态、动态是指"链接"的过程存在区别:

技术分享


3. DLL地狱

DLL地狱(DLL Hell)指在Microsoft Windows系统中,因为动态链接库(DLL)的版本或兼容性的问题而造成程序无法正常运行。

Windows早期并没有很严谨的DLL版本管理机制,以致经常发生安装了某软件后,因为其覆盖了系统上原有的同一个DLL文件,而导致原有可运行的程序无法运行。但还原回原有的DLL文件之后,所新安装的软件就无法运行。若覆盖到系统所使用的重要DLL时亦可能让系统容易死机甚至无法正常启动。

在CodeBlocks+GCC环境下,静态链接与动态链接设置如下:

静态链接

1.建立静态链接库

示例:

建立静态链接库工程,工程文件包括static.h和static.cpp,具体如下,然后编译工程,会生成一个libStaticLibrary.a文件。

libStaticLibrary.a是用于链接的,与其他文件一起编译生成一个exe执行文件。

static.h

  1. #ifndef STATIC_H_INCLUDED  
  2. #define STATIC_H_INCLUDED  
  3.   
  4. #ifdef __cplusplus  
  5. extern "C"  
  6. {  
  7. #endif  
  8.   
  9. int SampleAddInt(int i1, int i2);  
  10. void SampleFunction1();  
  11. int SampleFunction2();  
  12.   
  13. #ifdef __cplusplus  
  14. }  
  15. #endif  
  16.   
  17. #endif // STATIC_H_INCLUDED  

static.cpp

  1. #include "static.h"  
  2.   
  3. // A function adding two integers and returning the result  
  4. int SampleAddInt(int i1, int i2)  
  5. {  
  6.     return i1 + i2;  
  7. }  
  8.   
  9. // A function doing nothing ;)  
  10. void SampleFunction1()  
  11. {  
  12.     // insert code here  
  13. }  
  14.   
  15. // A function always returning zero  
  16. int SampleFunction2()  
  17. {  
  18.     // insert code here  
  19.   
  20.     return 0;  
  21. }  

2.建立主工程

建立Console application,将生成一个main.cpp示例文件,在最上方添加#include "static.h"语句,这样就可以调用静态链接库里的函数了。

main.cpp

  1. #include <iostream>  
  2. #include "static.h"  
  3. using namespace std;  
  4.   
  5. int main()  
  6. {  
  7.     int a=1,b=2;  
  8.     cout << "a + b = "<<SampleAddInt(a,b)<< endl;  
  9.     return 0;  
  10. }  

然后选择菜单栏Project->Build Options,弹出Project Build Options,选择工程名称。在Linker settings选项卡下添加libStaticLibrary.a的路径,即添加需要的库。在Search directories选项卡下的Compiler子选项卡下添加static.h所在的目录路径,即写入项目的头文件目录。最后,点击编译即可。

动态链接

1.建立动态链接库

示例:

建立动态链接库工程,工程文件包括dynamic.h和dynamic.cpp,具体如下,然后编译工程,会生成一个libDynamicLibrary.a文件和DynamicLibrary.dll文件。

libDynamicLibrary.a是用于链接的,DynamicLibrary.dll用于与将要链接的exe执行文件一起工作。

dynamic.h

  1. #ifndef __DYNAMIC_H__  
  2. #define __DYNAMIC_H__  
  3.   
  4. #include <windows.h>  
  5.   
  6. /*  To use this exported function of dll, include this header 
  7.  *  in your project. 
  8.  */  
  9.   
  10. #ifdef BUILD_DLL  
  11.     #define DLL_EXPORT __declspec(dllexport)  
  12. #else  
  13.     #define DLL_EXPORT __declspec(dllimport)  
  14. #endif  
  15.   
  16.   
  17. #ifdef __cplusplus  
  18. extern "C"  
  19. {  
  20. #endif  
  21.   
  22. void DLL_EXPORT SomeFunction(const LPCSTR sometext);  
  23.   
  24. #ifdef __cplusplus  
  25. }  
  26. #endif  
  27.   
  28. #endif // __DYNAMIC__  


dynamic.cpp

  1. #include "dynamic.h"  
  2.   
  3. // a sample exported function  
  4. void DLL_EXPORT SomeFunction(const LPCSTR sometext)  
  5. {  
  6.     MessageBoxA(0, sometext, "DLL Message", MB_OK | MB_ICONINFORMATION);  
  7. }  
  8.   
  9. extern "C" DLL_EXPORT BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)  
  10. {  
  11.     switch (fdwReason)  
  12.     {  
  13.         case DLL_PROCESS_ATTACH:  
  14.             // attach to process  
  15.             // return FALSE to fail DLL load  
  16.             break;  
  17.   
  18.         case DLL_PROCESS_DETACH:  
  19.             // detach from process  
  20.             break;  
  21.   
  22.         case DLL_THREAD_ATTACH:  
  23.             // attach to thread  
  24.             break;  
  25.   
  26.         case DLL_THREAD_DETACH:  
  27.             // detach from thread  
  28.             break;  
  29.     }  
  30.     return TRUE; // succesful  
  31. }  

2.建立主工程

建立win32 GUI project,将生成一个main.cpp示例文件,在最上方添加include "dynamic.h"语句,这样就可以调用动态链接库里的函数了。

main.cpp

  1. #include <windows.h>  
  2.   
  3. #include "dynamic.h"  
  4.   
  5. /*  Declare Windows procedure  */  
  6. LRESULT CALLBACK WindowProcedure (HWNDUINTWPARAMLPARAM);  
  7.   
  8. /*  Make the class name into a global variable  */  
  9. char szClassName[ ] = "CodeBlocksWindowsApp";  
  10.   
  11. int WINAPI WinMain (HINSTANCE hThisInstance,  
  12.                      HINSTANCE hPrevInstance,  
  13.                      LPSTR lpszArgument,  
  14.                      int nCmdShow)  
  15. {  
  16.     HWND hwnd;               /* This is the handle for our window */  
  17.     MSG messages;            /* Here messages to the application are saved */  
  18.     WNDCLASSEX wincl;        /* Data structure for the windowclass */  
  19.   
  20.     /* The Window structure */  
  21.     wincl.hInstance = hThisInstance;  
  22.     wincl.lpszClassName = szClassName;  
  23.     wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */  
  24.     wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */  
  25.     wincl.cbSize = sizeof (WNDCLASSEX);  
  26.   
  27.     /* Use default icon and mouse-pointer */  
  28.     wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);  
  29.     wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);  
  30.     wincl.hCursor = LoadCursor (NULL, IDC_ARROW);  
  31.     wincl.lpszMenuName = NULL;                 /* No menu */  
  32.     wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */  
  33.     wincl.cbWndExtra = 0;                      /* structure or the window instance */  
  34.     /* Use Windows‘s default colour as the background of the window */  
  35.     wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;  
  36.   
  37.     /* Register the window class, and if it fails quit the program */  
  38.     if (!RegisterClassEx (&wincl))  
  39.         return 0;  
  40.   
  41.     /* The class is registered, let‘s create the program*/  
  42.     hwnd = CreateWindowEx (  
  43.            0,                   /* Extended possibilites for variation */  
  44.            szClassName,         /* Classname */  
  45.            "Code::Blocks Template Windows App",       /* Title Text */  
  46.            WS_OVERLAPPEDWINDOW, /* default window */  
  47.            CW_USEDEFAULT,       /* Windows decides the position */  
  48.            CW_USEDEFAULT,       /* where the window ends up on the screen */  
  49.            544,                 /* The programs width */  
  50.            375,                 /* and height in pixels */  
  51.            HWND_DESKTOP,        /* The window is a child-window to desktop */  
  52.            NULL,                /* No menu */  
  53.            hThisInstance,       /* Program Instance handler */  
  54.            NULL                 /* No Window Creation data */  
  55.            );  
  56.   
  57.     /* Make the window visible on the screen */  
  58.     ShowWindow (hwnd, nCmdShow);  
  59.   
  60.     SomeFunction("Dynamic Tester");  
  61.   
  62.     /* Run the message loop. It will run until GetMessage() returns 0 */  
  63.     while (GetMessage (&messages, NULL, 0, 0))  
  64.     {  
  65.         /* Translate virtual-key messages into character messages */  
  66.         TranslateMessage(&messages);  
  67.         /* Send message to WindowProcedure */  
  68.         DispatchMessage(&messages);  
  69.     }  
  70.   
  71.     /* The program return-value is 0 - The value that PostQuitMessage() gave */  
  72.     return messages.wParam;  
  73. }  
  74.   
  75.   
  76. /*  This function is called by the Windows function DispatchMessage()  */  
  77.   
  78. LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)  
  79. {  
  80.     switch (message)                  /* handle the messages */  
  81.     {  
  82.         case WM_DESTROY:  
  83.             PostQuitMessage (0);       /* send a WM_QUIT to the message queue */  
  84.             break;  
  85.         default:                      /* for messages that we don‘t deal with */  
  86.             return DefWindowProc (hwnd, message, wParam, lParam);  
  87.     }  
  88.   
  89.     return 0;  
  90. }  

然后选择菜单栏Project->Build Options,弹出Project Build Options,选择工程名称。在Linker settings选项卡下添加libDynamicLibrary.a的路径,即添加需要的库。在Search directories选项卡下的Compiler子选项卡下添加dynamic.h所在的目录路径,即写入项目的头文件目录;在Linker子选项卡下添加libDynamicLibrary.a所在的目录路径,即写入库文件目录。最后,点击编译即可。

需要注意的是,以后单独运行编译后的exe执行文件时,需要将dll文件(此处为DynamicLibrary.dll)放在同一目录下,不然运行时可能会崩溃。

技术分享


参考:

codeblocks 配置编译器 1配置编译器的path, include, lib http://www.verydemo.com/demo_c167_i5481.html

C/C++ 跨平台交叉编译、静态库/动态库编译、MinGW、Cygwin、CodeBlocks使用原理及链接参数选项 http://www.cnblogs.com/LittleHann/p/3980364.html

什么是DLL? http://support2.microsoft.com/kb/815065/zh-cn

CodeBlocks静态链接与动态链接设置

标签:交叉编译   mingw   

原文地址:http://blog.csdn.net/qq_20480611/article/details/46276457

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!