3

I am using the latest MinGW to compile my c++ code with g++ compiler on Windows 10. The code compiles without errors but when I run the execution file it gives the error: The procedure entry point _ZNSt6chrono3_V212system_clock3nowEv could not be located in the dynamic link library A:\Code\DAA Assignments\2\outputunsorted1.exe The confusing part is that the same code runs totally fine when compiled with cygwin but gives this error only in MinGW. I also tried reinstalling MinGW multiple times. I checked the MinGW folder, it does have the chrono library, so why is it not finding the entry point. Another weird thing is that the end of the error says "in dynamic link library A:\Code\DAA Assignments\2\outputunsorted1.exe" which is the address of my execution file, so why is the program referring to it as library? My cpp code:

#include<iostream>
#include<fstream>
#include<chrono>
#include"quicksort.cpp"

using namespace std;



int main()
{
    ifstream inp_file;
    ofstream out_file;
    ofstream time_file;

    //First file
    int *arr1 = new int[100000];
    int *arr2 = new int[100000];
    //Iterative quick sort
    inp_file.open("file1.txt");

    for(int i=0;i<100000 ;i++)
    {
        inp_file>>arr1[i];
        inp_file>>arr2[i];

    }
    inp_file.close();
    out_file.open("iterative_quick_sorted_file1.txt");
    auto start = chrono::high_resolution_clock::now();
    iterQuicksort(arr1,0,99999);
    auto elapsed = chrono::high_resolution_clock::now() - start;
    double microseconds = (double)chrono::duration_cast<chrono::microseconds>(elapsed).count()/1000;
    time_file.open("unsorted_iterative_quick_sort_time1.txt");
    time_file<<microseconds;
    time_file.close();
    for(int i=0;i<100000;i++)
    {
        out_file<<arr1[i]<<"\r\n";
    }
    out_file.close();

    //Recursive quick sort
    out_file.open("recursive_quick_sorted_file1.txt");
    start = chrono::high_resolution_clock::now();
    recQuicksort(arr2,0,99999);
    elapsed = chrono::high_resolution_clock::now() - start;
    microseconds = (double)chrono::duration_cast<chrono::microseconds>(elapsed).count()/1000;
    time_file.open("unsorted_recursive_sort_time1.txt");
    time_file<<microseconds;
    time_file.close();
    for(int i=0;i<100000;i++)
    {
        out_file<<arr2[i]<<"\r\n";
    }
    out_file.close();

    return 0;
}  

quicksort.cpp :

void swap(int *a,int *b)
{
    int temp = *a;
    *a = *b;
    *b = temp;
}

int partition (int *arr, int low, int high) 
{ 
    int pivot = arr[high];    // pivot 
    int i = (low - 1);  // Index of smaller element 

    for (int j = low; j <= high- 1; j++) 
    { 
        // If current element is smaller than or 
        // equal to pivot 
        if (arr[j] <= pivot) 
        { 
            i++;    // increment index of smaller element 
            swap(&arr[i], &arr[j]); 
        } 
    } 
    swap(&arr[i + 1], &arr[high]); 
    return (i + 1); 
} 

void iterQuicksort(int *arr,int l, int h)
{ 

    int *stack = new int[h - l + 1]; 


    int top = -1; 


    stack[++top] = l; 
    stack[++top] = h; 


    while (top >= 0) { 

        h = stack[top--]; 
        l = stack[top--]; 


        int p = partition(arr, l, h); 


        if (p - 1 > l) { 
            stack[++top] = l; 
            stack[++top] = p - 1; 
        } 


        if (p + 1 < h) { 
            stack[++top] = p + 1; 
            stack[++top] = h; 
        } 
    } 
} 

void recQuicksort(int *arr,int l,int h)
{
    if(l<h)
    {
        int pivot =  partition(arr,l,h);
        recQuicksort(arr,l,pivot-1);
        recQuicksort(arr,pivot+1,h);
    }
}

command used for compiling:

g++ outputunsorted1.cpp -o outputunsorted1
Shantanu Shinde
  • 932
  • 3
  • 23
  • 48
  • I think the command you used to build the executable would be helpful. You could remove all the irrelevent code as well. Clearly this problem isn't caused by the details of your quicksort routine. – john Jan 27 '19 at 09:11
  • @john I had posted similar problem last time and people down voted for not posting irrelevant code. Now I posted and you are down voting me for that. – Shantanu Shinde Jan 27 '19 at 09:13
  • I haven't down voted you at all. The command to build the executable is quite important though. – john Jan 27 '19 at 09:14
  • Most likely you are getting downvotes cause this question lacks [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – user7860670 Jan 27 '19 at 09:15
  • @john I posted it. but somebody did down vote for no reason – Shantanu Shinde Jan 27 '19 at 09:15
  • I think you also need to compile `quicksort.cpp` along with `outputunsorted1.cpp`: `g++ quicksort.cpp outputunsorted1.cpp -o outputunsorted1` – kiner_shah Jan 27 '19 at 09:16
  • 1
    @kiner_shah it is actually included into first file (which is obviously wrong, but sorta works in this case) – user7860670 Jan 27 '19 at 09:17
  • 1
    @kiner_shah Actually no, because `main.cpp` includes `quicksort.cpp`. That's obviously bad practice but not a error. – john Jan 27 '19 at 09:18
  • @VTT as I said my code works totally fine when compiled with cygwin g++ – Shantanu Shinde Jan 27 '19 at 09:18
  • Did you check the compiler version, `chrono` was introduced in C++11, if I am not wrong? – kiner_shah Jan 27 '19 at 09:20
  • @kiner_shah I downloaded the latest available version of MinGW on the website. – Shantanu Shinde Jan 27 '19 at 09:23
  • @VTT I have recreated the error without the unnecessary code. should I remove this and post that example? – Shantanu Shinde Jan 27 '19 at 09:24
  • 1
    @ShantanuShinde, I found a link about similar problem: [link](https://stackoverflow.com/questions/38554987/mingw-boost-and-runtime-procedure-entry-point-could-not-be-located) – kiner_shah Jan 27 '19 at 09:25
  • You may try to compile with `-std=c÷÷11` option – Damien Jan 27 '19 at 09:30
  • 1
    @kiner_shah with that the program is running bu now it is closing without completing the recursive quicksort part. It runs till recQuicksort function, then closes without any errors. why is this so? – Shantanu Shinde Jan 27 '19 at 09:38
  • @ShantanuShinde, now you must debug. Put some print statements around the code where the problem is occurring and check if the behavior is as expected. – kiner_shah Jan 27 '19 at 09:41
  • @kiner_shah I did that. I put print inside recQuicksort. the program is calling that function but after certain number of calls it crashes. is it because of stack overflow? – Shantanu Shinde Jan 27 '19 at 09:44
  • Your code runs correctly (See [here](https://ideone.com/Ezs3nu)), can you please put the main function code as well as the input you are passing. – kiner_shah Jan 27 '19 at 09:46
  • @kiner_shah actually I am sorting 100k integers to compare runtime of iterative and recursive quicksort. – Shantanu Shinde Jan 27 '19 at 09:48
  • Does it print some message when it crashes? Or does it generate a core dump file? – kiner_shah Jan 27 '19 at 09:54
  • @kiner_shah no, but it used to do that in cygwin. I know this is off topic but can you suggest how to increase stack size to prevent stack overflow – Shantanu Shinde Jan 27 '19 at 09:56
  • See [link](https://stackoverflow.com/questions/2275550/change-stack-size-for-a-c-application-in-linux-during-compilation-with-gnu-com). Also, find a way to do that (generate core dump file) in MinGW. I think there was a command `ulimit unlimited`. – kiner_shah Jan 27 '19 at 09:58
  • @kiner_shah the link you sent me for stack size is for linux. it is giving me error resource.h file not present. mingw doesnt have it in its sys folder. cygwin does have it though – Shantanu Shinde Jan 27 '19 at 15:03

2 Answers2

10

The problem was with libstd++6.dll. It was solved by using the argument -static-libstdc++ in g++. MinGW was taking libstd++6.dll from Windows instead of the one in MinGW.

Shantanu Shinde
  • 932
  • 3
  • 23
  • 48
0

It's most likely that you have an old compiler version that does not yet natively support C++11. Try compiling with -std=c++11. Otherwise update compiler.

Rokas Višinskas
  • 533
  • 4
  • 12