【发布时间】:2019-01-27 09:08:33
【问题描述】:
我正在使用最新的 MinGW 在 Windows 10 上使用 g++ 编译器编译我的 c++ 代码。代码编译时没有错误,但是当我运行执行文件时出现错误:The procedure entry point _ZNSt6chrono3_V212system_clock3nowEv could not be located in the dynamic link library A:\Code\DAA Assignments\2\outputunsorted1.exe 令人困惑的部分是相同的代码运行使用 cygwin 编译时完全正常,但仅在 MinGW 中出现此错误。我也尝试过多次重新安装 MinGW。我检查了 MinGW 文件夹,它确实有 chrono 库,为什么它没有找到入口点。另一个奇怪的事情是错误的末尾说“在动态链接库A:\ Code\DAA Assignments\2\outputunsorted1.exe中”这是我的执行文件的地址,那么为什么程序将它称为库?
我的 cpp 代码:
#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;
}
快速排序.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);
}
}
用于编译的命令:
g++ outputunsorted1.cpp -o outputunsorted1
【问题讨论】:
-
我认为您用于构建可执行文件的命令会有所帮助。您也可以删除所有不相关的代码。显然,这个问题不是由您的快速排序例程的细节引起的。
-
@kiner_shah 它实际上包含在第一个文件中(这显然是错误的,但在这种情况下可以正常工作)
-
@kiner_shah 实际上没有,因为
main.cpp包括quicksort.cpp。这显然是不好的做法,但不是错误。 -
@ShantanuShinde,我找到了一个类似问题的链接:link
-
@kiner_shah 表示程序正在运行,但现在它正在关闭而没有完成递归快速排序部分。它一直运行到 recQuicksort 函数,然后关闭而没有任何错误。为什么会这样?
标签: c++ cygwin mingw mingw-w64 chrono