C项目结构,代码共享和编译具有依赖关系的代码



到目前为止,我一直在一个文件中编写所有代码,所以我从来没有想过项目结构和编译有依赖关系的代码。现在我正在实现使用堆代码作为子例程的优先级队列,将来我将不得不使用优先级队列作为bellmanford的子例程。在实现这些算法时,我还独立测试了它们。

现在,为了能够将一段代码用作另一段代码的子例程,我将代码拆分到不同的文件中在此之后我无法运行程序

最大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);

最大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);
}
}

最大heap_river_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.hmax_heap.cmax_heap_river_program.c——所有代码都在一个文件中,使用命令gcc -lm -o max_heap max_heap.c -ggdb -g3,我能够成功编译并运行代码。


试验

我的shell的交互式提示位于Project/Hip/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);

也用于其他算法。所以我想把它移到文件夹结构中更高的位置。目前,我在每个使用它们的文件中都定义了它们。


考虑到未来的发展,我应该如何组织项目

有三个问题:

  1. 文件max_heap_river_program.c必须包含#include "max_heap_header.h"
  2. 这个文件也必须编译。使用gcc -I ../lib/ -c max_heap_driver_program.c
  3. 链接步骤应为gcc -o max_heap_driver_program max_heap_driver_program.o max_heap.o -lm-lm标志是告诉gcc将代码与math-lib链接起来。标志位于要链接的对象之后

最新更新