Karim Kouznetsov
Karim Kouznetsov

Reputation: 133

undefined reference while linking with a shared object

I'm a dumb newbie.

I've got a file named file.c with the functions my_putstr(char *) and my_strlen(char *)

my_putstr() writes the parameter with write() (unistd.h)

I wanted to create a library from file.c so I did :

gcc -fPIC -c file.c
gcc -shared -o libfile.so file.o

Then I created a main.c file and called my_putstr() from it. I tried to compile and link my .so

gcc -L. -lfile main.c -o test

But I got an undefined reference to my_putstr()

I tried to create a .h with my_putstr() and my_strlen() in it, and include it to the main but I got the same error.

Sorry for stupid questions.

Havaniceday.

Upvotes: 1

Views: 8066

Answers (1)

Konstantin Vladimirov
Konstantin Vladimirov

Reputation: 6989

Your question suffers lack of information, but I can suggest you at first try

gcc main.c ./libfile.so -Wl,-rpath . -o test

If this will fail, you have something wrong with your sources. If everything is ok at this point, then try

gcc main.c -L . -lfile -Wl,-rpath . -o test

If this will output undefined reference, then probably you already have something like libfile.a without my_putstr(may be from previous experiments) in your lib path.

If everything is ok with it, then your linker is sensible to order in which libraries is supplied to command string, and you must remember, then library always comes after object, that uses this library.

Upvotes: 1

Related Questions