ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
$ 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!
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
$ 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.
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.
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.
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*
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.