LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-04-2009, 04:55 PM   #1
Skaarj
LQ Newbie
 
Registered: May 2009
Posts: 9

Rep: Reputation: 0
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
 
Old 05-05-2009, 02:42 AM   #2
rylan76
Senior Member
 
Registered: Apr 2004
Location: Potchefstroom, South Africa
Distribution: Fedora 17 - 3.3.4-5.fc17.x86_64
Posts: 1,552

Rep: Reputation: 103Reputation: 103
Quote:
Originally Posted by Skaarj View Post
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...
 
Old 05-05-2009, 02:43 AM   #3
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
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.
 
Old 05-05-2009, 01:13 PM   #4
Skaarj
LQ Newbie
 
Registered: May 2009
Posts: 9

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by rylan76 View Post
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 View Post
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 View Post
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 ?

Last edited by Skaarj; 05-05-2009 at 01:17 PM.
 
Old 05-05-2009, 02:30 PM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Skaarj View Post
...
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.
 
Old 05-05-2009, 02:59 PM   #6
Skaarj
LQ Newbie
 
Registered: May 2009
Posts: 9

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Sergei Steshenko View Post
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

:(
 
Old 05-05-2009, 03:33 PM   #7
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Skaarj View Post
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.
 
Old 05-05-2009, 03:53 PM   #8
Skaarj
LQ Newbie
 
Registered: May 2009
Posts: 9

Original Poster
Rep: Reputation: 0
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
 
Old 05-05-2009, 03:57 PM   #9
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Skaarj View Post
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.
 
Old 05-05-2009, 04:25 PM   #10
Skaarj
LQ Newbie
 
Registered: May 2009
Posts: 9

Original Poster
Rep: Reputation: 0
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
 
Old 05-05-2009, 05:03 PM   #11
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
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...

Last edited by dwhitney67; 05-05-2009 at 05:04 PM.
 
Old 05-05-2009, 05:13 PM   #12
Skaarj
LQ Newbie
 
Registered: May 2009
Posts: 9

Original Poster
Rep: Reputation: 0
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
 
Old 05-05-2009, 05:45 PM   #13
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
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:
	...

...

Last edited by dwhitney67; 05-05-2009 at 05:49 PM.
 
Old 05-05-2009, 05:57 PM   #14
Skaarj
LQ Newbie
 
Registered: May 2009
Posts: 9

Original Poster
Rep: Reputation: 0
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 ...
 
Old 05-05-2009, 07:54 PM   #15
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
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.

Last edited by dwhitney67; 05-05-2009 at 07:57 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
export CC=/usr/bin/gcc-4.1 sycamorex Linux - Software 4 01-17-2008 05:32 PM
Display server wont load after installing GLIB in /usr PhilK Red Hat 3 03-21-2006 05:38 AM
gcc wont install, 'failed dependencies: glibc-devel is needed by gcc-3.3.3-41' TdlSnare SUSE / openSUSE 3 11-29-2004 02:13 PM
Xawtv - Wont compile - /usr/bin/ld: cannot find -lXpm Bubba256 Linux - Software 0 10-26-2003 01:38 AM
export CC=/usr/bin/gcc-3.2 - switch gcc version? ferreter Linux - Software 1 08-20-2003 12:07 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:42 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration