【发布时间】:2019-02-07 12:09:57
【问题描述】:
手动执行二进制文件时程序执行没有错误。有谁知道这个makefile或源代码有什么问题会使命令“make run”产生错误:
这是生成文件:
# QuickSelect
# Author Nick Gallimore
EXE=QuickSelect
GCC=g++
CFLAGS=-Wall -std=c++17
.PHONY : all
all: $(EXE)
# QuickSelect
.PHONY : run
run : QuickSelect
@./QuickSelect
QuickSelect : QuickSelect.cpp
$(GCC) $^ $(CFLAGS) -o $@
# clean
.PHONY : clean
clean :
rm -f $(EXE)
这里是源代码:
// Author Nick Gallimore
// See https://en.wikipedia.org/wiki/Quickselect
#include <vector>
#include <iostream>
int partition(int list[], int left, int right, int pivotIndex)
{
int pivotValue = list[pivotIndex];
int tmp = list[pivotIndex];
list[pivotIndex] = list[right];
list[right] = tmp;
int storeIndex = left;
for (int i = left; i < right - 1; i++)
{
if (list[i] < pivotValue)
{
tmp = list[storeIndex];
list[storeIndex] = list[i];
list[i] = list[storeIndex];
storeIndex++;
}
}
tmp = list[right];
list[right] = list[storeIndex];
list[storeIndex] = list[right];
return storeIndex;
}
int select(int list[], int left, int right, int k)
{
if (left == right)
{
return list[left];
}
int pivotIndex = right;
pivotIndex = partition(list, left, right, pivotIndex);
if (k == pivotIndex)
{
return list[k];
}
else if (k < pivotIndex)
{
return select(list, left, pivotIndex - 1, k);
}
else
{
return select(list, pivotIndex + 1, right, k);
}
}
int main()
{
// init array with random values
int array[] = {4, 341, 123, 5634, 23, 356, 2887, 76, 45};
auto result = select(array, 0, sizeof(array[0] / sizeof(*array)), 1);
std::cout << result << std::endl;
return result;
}
【问题讨论】:
-
1) 无法重建
QuickSelect时,事件的顺序是什么?在运行 Make 之前是否修改了源代码? 2) 很奇怪,能发minimal complete example吗? -
所以问题实际上与源代码没有被更改有关。我只是个笨蛋。但是我将把这个问题改写成现在的问题:为什么这个程序在使用 ./QuickSelect 运行时会正常执行,但“make run”会产生错误。编辑添加源代码。感谢您的回复。