作者在 2011-11-29 22:45:13 发布以下内容
In generally, we use the Third software company provides lib or dll. The Third company will provide the common header file to help us find the appropriate interface to access.
whereas upper discription.
The common header file format is like (CommonCore.h)
#ifndef _COMMONCORE_H_
#define _COMMONCORE_H_
#define _COMMONCORE_H_
// upper is to avoid the header files to be included repeatedly
#ifndef _CommonCoreDll // _CommonCoreDll it should be defined in the Third dll project.
#define _CommonCoreDll __declspec(dllimport)
#else
#define _CommonCoreDll __declspec(dllexport)
#endif
#define _CommonCoreDll __declspec(dllimport)
#else
#define _CommonCoreDll __declspec(dllexport)
#endif
#endif // _COMMONCORE_H_
meanwhile, it will provide the necessary exported class or interface . just include the header file. those files also need to be exported to relative postion.
It just like that: (DataController.h)
#include "CommonCore.h"
#include <iostream>
using namespace std;
#include <iostream>
using namespace std;
template <typename T>
class _CommonCoreDll DataController
{
public:
DataController()
{
}
virtual ~DataController()
{
}
void Sort(T* pData, int nSize);
};
{
public:
DataController()
{
}
virtual ~DataController()
{
}
void Sort(T* pData, int nSize);
};
Only declaration, no implemented file (.cpp)
This dll project will produce a .dll and relative .lib file plus the .h header file. we invoke them to finish our functions.
This dll project will produce a .dll and relative .lib file plus the .h header file. we invoke them to finish our functions.
Note that Copy all .dll and .lib and relative header file to your localization projects.
it just looks like that.(main .cpp)
#include <iostream>
using namespace std;
#include "Person.h"
#include <ctime>
#include <map>
#include "DataController.h"
using namespace std;
#include "Person.h"
#include <ctime>
#include <map>
#include "DataController.h"
// load your static lib in your application
#pragma comment(lib,"E:\\Code\\MySortLib\\Debug\\MySortLib.lib")
// if you hate to write this line, you also can set debug and link path.
// I think it 's too trouble to do it.
//
void print(Person* person, int nLen)
{
for(int i=0; i< 10; i++)
{
cout << i << " --> " << person[i].m_Score << endl;
}
}
void print(Person* person, int nLen)
{
for(int i=0; i< 10; i++)
{
cout << i << " --> " << person[i].m_Score << endl;
}
}
int main()
{
map<int, float> personMap;
srand(time(NULL));
Person person[10];
for (int i=0; i< 10; ++i)
{
person[i].m_Score = (float)(rand() %101);
personMap.insert(make_pair(i,person[i].m_Score));
}
{
map<int, float> personMap;
srand(time(NULL));
Person person[10];
for (int i=0; i< 10; ++i)
{
person[i].m_Score = (float)(rand() %101);
personMap.insert(make_pair(i,person[i].m_Score));
}
print(person,10);
cout <<"---------------------------------------" << endl;
DataController<Person> controller;
controller.Sort(person,10);
// controller.Sort(personMap.begin(),10);
print(person,10);
DataController<Person> controller;
controller.Sort(person,10);
// controller.Sort(personMap.begin(),10);
print(person,10);
cout << "-------------------------------------------" << endl;
for (map<int, float>::iterator pos = personMap.begin(); pos != personMap.end(); ++pos )
{
cout <<pos->first << "--->" << (*pos).second << endl;
}
return 0;
}
{
cout <<pos->first << "--->" << (*pos).second << endl;
}
return 0;
}
PS person.h
class Person
{
public:
Person();
virtual ~Person();
float m_Score;
{
public:
Person();
virtual ~Person();
float m_Score;
bool operator <(const Person& person) // it is convenient to compare between 2 Person objects.
{
return (m_Score < person.m_Score)? true:false;
}
};
{
return (m_Score < person.m_Score)? true:false;
}
};
The source code if you need , can contact me. I will send it ASAP.
Best regards
Pnfeng.