当我构建main.cpp时出现错误:对WinMain @ 16的未定义引用,请使用Code :: Blocks在代码块中创建dll项目 当我构建main.cpp时遇到错误:对WinMain @ 16的未定义引用,请使用Code :: Blocks在代码块中创建dll项目 当我构建main.cpp时遇到错误:对WinMain @ 16的未定义引用,请使用Code :: Blocks在代码块中创建dll项目 当我生成main.cpp时出错:对WinMain @ 16的未定义引用,请使用Code :: Blocks在代码块中创建dll项目
main.cpp(由代码:块创建)
#include "main.h"
#include <string>
#include <fstream>
#include <windows.h>
#include <iostream>
#include <io.h>
using namespace::std;
void DLL_EXPORT createFiles(int filesAmount){
int n=5;
int a[5] = {1, 23, 47, 45, 21};
for(int i=0; i<filesAmount; i++){
string s1 = "files\\";
string s2 = to_string(i+1);
string s3 = s1 + s2 + ".txt";
ofstream fout(s3);
fout << "0";
fout.close();
}
for (int i=0; i<n; i++){
string s1 = "files\\";
string s2 = to_string(a[i]);
string s3 = s1 + s2 + ".txt";
ofstream fout (s3);
fout << "1";
fout.close();
}
}
DWORD WINAPI DLL_EXPORT lookForFile(void *data, HANDLE hSem){
Pair *pairs = (Pair *) data;
int start = (pairs->start);
int finish = (pairs->finish);
char a;
DWORD dwWaitResult = WaitForSingleObject( hSem, 1);
while(dwWaitResult!=WAIT_OBJECT_0)
{
cout << pairs->threadNumber << " thread waiting for semaphore..."
<<endl;
dwWaitResult = WaitForSingleObject( hSem, 1);
}
cout << pairs->threadNumber << " thread has been runned"<<endl;
Sleep(100);
for(int i=start; i<=finish; i++){
string s1 = "files\\";
string s2 = to_string(i+1);
string s3 = s1 + s2 + ".txt";
ifstream F(s3);
a = F.get();
if(a == '1')
cout << "\"1\" was found by " << (pairs->threadNumber) << " thread
in " << s2 <<".txt"<<endl;
F.close();
}
ReleaseSemaphore( hSem, 1, NULL );
cout << pairs->threadNumber << " thread has been finished"<<endl;
return 0;
}
void DLL_EXPORT makeCounts(int filesAmount, int numberOfThreads, Pair
pairs[]){
int fp = filesAmount/numberOfThreads-1;
int st = 0;
for(int i = 0; i < numberOfThreads; i++){
if(i == numberOfThreads-1){
pairs[i].start = st;
pairs[i].finish = filesAmount-1;
}
else{
pairs[i].start = st;
pairs[i].finish = st+fp;
st+=fp+1;
}
}
}
extern "C" DLL_EXPORT BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD
fdwReason, LPVOID lpvReserved)
{
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
// attach to process
// return FALSE to fail DLL load
break;
case DLL_PROCESS_DETACH:
// detach from process
break;
case DLL_THREAD_ATTACH:
// attach to thread
break;
case DLL_THREAD_DETACH:
// detach from thread
break;
}
return TRUE; // succesful
}
main.h(由代码:块创建)
#ifndef __MAIN_H__
#define __MAIN_H__
struct Pair {
int start;
int finish;
int threadNumber;
};
#include <windows.h>
#ifdef BUILD_DLL
#define DLL_EXPORT __declspec(dllexport)
#else
#define DLL_EXPORT __declspec(dllimport)
#endif
#ifdef __cplusplus
extern "C"
{
#endif
void DLL_EXPORT createFiles(int filesAmount);
DWORD WINAPI DLL_EXPORT lookForFile(void *data, HANDLE hSem);
void DLL_EXPORT makeCounts(int filesAmount, int numberOfThreads, Pair
pairs[]);
#ifdef __cplusplus
}
#endif
#endif // __MAIN_H__