Rookie Engineer

If you aren't the kind of person that feels this way naturally, you'll need to become one in order to make it as a hacker. Otherwise you'll find your hacking energy is sapped by distractions like sex, money, and social approval.

  C++博客 :: 首页 :: 联系 :: 聚合  :: 管理
  24 Posts :: 0 Stories :: 2 Comments :: 0 Trackbacks

常用链接

留言簿

我参与的团队

搜索

  •  

最新评论

阅读排行榜

评论排行榜

The Static Library

The library is pretty standard, in that the function is declared in the header and defined within the 'c/cpp' file. The files are listed below:

Add a new project: Win32 Console Application------Application Type: Static library

addStaticd.h:

#ifndef ADD_H

#define ADD_H

int addStatic(int a, int b);

#endif  // ADD_H

addStatic.cpp:

#include "addStatic.h"

int addStatic(int a, int b) {

    return a + b;

}

 

The Dynamic Library

The tutorial for the dynamic library is intended to be as similar as possible to the static library. You'll notice that the only addition is __declspec(dllexport), which is a Microsoft specific identifier, which identifies that the function will be used as a DLL (Dynamically Linked Library) export. The Cpp file is exactly the same as the previous examples.

Add a new project: win32 console Application-----Application Type: Dll

addDynamic.h:

#ifndef ADD_H

#define ADD_H

int __declspec(dllexport) addDynamic(int a, int b);

#endif  // ADD_H

addDynamic.cpp:

#include "addDynamic.h"

int addDynamic(int a, int b) {

    return a + b;

}

New a solution, add a new project---Application Type: Console Application

Referencing the Static Library

The implementation of the library is pretty standard. You must have access to the header file and reference it. If you do not have the header file, or do not wish to reference it, you must indicate the function definition with the extern tag. The main.cpp file is pretty standard, and demonstrates how to run a static library.

main.cpp:

#include <iostream>

#include "addStatic.h"

#include <stdio.h>

 

Using namespace std;

 

int main() {

 

    cout<<addStatic(1,2)<<endl;

    getchar();

    return 0;

}


The Configuration Method (Recommend Use: Third Party Libraries with Special Configurations)

The reasons for this library are similar to that mentioned in the drag and drop method. However, this method gives you more control. This is best suited for a third party library.

The configuration serves the exact same purposes as the drag and drop method, but is more difficult. Navigate to Project->Properties-> Input->Additional Dependencies and add the directory path to the source of your library. Additional dependencies are references using your solution file as the base directory, so that '.' indicates the current solution directory followed by the path to the direct library file. You can also explicitly write in the whole directory path, i.e., C:\Users\.... Should you require further assistance, refer to the MSDN article which is very specific but not very graphical or explanatory.

The path was “addStatic.lib”

 

Referencing the Dynamic Library

The first thing you should notice is that the header file has been modified to import the library with __declspec(dllimport) as opposed to __declspec(dllexport). This tells the compiler that this is referencing a dynamic library. This process seems redundant to explicitly declare your import or export statements as you would either be compiling a library or exporting a library, and most likely, neither doing both in the same project. This is why I prefer the GCC compiler to produce dynamic libraries. In addition, the Microsoft Visual C++ IDE requires that you link the *.lib file when producing your shared/dynamic libraries (although, it is possible that you can link your dynamic libraries explicitly using the Windows API; this will be covered in the next article). In addition, keep in mind that you still require the header file or require the function definition.

main.cpp

Collapse | Copy Code

#include <iostream>

#include "addStatic.h"

#include <stdio.h>

 

Using namespace std;

 

int main() {

 

cout<<addStatic(1,2)<<endl;

cout<<addDynamic(1,2)<<endl;

    getchar();

    return 0;

}


The Configuration Method (Recommended Use: Third Party Libraries with Special Configurations) same as Static

 http://www.codeproject.com/KB/cpp/libraries1.aspx

posted on 2011-09-05 18:08 micwu 阅读(575) 评论(0)  编辑 收藏 引用 所属分类: C++

只有注册用户登录后才能发表评论。
【推荐】超50万行VC++源码: 大型组态工控、电力仿真CAD与GIS源码库
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理