LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 11-16-2011, 03:51 PM   #1
james_b
LQ Newbie
 
Registered: Nov 2011
Posts: 8

Rep: Reputation: Disabled
Strange g++ behaviour?


Hiya,

I'm new to development on Linux/g++. Could anyone explain why this compiles:

$ g++ test.cpp -o test.o /usr/lib64/mysql/libmysqlclient.a

But this doesn't:

$ g++ -L/usr/lib64/mysql -lmysqlclient test.cpp -o test.o
/tmp/ccqgTNhu.o: In function `main':
/root/test.cpp:10: undefined reference to `mysql_init'
collect2: ld returned 1 exit status

And neither does this?

$ g++ -L/usr/lib64/mysql libmysqlclient.a test.cpp -o test.o
g++: libmysqlclient.a: No such file or directory

I'd guess the last one fails because g++ is looking for the .a in the current directory. I must be misunderstanding how linking on the command-line works, but I can't figure out what's wrong.

Strangely, I'm apparently supposed to use '-L/usr/lib64 -lmysqlclient', but there is something strange with the file:

$ cd /usr/lib64
$ ls *mysql*
libmysqlclient.a
$ file libmysqlclient.a
libmysqlclient.a: cannot open `libmysqlclient.a' (No such file or directory)

Very confusing. Any suggestions are much appreciated!

Cheers
 
Old 11-16-2011, 04:13 PM   #2
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,139

Rep: Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127
Quote:
Originally Posted by james_b View Post
But this doesn't:

$ g++ -L/usr/lib64/mysql -lmysqlclient test.cpp -o test.o
test.cpp must come earlier on the line than -lmysqlclient

Also, your output selection of test.o is confusing. You are asking for a complete executable, which is not normally given the extension .o

Quote:
And neither does this?

$ g++ -L/usr/lib64/mysql libmysqlclient.a test.cpp -o test.o
g++: libmysqlclient.a: No such file or directory

I'd guess the last one fails because g++ is looking for the .a in the current directory.
Yes, but even with a correct path the .a file must come later on the line than the file that creates the need for that .a file (which is your .cpp file).

Quote:
Originally Posted by james_b View Post
$ cd /usr/lib64
$ ls *mysql*
libmysqlclient.a
$ file libmysqlclient.a
libmysqlclient.a: cannot open `libmysqlclient.a' (No such file or directory)

Very confusing.
I can't explain that one. But since your original command worked, I think the file must be OK.
Quote:
this compiles:

$ g++ test.cpp -o test.o /usr/lib64/mysql/libmysqlclient.a

Last edited by johnsfine; 11-16-2011 at 04:49 PM.
 
Old 11-16-2011, 04:28 PM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,925

Rep: Reputation: 524Reputation: 524Reputation: 524Reputation: 524Reputation: 524Reputation: 524
Code:
$ cd /usr/lib64
$ ls -l *mysql*
 
Old 11-16-2011, 05:57 PM   #4
james_b
LQ Newbie
 
Registered: Nov 2011
Posts: 8

Original Poster
Rep: Reputation: Disabled
Thanks! Putting the link options after the source file does indeed work.

I'm a little unsure of why using -L/usr/lib64 doesn't work.

Code:
$ cd /usr/lib64
$ ls -l *mysql*
total 22456
-rw-r--r--. 1 root root  3020142 Oct 12 13:16 libmygcc.a
-rw-r--r--. 1 root root 16785382 Oct 12 13:15 libmysqlclient.a
lrwxrwxrwx. 1 root root       16 Nov 16 20:00 libmysqlclient_r.a -> libmysqlclient.a
lrwxrwxrwx. 1 root root       26 Nov 16 18:41 libmysqlclient_r.so.16 -> libmysqlclient_r.so.16.0.0
-rwxr-xr-x. 1 root root  1588520 Jan 27  2011 libmysqlclient_r.so.16.0.0
lrwxrwxrwx. 1 root root       24 Nov 16 18:41 libmysqlclient.so.16 -> libmysqlclient.so.16.0.0
-rwxr-xr-x. 1 root root  1577896 Jan 27  2011 libmysqlclient.so.16.0.0
-rw-r--r--. 1 root root     9904 Oct 12 13:14 libmysqlservices.a
drwxr-xr-x. 3 root root     4096 Nov 16 19:27 plugin

$ cd mysql
$ ls -l
total 22456
-rw-r--r--. 1 root root  3020142 Oct 12 13:16 libmygcc.a
-rw-r--r--. 1 root root 16785382 Oct 12 13:15 libmysqlclient.a
lrwxrwxrwx. 1 root root       16 Nov 16 20:00 libmysqlclient_r.a -> libmysqlclient.a
lrwxrwxrwx. 1 root root       26 Nov 16 18:41 libmysqlclient_r.so.16 -> libmysqlclient_r.so.16.0.0
-rwxr-xr-x. 1 root root  1588520 Jan 27  2011 libmysqlclient_r.so.16.0.0
lrwxrwxrwx. 1 root root       24 Nov 16 18:41 libmysqlclient.so.16 -> libmysqlclient.so.16.0.0
-rwxr-xr-x. 1 root root  1577896 Jan 27  2011 libmysqlclient.so.16.0.0
-rw-r--r--. 1 root root     9904 Oct 12 13:14 libmysqlservices.a
drwxr-xr-x. 3 root root     4096 Nov 16 19:27 plugin
Thanks again
 
Old 11-16-2011, 06:22 PM   #5
james_b
LQ Newbie
 
Registered: Nov 2011
Posts: 8

Original Poster
Rep: Reputation: Disabled
I've managed to get it to link properly in the shell and Netbeans.

For some reason, it's OK to have -lpthread, -lm and -ldl before the source file - but -lmysqlclient has to come after? Is this to do with the way they are linked? Statically or dynamically maybe?

Also, libmysqlclient.a does not exist in /usr/lib64, only in /usr/lib64/mysql. I have no idea why ls -l reports it as being in /usr/lib64..

Please let me know if you've seen this before! It's almost like ls -l is looking in subdirectories too.

Thanks.

Last edited by james_b; 11-16-2011 at 06:25 PM.
 
Old 11-16-2011, 08:11 PM   #6
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by james_b View Post
I've managed to get it to link properly in the shell and Netbeans.

For some reason, it's OK to have -lpthread, -lm and -ldl before the source file - but -lmysqlclient has to come after? Is this to do with the way they are linked? Statically or dynamically maybe?

Also, libmysqlclient.a does not exist in /usr/lib64, only in /usr/lib64/mysql. I have no idea why ls -l reports it as being in /usr/lib64..

Please let me know if you've seen this before! It's almost like ls -l is looking in subdirectories too.

Thanks.
You probably realize that "under the hood" g++ uses ld. I suggest to read http://linux.die.net/man/1/ld - paying attention to '--verbose'. You have to also learn how from g++ you can pass arguments directly to ld - gcc documentation has a special chapter on this.
 
Old 11-17-2011, 03:48 AM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,925

Rep: Reputation: 524Reputation: 524Reputation: 524Reputation: 524Reputation: 524Reputation: 524
Then it is (anywhere in commandline): -L/usr/lib64/mysql -lmysqlclient

PS: in retrospect, ls should have been: ls -ld /usr/lib64/*mysql*
 
Old 11-17-2011, 09:03 AM   #8
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,139

Rep: Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127
Quote:
Originally Posted by james_b View Post
Also, libmysqlclient.a does not exist in /usr/lib64, only in /usr/lib64/mysql. I have no idea why ls -l reports it as being in /usr/lib64..
That's easy now that you provided correct info.

You used the command
ls -l *mysql*

The shell expands the *mysql* into all the names matching that spec in /usr/lib64. It does that before passing anything to the ls command.

The only name matched is mysql, which is a directory, so ls lists the contents of that directory.

For comparison try the command
ls -ld *mysql*
That will list all the files or directories matched by *mysql*
ls -l *mysql* lists files matched by *mysql* plus the contents of directories matched by *mysql*

Last edited by johnsfine; 11-17-2011 at 09:08 AM.
 
1 members found this post helpful.
Old 11-17-2011, 09:22 AM   #9
james_b
LQ Newbie
 
Registered: Nov 2011
Posts: 8

Original Poster
Rep: Reputation: Disabled
Ah, I see now.. I just misunderstood what it was telling me.

That's great! Thanks!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
ps -ef strange behaviour ?? lionking_x Linux - Newbie 1 11-28-2007 06:22 PM
strange behaviour marsques Slackware 11 02-15-2006 07:05 PM
Strange Behaviour! joshuarowley LQ Suggestions & Feedback 1 12-08-2005 04:36 PM
Strange behaviour Anmol Suse/Novell 2 10-28-2005 12:05 AM
Strange Behaviour mikeyt_3333 Linux - General 4 08-06-2001 04:07 PM


All times are GMT -5. The time now is 11:51 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration