LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   gcc wont use libary in /usr/loacal/... (https://www.linuxquestions.org/questions/programming-9/gcc-wont-use-libary-in-usr-loacal-723685/)

Skaarj 05-04-2009 04:55 PM

gcc wont use libary in /usr/loacal/...
 
hello

im writing a program which uses libssh ( http://0xbadc0de.be/wiki/libssh:libssh ), its called sclient (s = student)

due to a problem with the ssh server i want to make a modification in the sshlib, therfore i have to (re)build the sshlib myself

i decided to make a trivial test-modification on the sshlib before doing any real changes so i can test if the compile/link/build-process works, but i cant even manage to build sclient with trivial changes in sshlib

i made the follwoing trivial changes to the sshlib:

Code:

added to channels.c
int returnTwoTimes(int in) {
    return in*2;
}

Code:

added to libssh.h
int returnTwoTimes(int in);

i was able to build the sshlib without any errors (./configure .... make .... make install )

so my modified sshlib is installed in /usr/local/{include|lib} while the systems original is installed in /usr/{include|lib}

Code:

ls /usr/local/lib
libssh.a  libssh.la  libssh.so  libssh.so.2  libssh.so.2.0.0  python2.5

Code:

ls /usr/local/include/libssh
config.h  crypto.h  libssh.h  server.h  sftp.h  ssh1.h  ssh2.h

Code:

grep 'returnTwo' /usr/local/include/libssh/libssh.h
int returnTwoTimes(int in);

my problem is: i cannot build my sclient using the modified sshlib

Code:

snapshot from sclient.c
#include <stdio.h>
#include <stdlib.h>
#include <libssh/libssh.h>
...
int main() {
int testVar=returnTwoTimes(3);
if (testVar==6)  {
    return 0;
} else {
    return 1;
}
...

when compiling with -lssh he uses the non modified sshlib in /usr/lib/ and not the sshlib in /usr/local/...

Code:

gcc -I/usr/local/include/ -L/usr/local/lib/ -lssh sclient.c
/tmp/ccPqh3b5.o: In function `main':
sclient.c:(.text+0x30): undefined reference to `returnTwoTimes'
collect2: ld returned 1 exit status

when compiling without -lssh he wont find the libssh at all
Code:

gcc -I/usr/local/include/ -L/usr/local/lib/ sclient.c
/tmp/ccwQYFJf.o: In function `main':
sclient.c:(.text+0x30): undefined reference to `returnTwoTimes'
/tmp/ccwQYFJf.o: In function `connect_to_server':
sclient.c:(.text+0x83): undefined reference to `ssh_new'
sclient.c:(.text+0x98): undefined reference to `ssh_set_options'
sclient.c:(.text+0xa3): undefined reference to `ssh_connect'
sclient.c:(.text+0xb7): undefined reference to `ssh_get_error'
/tmp/ccwQYFJf.o: In function `authenticate':
sclient.c:(.text+0xf3): undefined reference to `ssh_userauth_password'
/tmp/ccwQYFJf.o: In function `managechannel':
sclient.c:(.text+0x660): undefined reference to `channel_new'
sclient.c:(.text+0x66e): undefined reference to `channel_open_session'
sclient.c:(.text+0x67d): undefined reference to `ssh_get_error'
sclient.c:(.text+0x698): undefined reference to `channel_request_pty'
sclient.c:(.text+0x6c0): undefined reference to `channel_request_shell'
sclient.c:(.text+0x6cf): undefined reference to `ssh_get_error'
sclient.c:(.text+0x727): undefined reference to `channel_poll'
sclient.c:(.text+0x73b): undefined reference to `ssh_get_error'
sclient.c:(.text+0x766): undefined reference to `channel_write'
sclient.c:(.text+0x78e): undefined reference to `ssh_get_error'
sclient.c:(.text+0x7bb): undefined reference to `channel_is_open'
/tmp/ccwQYFJf.o: In function `set_options':
slient.c:(.text+0x7d5): undefined reference to `ssh_options_new'
slient.c:(.text+0x7ef): undefined reference to `ssh_options_set_username'
sclient.c:(.text+0x806): undefined reference to `ssh_options_set_host'
sclient.c:(.text+0x821): undefined reference to `ssh_options_set_timeout'
/tmp/ccwQYFJf.o: In function `serverknown':
sclient.c:(.text+0x848): undefined reference to `ssh_is_server_known'
sclient.c:(.text+0x8a0): undefined reference to `ssh_get_pubkey_hash'
sclient.c:(.text+0x8ab): undefined reference to `ssh_write_knownhost'
collect2: ld returned 1 exit status

i tryed several -L and -I paramteres already (adding an directory level , leaving out an directory level , giving complete path to files instead of just path to directory) , but nothing worked ...

how do i make gcc build sclient using the sslib in /usr/local/... ??

thanks for help, skaarj

some more system info:
Code:

uname -a
Linux boxName 2.6.27-11-server #1 SMP Wed Apr 1 21:53:55 UTC 2009 i686 GNU/Linux
lsb_release -a
Distributor ID:        Ubuntu
Description:        Ubuntu 8.10
Release:        8.10
Codename:        intrepid
gcc --version
gcc (Ubuntu 4.3.2-1ubuntu12) 4.3.2


rylan76 05-05-2009 02:42 AM

Quote:

Originally Posted by Skaarj (Post 3530096)
how do i make gcc build sclient using the sslib in /usr/local/... ??

Well, you can try using a symlink - I've often solved these types of library related errors that way.

I. e. in /usr/lib, rename sshlib.a (or whatever) to sshlib.a.old. Then, symlink sshlib.a in /usr/lib to mysshlib.a (or whatever) in /usr/local/lib. Then, if the linker is looking for sshlib.a, it will still find it in /usr/lib, but it will be symlinked to mysshlib.a in /usr/local/lib...

Code:

# mv /usr/lib/sshlib.a /usr/lib/sshlib.a.old
# cd /usr/lib
# ln -s  /usr/local/lib/mysshlib.a sshlib.a

Hope this helps...

Sergei Steshenko 05-05-2009 02:43 AM

First of all, install your version into an "innocent" directory (like /home/yourid/yourssh), not into /usr/local/lib.

Second, use 'nm' to verify that the missing 'returnTwoTimes' symbol is indeed defined in your library.

Skaarj 05-05-2009 01:13 PM

Quote:

Originally Posted by rylan76 (Post 3530489)
Well, you can try using a symlink - I've often solved these types of library related errors that way.

that wont really solve the problem, what if there is one day a libary i cant move but i want to build my own version ?



Quote:

Originally Posted by Sergei Steshenko (Post 3530490)
First of all, install your version into an "innocent" directory (like /home/yourid/yourssh), not into /usr/local/lib.

moving it somwehre into ~ didnt help with building and i wast overwriting any eisting files

Quote:

Originally Posted by Sergei Steshenko (Post 3530490)
Second, use 'nm' to verify that the missing 'returnTwoTimes' symbol is indeed defined in your library.

Code:

nm /usr/local/lib/libssh.so | grep return
0000a5a0 t returnTwoTimes

someone got an ida ?

Sergei Steshenko 05-05-2009 02:30 PM

Quote:

Originally Posted by Skaarj (Post 3531090)
...
Code:

nm /usr/local/lib/libssh.so | grep return
0000a5a0 t returnTwoTimes

someone got an ida ?

You've been looking for returnTwoTimes in a wrong file - .so files are dynamically linked at runtime (AFAIK/AFAIR), and you wanted static linking.

Check libssh.a; temporarily rename libssh.la or vice versa, i.e. narrow the choices available to linker.

Skaarj 05-05-2009 02:59 PM

Quote:

Originally Posted by Sergei Steshenko (Post 3531179)
You've been looking for returnTwoTimes in a wrong file - .so files are dynamically linked at runtime (AFAIK/AFAIR), and you wanted static linking.

Check libssh.a; temporarily rename libssh.la or vice versa, i.e. narrow the choices available to linker.

Code:

nm /usr/local/lib/libssh.a | grep return
000000e0 T returnTwoTimes


renaming didnt work either, also gcc -static .... dosent work

:(

Sergei Steshenko 05-05-2009 03:33 PM

Quote:

Originally Posted by Skaarj (Post 3531212)
Code:

nm /usr/local/lib/libssh.a | grep return
000000e0 T returnTwoTimes


renaming didnt work either, also gcc -static .... dosent work

:(

000000e0 T returnTwoTimes - that's what needs to be seen. You do not need '-static'.

...

I think you need

Code:

gcc -I/usr/local/include/ -L/usr/local/lib/ sclient.c -lssh
, i.e. put the library after the file to be compiled.

Another idea:

Code:

gcc -I/usr/local/include/ sclient.c -o sclient.o
gcc -L/usr/local/lib/ sclient.o -lssh

but I think it's equivalent to the above.

Skaarj 05-05-2009 03:53 PM

didnt work either :(


Code:

gcc -I/usr/local/include/ -L/usr/local/lib/ sclient.c -lssh
/tmp/ccUqSRGj.o: In function `main':
sclient.c:(.text+0x30): undefined reference to `returnTwoTimes'
collect2: ld returned 1 exit status



Code:

gcc -I/usr/local/include/ sclient.c -o sclient.o
/tmp/ccCwToy8.o: In function `main':
sclient.c:(.text+0x30): undefined reference to `returnTwoTimes'
/tmp/ccCwToy8.o: In function `connect_to_server':
sclient.c:(.text+0x83): undefined reference to `ssh_new'
sclient.c:(.text+0x98): undefined reference to `ssh_set_options'
sclient.c:(.text+0xa3): undefined reference to `ssh_connect'
sclient.c:(.text+0xb7): undefined reference to `ssh_get_error'
/tmp/ccCwToy8.o: In function `authenticate':
sclient.c:(.text+0xf3): undefined reference to `ssh_userauth_password'
/tmp/ccCwToy8.o: In function `managechannel':
sclient.c:(.text+0x660): undefined reference to `channel_new'
sclient.c:(.text+0x66e): undefined reference to `channel_open_session'
sclient.c:(.text+0x67d): undefined reference to `ssh_get_error'
sclient.c:(.text+0x698): undefined reference to `channel_request_pty'
sclient.c:(.text+0x6c0): undefined reference to `channel_request_shell'
sclient.c:(.text+0x6cf): undefined reference to `ssh_get_error'
sclient.c:(.text+0x727): undefined reference to `channel_poll'
sclient.c:(.text+0x73b): undefined reference to `ssh_get_error'
sclient.c:(.text+0x766): undefined reference to `channel_write'
sclient.c:(.text+0x78e): undefined reference to `ssh_get_error'
sclient.c:(.text+0x7bb): undefined reference to `channel_is_open'
/tmp/ccCwToy8.o: In function `set_options':
sclient.c:(.text+0x7d5): undefined reference to `ssh_options_new'
sclient.c:(.text+0x7ef): undefined reference to `ssh_options_set_username'
sclient.c:(.text+0x806): undefined reference to `ssh_options_set_host'
sclient.c:(.text+0x821): undefined reference to `ssh_options_set_timeout'
/tmp/ccCwToy8.o: In function `serverknown':
sclient.c:(.text+0x848): undefined reference to `ssh_is_server_known'
sclient.c:(.text+0x8a0): undefined reference to `ssh_get_pubkey_hash'
sclient.c:(.text+0x8ab): undefined reference to `ssh_write_knownhost'
collect2: ld returned 1 exit status


Sergei Steshenko 05-05-2009 03:57 PM

Quote:

Originally Posted by Skaarj (Post 3531272)
didnt work either :(


Code:

gcc -I/usr/local/include/ -L/usr/local/lib/ sclient.c -lssh
/tmp/ccUqSRGj.o: In function `main':
sclient.c:(.text+0x30): undefined reference to `returnTwoTimes'
collect2: ld returned 1 exit status



Code:

gcc -I/usr/local/include/ sclient.c -o sclient.o
/tmp/ccCwToy8.o: In function `main':
sclient.c:(.text+0x30): undefined reference to `returnTwoTimes'
/tmp/ccCwToy8.o: In function `connect_to_server':
sclient.c:(.text+0x83): undefined reference to `ssh_new'
sclient.c:(.text+0x98): undefined reference to `ssh_set_options'
sclient.c:(.text+0xa3): undefined reference to `ssh_connect'
sclient.c:(.text+0xb7): undefined reference to `ssh_get_error'
/tmp/ccCwToy8.o: In function `authenticate':
sclient.c:(.text+0xf3): undefined reference to `ssh_userauth_password'
/tmp/ccCwToy8.o: In function `managechannel':
sclient.c:(.text+0x660): undefined reference to `channel_new'
sclient.c:(.text+0x66e): undefined reference to `channel_open_session'
sclient.c:(.text+0x67d): undefined reference to `ssh_get_error'
sclient.c:(.text+0x698): undefined reference to `channel_request_pty'
sclient.c:(.text+0x6c0): undefined reference to `channel_request_shell'
sclient.c:(.text+0x6cf): undefined reference to `ssh_get_error'
sclient.c:(.text+0x727): undefined reference to `channel_poll'
sclient.c:(.text+0x73b): undefined reference to `ssh_get_error'
sclient.c:(.text+0x766): undefined reference to `channel_write'
sclient.c:(.text+0x78e): undefined reference to `ssh_get_error'
sclient.c:(.text+0x7bb): undefined reference to `channel_is_open'
/tmp/ccCwToy8.o: In function `set_options':
sclient.c:(.text+0x7d5): undefined reference to `ssh_options_new'
sclient.c:(.text+0x7ef): undefined reference to `ssh_options_set_username'
sclient.c:(.text+0x806): undefined reference to `ssh_options_set_host'
sclient.c:(.text+0x821): undefined reference to `ssh_options_set_timeout'
/tmp/ccCwToy8.o: In function `serverknown':
sclient.c:(.text+0x848): undefined reference to `ssh_is_server_known'
sclient.c:(.text+0x8a0): undefined reference to `ssh_get_pubkey_hash'
sclient.c:(.text+0x8ab): undefined reference to `ssh_write_knownhost'
collect2: ld returned 1 exit status


Sorry, the second set of commands should have been

Code:

gcc -I/usr/local/include/ -c sclient.c -o sclient.o
gcc -L/usr/local/lib/ sclient.o -lssh

- I forgot to put '-c' which means compile only, do not link.

Then

Code:

gcc -L/usr/local/lib/ sclient.o -lssh
will link only - no compilation.

Skaarj 05-05-2009 04:25 PM

Code:

gcc -I/usr/local/include/ -c studclient.c -o sclient.o
no output and return code is 0 => works

Code:

gcc -L/usr/local/lib/ sclient.o -lssh
sclient.o: In function `main':
sclient.c:(.text+0x30): undefined reference to `returnTwoTimes'
collect2: ld returned 1 exit status

i also tryed with and without -lssh and i tryed varying the -L parameter ... nothing works

dwhitney67 05-05-2009 05:03 PM

I tried something similar to what you are doing, although the opposite, and it worked for me.

I typically install my library in /usr/local/lib, but for test purposes I installed it in /usr/lib. Then I modified my library code, then built it and installed it /usr/local/lib.

When I built a test app that uses the library, it picked up the one in /usr/local/lib; not the one in /usr/lib.

I think the reason for this is with how 'ldconfig' collects a listing of the libraries that it is made aware of. I have a file in /etc/ld.so.conf.d, name libc.conf that specifies the /usr/local/lib directory.

So if you do not have a libc.conf file, create it with the following contents:
Code:

# libc default configuration
/usr/local/lib

Then, if not already there, place your modified library in /usr/local/lib. Then run the following command as root:
Code:

ldconfig
You should then be able to build your application, and without the need to specify the -L option. Let's hope this works!

Now, I'm off to clean up my /usr/lib folder...

Skaarj 05-05-2009 05:13 PM

my /etc/ld.so.conf.d/libc.conf looks like the one you posted .... also running ldconf didnt change anything ...


and:
Code:

cat /etc/ld.so.conf
include /etc/ld.so.conf.d/*.conf


dwhitney67 05-05-2009 05:45 PM

Rerun the 'ldconfig' (at root) and specify the -v option. I'm curious as to whether your modified library shows up in the list.
Code:

ldconfig -v | grep libssh
P.S. On my Ubuntu 9.04 system, when I run the 'ldconfig -v', the directories are listed in this order:
Code:

/lib/i486-linux-gnu:
/usr/lib/i486-linux-gnu:
/usr/lib/alsa-lib:
        ...
/usr/local/lib:
        ...
/lib:
        ...
/usr/lib:
        ...

...


Skaarj 05-05-2009 05:57 PM

Code:

/lib/i486-linux-gnu:
/usr/lib/i486-linux-gnu:
/usr/local/lib:
    libssh.so.2 -> libssh.so.2.0.0
/lib:
    libmemusage.so -> libmemusage.so
    ... SNIP ...
    libfreetype.so.6 -> libfreetype.so.6.3.18
    libssh.so.2 -> libssh.so.2.0.0
    libsasl2.so.2 -> libsasl2.so.2.0.22
    libglib-2.0.so.0 -> libglib-2.0.so.0.1800.2
    libltdl.so.7 -> libltdl.so.7.1.2
    libssh2.so.1 -> libssh2.so.1.0.0
    libexpat.so.1 -> libexpat.so.1.5.2
    ... no more occurencys of ssh ...


dwhitney67 05-05-2009 07:54 PM

I do not know what to say. Just now, I developed a silly shared-object library, and installed it in /usr/lib.

Then I added a new function to the library, and installed the modified library in /usr/local/lib.

When I built my test application, I specified the library located in /usr/local/lib, so that I could link without any getting any "unresolved reference" errors.

When I ran the application, it automatically loaded the shared-object library in /usr/lib, thus causing the application to abort because it could not reference a symbol (i.e. the function I added).

When I re-ran the application by first specifying the LD_LIBRARY_PATH=/usr/local/lib, then it worked.

Here's my silly library that I tested with...

MyLib.h
Code:

#ifndef MY_LIB_H
#define MY_LIB_H

int doubleTheValue(const int value);

#ifdef MOD
int tripleTheValue(const int value);
#endif

#endif

MyLib.c
Code:

#include "MyLib.h"

int doubleTheValue(const int value)
{
  return 2 * value;
}

#ifdef MOD
int tripleTheValue(const int value)
{
  return 3 * value;
}
#endif

To build/install in /usr/lib:
Code:

gcc -c MyLib.c
gcc -static -o libMyLib.so MyLib.o
sudo install -m 644 libMyLib.so /usr/lib

To build/install the modified library in /usr/local/lib:
Code:

gcc -DMOD -c MyLib.c
gcc -static -o libMyLib.so MyLib.o
sudo install -m 644 libMyLib.so /usr/local/lib

My test application looks like:
Code:

#include "MyLib.h"
#include <stdio.h>

int main()
{
  printf("2 * 10 = %d\n", doubleTheValue(10));
  printf("3 * 10 = %d\n", tripleTheValue(10));
}

To compile and run:
Code:

gcc test.c -L/usr/local/lib -lMyLib
LD_LIBRARY_PATH=/usr/local/lib a.out

Anyhow, it seems like you were doing things correctly from the beginning, but merely neglecting to specify the LD_LIBRARY_PATH. Hopefully this will sort out your issues.


All times are GMT -5. The time now is 03:07 AM.