26

I created a .so file and put it in the location /opt/lib and added this path to LD_LIBRARY_PATH now after this when I try to compile my main program with the following command:

g++ -Wall -I/home/alwin/Development/Calculator/ main.cpp -lcalc -o calculator

I get the following error:

/usr/bin/ld: cannot find -lcalc
collect2: ld returned 1 exit status

Can someone help me with this. I created the shared library using the code blocks IDE

Maximilian
  • 31
  • 1
  • 7
Alwin Doss
  • 962
  • 2
  • 16
  • 33

5 Answers5

26

Add -L/opt/lib to your compiler parameters, this makes the compiler and linker search that path for libcalc.so in that folder.

Dr. Snoopy
  • 55,122
  • 7
  • 121
  • 140
  • Oh yeah I tried that too, still I got the same error :( see the following alwin@alwin-laptop:~/Desktop/CPPTest$ g++ -Wall -I/home/alwin/Development/Calculator/ -L/opt/lib main.cpp -libcalc.so -o calculator /usr/bin/ld: cannot find -libcalc.so collect2: ld returned 1 exit status – Alwin Doss Mar 16 '11 at 19:53
  • 5
    @Jay it's -lcalc, not -libcalc.so – Dr. Snoopy Mar 16 '11 at 21:51
3

When you make the call to gcc it should say

g++ -Wall -I/home/alwin/Development/Calculator/ -L/opt/lib main.cpp -lcalc -o calculator

not -libcalc.so 

I have a similar problem with auto-generated makes.

You can create a soft link from your compile directory to the library directory. Then the library becomes "local".

cd /compile/directory

ln -s  /path/to/libcalc.so libcalc.so
samayo
  • 16,163
  • 12
  • 91
  • 106
otter
  • 23
  • 1
2

@Alwin Doss You should provide the -L option before -l. You would have done the other way round probably. Try this :)

Sunil
  • 856
  • 12
  • 24
2

You need to add -L/opt/lib to tell ld to look there for shared objects.

geekosaur
  • 59,309
  • 11
  • 123
  • 114
  • Like I already mentioned in the previous comment, I did add -L/opt/lib is there anything else that I am missing :( – Alwin Doss Mar 16 '11 at 19:56
0

export LDFLAGS=-L/path to lib/ this will solve the error