2

I am trying to use two functions from two different files. I put these three files in the same folder. The compiler gives an error on these two functions that I am linking to. Here is the main file that I am running.

#include <stdio.h>
#include <stdlib.h>
#include "max_pool.h"
#include "convolutional.h"
#include "matrix_to_array.h"

int main() {
  image im = load_image("doge.jpg");
  int* result;
  int scale = 100;
  int* input = arrayUpScaling(im, im.h, im.w, im.c, scale);
  result = pool_max(im.h, im.w, im.c, input);
  int h = im.h;
  int w = im.w;
  int c = im.c;
  for (int k = 0; k < c; k++) {
    for (int i = 0; i < h; i++ ) {
      for (int j = 0; j < w; j++ ) {
        int dst_index = j + w*i + w*h*k;
        printf("result[%d][%d][%d]: %d ", i, j, k, result[dst_index]);
      }
      printf("\n");
    }
  }
  return 0;
}

In thie file, two functions from other files are called. One is load_image(), the other is arrayUpScaling()

Here is convolutional.h

#ifndef CON_H
#define CON_H
#include "image_to_matrix.h"

int SIZE[3];
//This program is a multiplier which supports input to be 4/8/16 bits
//It consists of three parts, a multiplier, an adder and an acumulator 
which
//support 16/18/20/22 bits. This program will return a 8 bits value
int* readImage(image im, int scale);
int* get_Data_From_Con(int h, int w, int c, int* up_scaled_input, int 
filter_size, int filter[filter_size][filter_size], int stride, int scale);
void buildSize(int* SIZE, int h, int w, int c);
int** truncateArray(int h, int w, int filter_size, int** input, int col, int row);

int** addPadding(int h, int w, int input[h][w], int h_padding, int w_padding);
int** inputManagement(int h, int w, int filter_size, int input[h][w],
                        int filter[filter_size][filter_size], int stride,
                          int scale);
int multiplier(int first, int second, int bitwidth, int scale);
int accumulator(int previous, int product);
int mat(int size, int** input, int filter[size][size], int scale);
int* arrayUpScaling(image im, int h, int w, int c, int scale);
float** arrayDownScaling(int h, int w, int scale, int** input);
void freeIntArray(int rows, int** arr);
void freeDoubleArray(int rows, float** arr);
int relu(int input);

#endif

Here is the max_pool.h

#ifndef MAXP_H
#define MAXP_H
int* pool_max(int h, int w, int c, int* data);
int selectMax(int h, int w, int filter_size, int input[h][w], int col, int row);
#endif

Here is the image_to_matrix.h

#ifndef IMA_H
#define IMA_H

typedef struct{
    int w,h,c;
    float *data;
} image;

float get_pixel(image im, int x, int y, int c);
image make_image(int w, int h, int c);
image load_image(char *filename);
image make_empty_image(int w, int h, int c);
image load_image_stb(char *filename, int channels);
void free_image(image im);

#endif

Below is showing the error

Undefined symbols for architecture x86_64:
  "_arrayUpScaling", referenced from:
      _main in max_pool-7ba8aa.o
  "_load_image", referenced from:
      _main in max_pool-7ba8aa.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Here is the compile command that I am using

gcc -Wall -std=c11 -g -o max max_pool.c

All the files are in the same folder, why the linking is ever going to be the issue?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Liu Hantao
  • 620
  • 1
  • 9
  • 19
  • 1
    What are you typing when you compile the executable? – Increasingly Idiotic May 11 '18 at 21:07
  • 2
    You're probably neglecting to tell the compiler/linker which additional file(s) need to be linked in to pick up the definitions of those external functions. Merely including the header files doesn't do that. – Steve Summit May 11 '18 at 21:11
  • gcc -Wall -std=c11 -g -o max max_pool.c – Liu Hantao May 11 '18 at 21:14
  • do NOT place data instances in a header file. You can place 'extern' statements to the data, you can place function prototypes, your can place struct definitions, etc, but NOT the actual data – user3629249 May 11 '18 at 21:23
  • 1
    this command line: `gcc -Wall -std=c11 -g -o max max_pool.c` only tries to compile and link one source file. You actually have 3 source files. The easiest is to have 3 command lines which include the option `-c` one command for each source file. Then a 4th command (no `-c`) the references the 3 object files and the desired executable name to link it all together. Each compile statement should enable the warnings, so besides `-Wall` you should also have `-Wextra -Wconversion -pedantic -std=gnu11` (cont) – user3629249 May 11 '18 at 21:31
  • (cont) and if you want the final executable to be easily processed by the `gdb` debugger, then the compile statements AND the linker statement should all have the `-ggdb` option – user3629249 May 11 '18 at 21:31

0 Answers0