Could not find -lCommunication collect2: error: ld returned 1 exit status

I do not know gccand c. In my directory /home/pi/Desktop/intern/adis16227_generic, I have the 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;
}

Launch 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 am I missing here?

What do I need to run the code?

+4
source share
4 answers

these parameters:

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

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

But there are only sources in this directory. The compiler will not create the sources of your Communication library for you.

So you can create a library and link it to it:

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

but perhaps the fastest and fastest way to achieve a successful build:

sudo gcc -o main *.c

main

, , , , .

+1

-l , Communication.c. - Communication.c .


-c :

gcc -c -Wall -Wextra -pedantic -omain.o main.c
gcc -c -Wall -Wextra -pedantic -oCommunication.o Communication.c

..... ( , )

.o . , , , .

:

gcc -oprogram main.o Communication.o

- say - Communication.c ADIS16227.c, :

gcc -c -Wall -Wextra -pedantic -oCommunication.o Communication.c
gcc -c -Wall -Wextra -pedantic --oADIS16227.o ADIS16227.c

ar :

ar rcs libCommunication.a Communication.o ADIS16227.o

( -lCommunication).


: root. . sudo .

+3

/home/pi/Desktop/intern/adis16227_generic:

cd /home/pi/Desktop/intern/adis16227_generic

:

gcc ADIS16227.c Communication.c main.c -I .

Now you can run your compiled program (called by default a.out):

./a.out
+1
source

You need to compile individual files and then compile main with the corresponding obj file.

gcc -c Communication.c Communication.h
gcc main.c Communication.o -o main 
+1
source

Source: https://habr.com/ru/post/1683895/


All Articles