Home > Back-end >  symbol not found when compiling c program
symbol not found when compiling c program

Time:07-21

I am quite new to C and I am having problems with compiling these files.

Project layout:

.
├── Makefile
├── README
├── build
└── src
    ├── main.c
    ├── max_array.c
    ├── max_array.h
    ├── mean_array.c
    ├── mean_array.h
    ├── merge_sort.c
    ├── merge_sort.h
    ├── min_array.c
    ├── min_array.h
    ├── util.c
    └── util.h

Output of make:

cc -Wall -Werror -Wextra -Wpedantic --std=c99 -g -c src/main.c -o src/main.o
cc -Wall -Werror -Wextra -Wpedantic --std=c99 -g -c src/min_array.c -o src/min_array.o
cc -Wall -Werror -Wextra -Wpedantic --std=c99 -g -c src/max_array.c -o src/max_array.o
cc -Wall -Werror -Wextra -Wpedantic --std=c99 -g -c src/mean_array.c -o src/mean_array.o
cc -Wall -Werror -Wextra -Wpedantic --std=c99 -g -c src/util.c -o src/util.o
cc -Wall -Werror -Wextra -Wpedantic --std=c99 -g -c src/merge_sort.c -o src/merge_sort.o
cc -Wall -Werror -Wextra -Wpedantic --std=c99 -g -o build/pjs  src/main.o  src/min_array.o  src/max_array.o  src/mean_array.o  src/util.o  src/merge_sort.o
Undefined symbols for architecture arm64:
  "_print_sort_debug", referenced from:
      _merge in merge_sort.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [build/pjs] Error 1

inside of merge_sort.c I have included util.h

contents of merge_sort.c:

#include "merge_sort.h"
#include "util.h"

void merge(double arr[], int l, int m, int r){
  int l_size = (m - l)   1;
  int r_size = (r - m);
  int count, x, y;

  double left[l_size];
  double right[r_size];

  for (x = 0; x < l_size;   x) {
    left[x] = arr[x   l];
  }

  for (y = 0; y < r_size;   y) {
    right[y] = arr[y   m   1];
  }

  print_sort_debug(arr, l_size, r_size, left, right, l, m, r, 5);

  x = y = count = 0;
  while (x < l_size && y < r_size) {
    if (left[x] > right[y]) {
      arr[count] = right[y];
        y;
    } else {
      arr[count] = left[x];
        x;
    }
      count;
  }

  while (x < l_size) {
    arr[count] = left[x];
      x;
      count;
  }

  while (y < r_size) {
    arr[count] = right[y];
      y;
      count;
  }
}

void merge_sort(double arr[], int l, int r){
  if (r > l) {
    int m = (r   l) / 2;

    merge_sort(arr, l, m);
    merge_sort(arr, m   1, r);

    merge(arr, l, m, r);
  }
}

Contents of util.h:

#ifndef UTIL_H
#define UTIL_H

void print_arr(double * arr, int arr_size);

void print_sort_debug(double *arr, int l_size, int r_size, double *left, double *right, int l, int m, int r, int arr_size);

#endif // UTIL_H

Contents of util.c:

#include <stdio.h>

#include "util.h"

void print_arr(double *arr, int arr_size) {
  int x;
  for (x = 0; x < arr_size; x  ){
    printf("%.2f\n", arr[x]);
  }
}

void debug_print(double *arr, int l_size, int r_size, double *left, double *right, int l, int m, int r, int arr_size) {
  printf("-------------Left\n");
  print_arr(left, l_size);
  printf("-------------Right\n");
  print_arr(right, r_size);
  printf("-------------Arr\n");
  print_arr(arr, arr_size);
  printf("l: %i\n", l);
  printf("m: %i\n", m);
  printf("r: %i\n", r);
  printf("l_size: %i\n", l_size);
  printf("r_size: %i\n", r_size);
  printf("-------------Done\n");
}

Here are the contents of my Makefile:

CC=cc
CF=-Wall -Werror -Wextra -Wpedantic --std=c99 -g
SRC=src/main.c src/min_array.c src/max_array.c src/mean_array.c src/util.c src/merge_sort.c
OBJS=${patsubst %.c, %.o, ${SRC}}
all: build/pjs

build/pjs: $(OBJS)
    $(CC) $(CF) -o $@ $(OBJS)

%.o: %.c
    $(CC) $(CF) -c $^ -o $@

clean:
    -rm src/*.o build/pjs

I don't understand why I can't compile this. Thanks, in advance.

CodePudding user response:

The declaration in util.h

void print_sort_debug(double *arr, int l_size, int r_size, double *left,
                      double *right, int l, int m, int r, int arr_size);

does not match the definition in util.c

void debug_print(double *arr, int l_size, int r_size, double *left,
                 double *right, int l, int m, int r, int arr_size) {

The simple fix would be to make the definition match the declaration:

void print_sort_debug(double *arr, int l_size, int r_size, double *left,
                      double *right, int l, int m, int r, int arr_size) {

CodePudding user response:

Make sure you recompile all the source files.

cc -Wall -std=c99 -o build/pjs src/main.c src/util.c src/merge_sort.c

Or use a makefile that automatically recompiles files as needed.

  • Related