Haslo Vardos
Haslo Vardos

Reputation: 322

C project structure, code sharing and compiling code with dependencies

Hitherto, I had been writing all the code in a single file, so I never had to think of project structure and compiling code with dependencies. Now I am implementing priority queue that use heap code as subroutine and in future I will have to use priority queue as a subroutine to bellmanford. I also test all the algorithm independently as I implement them.

Now to be able to use one piece of code as a subroutine to another I spilt the code in different files. I am unable to run the program after this.

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;
}

Before I split the code in different files - max_heap_header.h, max_heap.c and max_heap_driver_program.c - all the code was in a single file max_heap.c and with command gcc -lm -o max_heap max_heap.c -ggdb -g3 I was able to successfully compile and run the code.


Trials

My shell's interactive prompt is inside Project/Heap/Max_heap folder. Here, I tried command - gcc -I ../lib/ -c ./max_heap.c - that ran successfully and produced a max_heap.o file and then on gcc -o max_heap_driver_program max_heap_driver_program.c max_heap.o, I got:

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

Need help here!


Project structure:

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

The macro functions:

are used in other algorithms as well. So I will want to move that somewhere higher in folder structure. Currently, I define them in every file where they are used.


Keeping future development in mind, how should I go about organising the project?.

Upvotes: 0

Views: 218

Answers (1)

Haslo Vardos
Haslo Vardos

Reputation: 322

There are three issue:

  1. File max_heap_driver_program.c has to include #include "max_heap_header.h".
  2. This file has to be compiled as well. Use gcc -I ../lib/ -c max_heap_driver_program.c.
  3. The linking step should be gcc -o max_heap_driver_program max_heap_driver_program.o max_heap.o -lm. -lm flag is to tell gcc to link the code with math lib. The flag comes after the objects to link.

Upvotes: 1

Related Questions