【问题标题】:C project structure, code sharing and compiling code with dependenciesC项目结构,代码共享和编译代码的依赖
【发布时间】:2020-02-29 02:57:00
【问题描述】:

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

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

ma​​x_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);

ma​​x_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);
  }
}

ma​​x_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.hmax_heap.cmax_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" 了吗?
  • 我将它包含在 max_heap.c 中,而不是 max_heap_driver_program.c 中。我还编辑了问题以删除#include "max-heap.h 中的错字。如果它包含在那里,当我链接gcc -o max_heap_driver_program max_heap_driver_program.c max_heap.o 时它不应该工作吗?
  • 你必须从max_heap_driver_program.c#include "max_heap_header.h"

标签: c gcc compilation code-reuse


【解决方案1】:

有三个问题:

  1. 文件 max_heap_driver_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 将代码与数学库链接。标志位于要链接的对象之后。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-21
    • 1970-01-01
    • 2017-08-27
    • 2019-06-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-09
    相关资源
    最近更新 更多