1

I have installed a C library on my computer (Ubuntu) called xx, whose header file is at /usr/local/include/xx/xx.h and the .a file and .so file are in the path /usr/local/lib named libxx.a and libxx.so.

The test file:

#include<xx/xx.h>
#include<stdio.h>
int main(){
 printf("Test\n");
 call_function_declared_in_the_header_file();
 return 0;
}

When I use gcc to compile gcc test.c -o test, I get the following error:

/tmp/ccb7O0eh.o: In function `main':
test.c:(.text+0xa): undefined reference to `call_function_declared_in_the_header_file'
collect2: error: ld returned 1 exit status

I don't know why. I am not good at the C language. So how can I fix it?

Boann
  • 48,794
  • 16
  • 117
  • 146
ChengTao
  • 87
  • 2
  • 9

1 Answers1

1

you have to link against the library, i.e. if you want to include <math.h> ( libm.so / libm.a ) you have to link against :

gcc program.c -o program -lm where the -lm invokes the linking

see http://www.network-theory.co.uk/docs/gccintro/gccintro_17.html,

https://www.rapidtables.com/code/linux/gcc/gcc-l.html

compiling always involves the compiler and the linker

ralf htp
  • 9,149
  • 4
  • 22
  • 34