我无法让我的runner.c看到我的mergesort.h导入,这是我在进行评论之前需要做的最后一件事。
没有看到我在这里做错了所以我想我会更多的目光来看待它。我在这里查了一些问题,并且几乎复制了我实验室教授的模板,但它仍然不起作用。
错误
/tmp/ccf8TT1E.o: In function `main':
/home/carson/CProgs/lab5/runner.c:10: undefined reference to `mergeSort'
/home/carson/CProgs/lab5/runner.c:11: undefined reference to `print'
collect2: error: ld returned 1 exit status
runner.c
#include <stdio.h>
#include <stdlib.h>
#include "mergesort.h"
int main()
{
int length = 4;
int array[4] = {7,5,3,1};
mergeSort(array, length);
print(array,length);
}
mergesort.h
#ifndef MERGESORT_H_
#define MERGESORT_H_
void mergeSort(int* x, int length);
#endif
mergesort.c
#include <stdio.h>
#include <stdlib.h>
#include "mergesort.h"
void mergeSort(int* array, int length)
{
if(length == 2)
{
if(array[1] < array[0])
{
swap(&array[1],&array[0]);
}
return;
}
else if(length == 1)
{
return;
}
else
{
if(length%2 == 0)
{
int halfLength = length/2;
int bottomHalf[halfLength];
int topHalf[halfLength];
int i;
for(i = 0; i <= (halfLength); i++)
{
bottomHalf[i] = array[i];
topHalf[i] = array[i+(halfLength)];
}
mergeSort(bottomHalf, halfLength);
mergeSort(topHalf, halfLength);
int arrayLoc = 0;
int bottomHalfLoc = 0;
int topHalfLoc = 0;
while(bottomHalfLoc < halfLength && topHalfLoc < halfLength)
{
if(bottomHalf[bottomHalfLoc] > topHalf[topHalfLoc])
{
array[arrayLoc] = topHalf[topHalfLoc];
topHalfLoc++;
}
else
{
array[arrayLoc] = bottomHalf[bottomHalfLoc];
bottomHalfLoc++;
}
arrayLoc++;
}
if(bottomHalfLoc < arrayLoc)
{
while(bottomHalfLoc < halfLength)
{
array[arrayLoc] = bottomHalf[bottomHalfLoc];
bottomHalfLoc++;
arrayLoc++;
}
}
return;
}else
{
int halfLength = length/2;
int bottomHalf[halfLength];
int topHalf[halfLength+1];
int i;
for(i = 0; i <= (halfLength); i++)
{
bottomHalf[i] = array[i];
}
for(i = 0; i <=(halfLength+1); i++)
{
topHalf[i] = array[i+halfLength];
}
mergeSort(bottomHalf, halfLength);
mergeSort(topHalf, halfLength);
int arrayLoc = 0;
int bottomHalfLoc = 0;
int topHalfLoc = 0;
while(bottomHalfLoc < halfLength && topHalfLoc < halfLength)
{
if(bottomHalf[bottomHalfLoc] > topHalf[topHalfLoc])
{
array[arrayLoc] = topHalf[topHalfLoc];
topHalfLoc++;
}
else
{
array[arrayLoc] = bottomHalf[bottomHalfLoc];
bottomHalfLoc++;
}
arrayLoc++;
}
if(bottomHalfLoc < arrayLoc)
{
while(bottomHalfLoc < halfLength)
{
array[arrayLoc] = bottomHalf[bottomHalfLoc];
bottomHalfLoc++;
arrayLoc++;
}
}
return;
}
}
}
void swap(int *smallerValue, int *largerValue)
{
int temp = *smallerValue;
*smallerValue = *largerValue;
*largerValue = temp;
}
void print(int array[], int length)
{
printf("%d",array[0]);
int i;
for(i = 1; i < length; i++)
{
printf(",%d",array[i]);
}
printf("\n");
}
答案 0 :(得分:0)
使用gcc *.c -o runner.out
还有一件事我没有看到mergesort.h文件中的print函数,但是你在main函数中调用它。包含它或使用extern print(array,length);