sanatan
sanatan

Reputation: 63

gcc linker not finding the .so library file

I am trying to make a small application for my arm target. I have already set up the toolchain in eclipse under linux. Now i am trying to build libraries.

here is my library header file led.h:

#ifndef LED_H_
#define LED_H_

extern char ledon(char lednum);
extern char ledoff(char lednum);

#endif /* LED_H_ */

and my c file led.c :

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <led.h>
int fd = 0;

char ledon(char lednum)
{
    switch(lednum)
    {
    case 0 :fd = open("/sys/class/leds/beaglebone::usr0/brightness", O_WRONLY);
               break;
    case 1 :fd = open("/sys/class/leds/beaglebone::usr1/brightness", O_WRONLY);
                break;
    case 2 :fd = open("/sys/class/leds/beaglebone::usr2/brightness", O_WRONLY);
                break;
    case 3 :fd = open("/sys/class/leds/beaglebone::usr3/brightness", O_WRONLY);
                break;

    }
    if (fd>0)
    {
        write(fd,"1",sizeof(char));
        close(fd);
        return 1;
     }
    else
    {
        printf("Message form library function\n Error reading the LED %d",lednum);
        return 0;
    }


}

char ledoff(char lednum)
{
    switch(lednum)
    {
    case 0 :fd = open("/sys/class/leds/beaglebone::usr0/brightness", O_WRONLY);
               break;
    case 1 :fd = open("/sys/class/leds/beaglebone::usr1/brightness", O_WRONLY);
                break;
    case 2 :fd = open("/sys/class/leds/beaglebone::usr2/brightness", O_WRONLY);
                break;
    case 3 :fd = open("/sys/class/leds/beaglebone::usr3/brightness", O_WRONLY);
                break;

    }
    if (fd>0)
    {
        write(fd,"0",sizeof(char));
        close(fd);
        return 1;
     }
    else
    {
        printf("Message form library function\n Error reading the LED %d",lednum);
        return 0;
    }


}

compiling the above i get led.h and libled.so.

now i create a test c file ledtest.c in a different project.

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <led.h>

int main(void) {

    while(1)
    {
        ledon(2);
        sleep(1);
        ledoff(2);
        sleep(1);
    }

    return EXIT_SUCCESS;
}

I made two folders "include" which contains led.h and "lib" which contains libled.so.

I have configured as follows: Include paths library paths

Now when i compile ledtest.c i get the following error

make all 
Building target: ledtest
Invoking: GCC C Linker
arm-linux-gnueabi-gcc -L/usr/arm-linux-gnueabi/lib -L/home/sanatan/Coustom/lib -o      
   "ledtest"  ./src/ledtest.o   
./src/ledtest.o: In function `main':
ledtest.c:(.text.startup+0x4): undefined reference to `ledon'
ledtest.c:(.text.startup+0x10): undefined reference to `ledoff'
collect2: ld returned 1 exit status
make: *** [ledtest] Error 1

But if i add the libled.so in the libraries :

i get the below error:

**** Build of configuration Release for project ledtest ****

make all 
Building file: ../src/ledtest.c
Invoking: GCC C Compiler
arm-linux-gnueabi-gcc -I/home/sanatan/Coustom/include -I/usr/arm-linux-
    gnueabi/include -O3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/ledtest.d" -
    MT"src/ledtest.d" -o "src/ledtest.o" "../src/ledtest.c"
Finished building: ../src/ledtest.c

Building target: ledtest
Invoking: GCC C Linker
arm-linux-gnueabi-gcc -L/usr/arm-linux-gnueabi/lib -L/home/sanatan/Coustom/lib -o 
    "ledtest"  ./src/ledtest.o   -l/home/sanatan/Coustom/lib/libled.so
/usr/lib/gcc/arm-linux-gnueabi/4.6.1/../../../../arm-linux-gnueabi/bin/ld: cannot 
    find -l/home/sanatan/Coustom/lib/libled.so
collect2: ld returned 1 exit status
make: *** [ledtest] Error 1

**** Build Finished ****

But i am sure the file is there :

root@ubuntu:/home/sanatan/Coustom/lib# ls
led.o  libled.so

Can someone please help me out here, i don't know what to do. Believe me i did my homework.. :)

thanks in advance.

Sanatan

Upvotes: 2

Views: 2879

Answers (1)

mathematician1975
mathematician1975

Reputation: 21351

Whenever you want to link to a library you need to include the option

    -lxyz

to link to library with name libxyz and possibly include the path to it using a -L option. As the comment of Fred says, it would appear you may need to add the -lled option.

For details on how to add a library to a project in Eclipse, see the answer to this question

Upvotes: 2

Related Questions