Johir Johir - 1 year ago 82
C Question

Cannot find -lCommunication collect2: error: ld returned 1 exit status

I do not know

gcc
and
c
well. In my
/home/pi/Desktop/intern/adis16227_generic
directory I have following 5 files.

ADIS16227.c
ADIS16227.h
Communication.c
Communication.h
main.c


main.c

#include<stdio.h>
#include "Communication.h" // Communication definitions.

int main() {
printf("hello!!\n");
unsigned char status = 0;
status = SPI_Init(0, 1000000, 1, 1);
printf("%u", status);
return 0;
}


Run command:

$ sudo gcc -L /home/pi/Desktop/intern/adis16227_generic main.c -lCommunication


Error:

/usr/bin/ld: cannot find -lCommunication
collect2: error: ld returned 1 exit status


Question:

What I am missing here?

What do I need to run the code?

Answer Source

those options:

-L /home/pi/Desktop/intern/adis16227_generic -lCommunication

suggest that the linker should find libCommunication.a (or .so) in the /home/pi/Desktop/intern/adis16227_generic directory.

But there are only sources in this directory. The linker won't build the sources of your "Communication" library for you.

So you could build the library and link with it:

gcc -c ADIS16227.c Communication.c
ar r libCommunication.a ADIS16227.o Communication.o

but maybe the fastest & quickest way to achieve a successful build would be:

sudo gcc -o main *.c

so it compiles all the files of the directory into the executable called main

Of course, it makes compilation times longer, but maybe it's not noticeable.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download