迄今为止,我一直将所有代码编写在一个文件中,因此,我不必考虑项目结构和使用依赖关系编译代码。现在,我正在实现使用堆代码作为子例程的优先级队列,将来,我将不得不使用优先级队列作为bellmanford的子例程。在实施它们时,我还将独立测试所有算法。
现在能够将一个代码段用作另一个代码的子例程,所以我将代码散布在不同的文件中。 此后我无法运行该程序。
max_heap_header.h
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define arr_length(arr) (sizeof(arr) == 0 ? 0 : sizeof(arr) / sizeof((arr)[0]));
#define swap(arr, x, y) do { typeof(arr[x]) temp = arr[x]; arr[x] = arr[y]; arr[y] = temp; } while (0);
#define parent(i) (floor(i));
#define left(i) ((i << 1) + 1);
#define right(i) ((i << 1) + 2);
/* function declaration */
void max_heapify (int arr[], int i, int arr_len);
/* produce a max heap from an unsorted array */
void build_max_heap (int arr[], int arr_len);
/* sorts an array in place */
void heapsort (int arr[], int arr_len);
max_heap.c
#include "max_heap_header.h"
void max_heapify (int arr[], int i, int arr_len)
{
int largest = i;
int l = left(i);
int r = right(i);
if (l < arr_len && arr[l] > arr[i]) {
largest = l;
}
if (r < arr_len && arr[r] > arr[largest]) {
largest = r;
}
if (largest != i) {
swap(arr, i, largest);
max_heapify(arr, largest, arr_len);
}
}
void build_max_heap (int arr[], int arr_len)
{
int heap_size = arr_len;
for (int i = floor(arr_len / 2); i >= 0; i--) {
max_heapify(arr, i, arr_len);
}
}
void heap_sort (int arr[], int arr_len)
{
build_max_heap(arr, arr_len);
for (int i = arr_len; i >= 1; i--) {
swap(arr, 0, i);
max_heapify(arr, 0, arr_len);
}
}
max_heap_driver_program.c
int main ()
{
int arr[] = {4, 1, 3, 2, 16, 9, 10, 14, 8, 7};
int arr_len = arr_length(arr);
build_max_heap(arr, arr_len);
for (int i = 0; i < arr_len; i++) {
printf("%d ", arr[i]);
}
return 0;
}
在将代码分割到不同文件中之前-max_heap_header.h,max_heap.c和max_heap_driver_program.c-所有代码都在单个文件中[gcc -lm -o max_heap max_heap.c -ggdb -g3
能够成功编译并运行代码。
Project / Heap / Max_heap
文件夹中。在这里,我尝试了命令-gcc -I ../lib/ -c ./max_heap.c
-成功运行并生成了一个[[max_heap.o文件,然后在gcc -o max_heap_driver_program max_heap_driver_program.c max_heap.o
上,我得到了:max_heap_driver_program.c: In function ‘main’:
max_heap_driver_program.c:4:17: warning: implicit declaration of function ‘arr_length’ [-Wimplicit-function-declaration]
int arr_len = arr_length(arr);
^
max_heap_driver_program.c:6:3: warning: implicit declaration of function ‘build_max_heap’ [-Wimplicit-function-declaration]
build_max_heap(arr, arr_len);
^
max_heap_driver_program.c:9:2: warning: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]
printf("%d ", arr[i]);
^
max_heap_driver_program.c:9:2: warning: incompatible implicit declaration of built-in function ‘printf’
max_heap_driver_program.c:9:2: note: include ‘<stdio.h>’ or provide a declaration of ‘printf’
/tmp/ccnyLTUs.o: In function `main':
max_heap_driver_program.c:(.text+0x6a): undefined reference to `arr_length'
max_heap.o: In function `build_max_heap':
max_heap.c:(.text+0x151): undefined reference to `floor'
collect2: error: ld returned 1 exit status
这里需要帮助!项目结构:
Project |--Heap |--Max_Heap |--max_heap.c |--max_heap_driver_program.c |--Min_Heap |--min_heap.c |--min_heap_driver_program.c |--lib |--max_heap_header.h |--Priority_Queue |--max_priority_queue.c |--min_priority_queue.c |--Bellmanford |--bellmanford.c |--Other_Algorithms |--Other_Algorithms
#define arr_length(arr) (sizeof(arr) == 0 ? 0 : sizeof(arr) / sizeof((arr)[0]));
#define swap(arr, x, y) do { typeof(arr[x]) temp = arr[x]; arr[x] = arr[y]; arr[y] = temp; } while (0);
[[牢记未来的发展,我应该如何组织项目?。
max_heap_driver_program.c必须包含#include "max_heap_header.h"
。
gcc -I ../lib/ -c max_heap_driver_program.c
。gcc -o max_heap_driver_program max_heap_driver_program.o max_heap.o -lm
。 -lm标志是告诉gcc将代码与math lib链接。该标志位于要链接的对象之后。