c - C项目结构、代码共享和编译代码的依赖
问题描述
迄今为止,我一直将所有代码都写在一个文件中,所以我从来不用考虑项目结构和编译带有依赖项的代码。现在我正在实现使用堆代码作为子程序的优先级队列,将来我将不得不使用优先级队列作为 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 - 所有代码都在一个文件max_heap.c中,并且通过命令gcc -lm -o max_heap max_heap.c -ggdb -g3
我能够成功编译和运行代码。
试验
我的 shell 的交互式提示位于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 将代码与数学库链接。标志位于要链接的对象之后。
推荐阅读
- python - H5PY 键读取速度慢
- 3d - 通过两条平行线计算单应性
- javascript - 更改绑定对象的值时,Vue v-bind:类无法立即工作
- css - Bootstrap + 列 + 全高 + 倾斜
- xml - 在 Haskell 的 xml-conduit 中,如何创建节点?
- c++ - c ++为什么还剩下输入缓冲区?
- javascript - ReactJS:未捕获的 ReferenceError:$ 未定义
- c++ - 使用 Eigen 求解线性方程组
- javascript - 当在 React 构造函数中不访问 props 时,将 props 传递给 super() 的原因是什么?
- c++ - C++:如果我重载 new 运算符,我是否也必须重载 delete 运算符?