خرید بک لینک

I am having trouble compiling a C program that uses a shared library. To reduce the problem as much as possible, I am following a tutorial

I have two source files, in the same directory

foo.c:

#include <stdio.h>

extern void foo(void);

void foo(void)
{

    puts("Hello, I am a shared library");

}

main.c:

#include <stdio.h>

int main(void)
{

    puts("This is a shared library test...");
    foo();

    return 0;

}

foo.c compiles to a shared library:

gcc -c -Wall -Werror -fpic foo.c
gcc -shared -o libfoo.so foo.o

but linking main.c always errors

gcc -L/home/ME/tmp/foo -Wall -o test main.c -lfoo

/usr/bin/ld: caot find -lfoo.so: No such file or directory

ls shows a file libfoo.so (permissions 775)

I have tried these variations:

-llibfoo
-lfoo.so
-llibfoo.so

Setting and exporting $LD_LIBRARY_PATH

Using rpath:

gcc -L/home/ME/tmp/foo -Wl,-rpath=/home/ME/tmp/foo -Wall -o test main.c -lfoo

used ldconfig:

sudo ldconfig ./foo

But I always get the same No such file or directory error.

The only way I can succeed is

gcc -L/home/ME/tmp/foo -Wall -o test main.c libfoo.so

but surely that defeats the objective of having a shared library.

How do I compile a program that uses a shared library?

I have the same problem on Linux Mint and piOS on a headless Pi Zero.

Answers to early Idea

ls -la *foo* shows:

-rw-rw-r-- 1   188 Aug 28 14:22 foo.c
-rw-rw-r-- 1  1504 Aug 28 14:26 foo.o
-rwxrwxr-x 1 15544 Aug 28 14:26 libfoo.so

I have corrected the typo in the -L option, but it still fails:

gcc -L/home/ME/tmp/ -Wall -o test main.c -lfoo

/usr/bin/ld: caot find -lfoo: No such file or directory

It does work with a relative path:

gcc -L. -Wall -o test main.c -lfoo
export LD_LIBRARY_PATH=/home/ME/tmp/
./test

I am still considering how that will work with a more complex example.

برچسب: نویسنده: استخدام کار تاريخ: شنبه 7 شهريور 1405 ساعت: 2:40

صفحه بندی