0

When I build main.cpp get error: undefined reference to WinMain@16, use Code::Blocks created dll project in codeblocks When I build main.cpp get error: undefined reference to WinMain@16, use Code::Blocks created dll project in codeblocks When I build main.cpp get error: undefined reference to WinMain@16, use Code::Blocks created dll project in codeblocks When I build main.cpp get error: undefined reference to WinMain@16, use Code::Blocks created dll project in codeblocks

main.cpp (created by code:blocks)

 #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 (created by code:blocks)

#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__
  • 1
    If you search for `undefined reference to WinMain@16` you should get a *lot* of hits, including quite a few here on SO itself. – Some programmer dude Oct 21 '19 at 10:29
  • And don't repeat yourself just to fill out space, [write good questions](http://stackoverflow.com/help/how-to-ask) and you don't have to do that. – Some programmer dude Oct 21 '19 at 10:31
  • And on an unrelated note, all symbols beginning with double underscore (like e.g. `__MAIN_H__`) are *reserved* and should not be defined by your code. See e.g. [What are the rules about using an underscore in a C++ identifier?](https://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) for details. – Some programmer dude Oct 21 '19 at 10:32
  • Apparently your IDE is trying to build an EXE, whereas you want a DLL. I recommend using MS Visual Studio instead. – rustyx Oct 21 '19 at 12:00

0 Answers0