如何修复分配代码中的链接器错误"Undefined symbols"?



很抱歉为一些人带来这个小问题,但我在调用insertionsort函数时遇到了问题。

这位教授说,返回值要么是0,要么是1。1表示成功,0表示失败。我只需要用文件中的标记对链表进行排序。我只是在测试如何实现这个功能,因为这个功能对项目来说是最重要的。

这正是这位教授将要竞选并希望看到的。-I用于插入排序,-q用于编译时的快速排序:

./fileSort -i ./somefile

如果"./somefile"包含:

嗨,那里,每个,一个

则输出应为:

every hi one there

所以我需要调用两个函数:

int insertionsort(void *toSort, int (*cmp)(void*, void*));
int quicksort(void *toSort, int (*cmp)(void*, void*));

但到目前为止,我正在努力完成一个,我很沮丧。任何帮助都会很棒。

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <ctype.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#define BUFSIZE 8
int compare(void *, void *);
void toSort(char *, int,  int (*cmp)(void*, void*));
void swap(char *, char *);
void print_array (char **, int );
// int insertionsort(void *toSort, int (*comparator)(void*, void*));

int main(int argc, char const *argv[])
{
int insertionsort(void *toSort, int (*cmp)(void*, void*));
//build an array
char *array[BUFSIZE] = {"a", "b", "c", "d", "e", "f", ""};
int n = sizeof(array)/sizeof(array[0]);
if(n == -1)
{
printf("Error getting the size of the arrayn");
}
printf("Size of the array: %dn", n);
printf("Sorting the array.n");
insertionsort(toSort, compare);
print_array(array, n);
printf("n");
return 0;
}
int compare( void * a,  void * b) 
{
int val1 = *(int*)a;
int val2 = *(int*)b;
return ( val1 - val2 ); 
}
void toSort(char *array, int size, int (*cmp)(void*, void*))
{
int i, j;
for ( i = 1; i < size; i++ )
{
j = i;
while ( j > 0 && cmp( (char*)array+j-1, (char*)array+j ) > 0 )
// ^^^ call of cmp instead of operator >
{
swap( (char*)array+j-1, (char*)array+j);
j--;
}
}
}
void swap(char *a, char *b)
{
char *temp;
temp = a;
a = b;
b = temp;
}
void print_array (char **array, int size)
{
int i;
for (i = 0; i < size; i++) {
printf ("%d: %sn", i, array[i]);
}
}

这导致了一个编译错误:

Undefined symbols for architecture x86_64:                                         
"_insertionsort", referenced from:                                               
_main in funcPointer-3f472a.o                                                
ld: symbol(s) not found for architecture x86_64                                    
clang: error: linker command failed with exit code 1 (use -v to see invocation)

错误消息是一个链接器错误,告诉您在构建过程中没有找到要调用的函数的实现
原型直接出现在显示的代码中(以及其他一些提示(,这让我认为应该由您实际实现该功能。

因此,为了解决您引用的错误,您可以简单地在您有注释的地方包含以下内容,该注释显示了缺失函数的原型:

// prototype, to tell the compiler about the existence of this function
int insertionsort(void *toSort, int (*comparator)(void*, void*));
// implementation of that function
// (the separation is not really needed but a recommended practice,
// to prepare for later work with multiple code files
int insertionsort(void *toSort, int (*comparator)(void*, void*))
{
/* empty, the point of this code is only to get rid of the linker error,
to be filled in by you, implementation according to what you learned
in class about sorting
*/
return 0; // to match the prototype with return value int
/* find out about this return value,
i.e. how the function is supposed to be used and what it should return
*/
}

相关内容

最新更新