Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linking to so library in gcc

I will start by saying that I am new to gcc and makefiles. I have a .so file on the desktop (~/Desktop) called lib.so. I want to link my program (called myProgram) to it. What I wrote in my makefile is:

g++ myProgram.o -L ~/Desktop -l lib -o myProgram

When I run make I get an error:

/usr/bin/ld: cannot find -llib

I also tried -l lib.so and got the same error. What is the correct way to link?

like image 293
Benjy Kessler Avatar asked Mar 31 '13 07:03

Benjy Kessler


1 Answers

Two solutions:

  1. Rename the file to libsomething.so, then use -l something. The linker automatically wraps the name with lib prefix and .so suffix (or .a suffix for static libraries).

  2. Use the option -l :lib.so. When you prefix the name with :, the linker uses the name as given.

These are explained in the ld man page.

like image 109
Barmar Avatar answered Oct 17 '22 01:10

Barmar