View Full Version : [ C++ and MySQL ] How to compile ?
The Secret
January 2nd, 2010, 08:35 PM
#include <iostream>
#include <mysql.h>
using namespace std;
int main(int argc, char *argv[])
{
MySQL *Connection;
Connection = mysql_init(NULL);
mysql_real_connect(Connection, "localhost", "root", "password", "test", 0, NULL, 0);
return 0;
}
g++ main.cpp -o main -L/usr/include/mysql-lmysqlclient -I/usr/include/mysql
main.cpp: In function ‘int main(int, char**)’:
main.cpp:8: error: ‘MySQL’ was not declared in this scope
main.cpp:8: error: ‘Connection’ was not declared in this scope
What I'm doing wrong ?
** Don't blame me for the source code and/or commands I've used - I'm learning and all this stuff comes from a tutorial.
Arndt
January 2nd, 2010, 09:55 PM
#include <iostream>
#include <mysql.h>
using namespace std;
int main(int argc, char *argv[])
{
MySQL *Connection;
Connection = mysql_init(NULL);
mysql_real_connect(Connection, "localhost", "root", "password", "test", 0, NULL, 0);
return 0;
}
g++ main.cpp -o main -L/usr/include/mysql-lmysqlclient -I/usr/include/mysql
main.cpp: In function ‘int main(int, char**)’:
main.cpp:8: error: ‘MySQL’ was not declared in this scope
main.cpp:8: error: ‘Connection’ was not declared in this scope
What I'm doing wrong ?
** Don't blame me for the source code and/or commands I've used - I'm learning and all this stuff comes from a tutorial.
I looked into mysql.h and found a struct there, typedefed as "MYSQL". I guess that's what you want, not "MySQL".
The Secret
January 2nd, 2010, 10:13 PM
/tmp/ccG0CdLt.o: In function `main':
main.cpp:(.text+0x11): undefined reference to `mysql_init'
main.cpp:(.text+0x59): undefined reference to `mysql_real_connect'
collect2: ld returned 1 exit status
Something is still wrong.
Arndt
January 2nd, 2010, 11:45 PM
/tmp/ccG0CdLt.o: In function `main':
main.cpp:(.text+0x11): undefined reference to `mysql_init'
main.cpp:(.text+0x59): undefined reference to `mysql_real_connect'
collect2: ld returned 1 exit status
Something is still wrong.
Does the tutorial really contain that compilation/linking command? It specifies where to find the mysql library (but strangely so - libraries are not in /usr/include), but then doesn't actually link with it.
caelestis2
January 3rd, 2010, 05:05 AM
Since you are using C++ I would suggest using
http://tangentsoft.net/mysql++/
Life would be easier for you.
dwhitney67
January 3rd, 2010, 05:07 AM
Try:
g++ -I/usr/include/mysql main.cpp -o main -L/usr/lib/mysql -lmysqlclient
P.S. Btw, since you are doing C++ development, you should really consider using the MySQL++ library, not the regular/wimpy MySQL C library.
sudo apt-get install libmysql++-dev
The Secret
January 3rd, 2010, 08:54 AM
Try:
g++ -I/usr/include/mysql main.cpp -o main -L/usr/lib/mysql -lmysqlclient
P.S. Btw, since you are doing C++ development, you should really consider using the MySQL++ library, not the regular/wimpy MySQL C library.
sudo apt-get install libmysql++-dev
Still the same error and the mysql++ dev package is already installed.
Arndt
January 3rd, 2010, 11:49 AM
Still the same error and the mysql++ dev package is already installed.
I don't know why there is a difference, but I can link the program without errors.
LKjell
January 3rd, 2010, 01:02 PM
If you use
#include <mysql/mysql.h> Then you do not need the long path include. But you still need the library linking.
dwhitney67
January 3rd, 2010, 02:05 PM
Still the same error
Have you checked to verify that you have a 'mysqlclient' library installed on your system? Check the results of this command:
ls -l /usr/lib/mysql/libmysqlclient*
Do you perhaps need mysqld or perhaps libndbclient?
... and the mysql++ dev package is already installed.
But you are not using it!
The Secret
January 4th, 2010, 12:46 PM
If you use
#include <mysql/mysql.h> Then you do not need the long path include. But you still need the library linking.
Thank you.
Have you checked to verify that you have a 'mysqlclient' library installed on your system? Check the results of this command:
ls -l /usr/lib/mysql/libmysqlclient*
Do you perhaps need mysqld or perhaps libndbclient?
If I would know what I need, this thread wouldn't be here :D Basically, I figured out that I don't have the mysqlclient libraries, tough after installing them, I receive the same error as without them.
But you are not using it!
Because I don't know how ( if this isn't the way it should be used ).
Arndt
January 4th, 2010, 12:48 PM
If I would know what I need, this thread wouldn't be here :D Basically, I figured out that I don't have the mysqlclient libraries, tough after installing them, I receive the same error as without them.
So what was the result of this?
ls -l /usr/lib/mysql/libmysqlclient*
The Secret
January 4th, 2010, 01:09 PM
So what was the result of this?
ls -l /usr/lib/mysql/libmysqlclient*
ls: cannot access /usr/lib/mysql/libmysqlclient*: No such file or directory
i libmysqlclient-dev - MySQL database development files
c libmysqlclient15-dev - MySQL database development files
i A libmysqlclient15off - MySQL database client library
i libmysqlclient16 - MySQL database client library
p libmysqlclient16-dev - MySQL database development files - empty t
libmysqld-dev and libmysql++-dev are installed, too.
dwhitney67
January 4th, 2010, 01:32 PM
Because I don't know how ( if this isn't the way it should be used ).
This is the golden-question you should have asked from the start.
In your opening post, it appears as if you a merely attempting to establish a connection to the database. Here's a simple example, using your parameters, and the MySQL++ interface:
#include <mysql++.h>
#include <iostream>
int main()
{
using namespace mysqlpp;
using namespace std;
try
{
Connection conn("test", "localhost", "root", "password");
}
catch (ConnectionFailed& e)
{
cerr << "Exception: " << e.what() << endl;
}
}
To compile/link this code:
g++ -Wall -pedantic -Wno-long-long -O2 `mysql_config --include` -I/usr/include/mysql++ -c sql++.cpp
g++ sql++.o `mysql_config --libs` -lmysqlpp -o sql++_test
Naturally, the example above is not very OO-ish. Also, repeatedly typing in the build commands is a pain; a Makefile would be much desired.
Arndt
January 4th, 2010, 01:32 PM
ls: cannot access /usr/lib/mysql/libmysqlclient*: No such file or directory
i libmysqlclient-dev - MySQL database development files
c libmysqlclient15-dev - MySQL database development files
i A libmysqlclient15off - MySQL database client library
i libmysqlclient16 - MySQL database client library
p libmysqlclient16-dev - MySQL database development files - empty t
libmysqld-dev and libmysql++-dev are installed, too.
I don't know what those letters mean. But mine is in /usr/lib, actually. In /usr/lib/mysql, I have libmysqld.a, among other things.
dwhitney67
January 4th, 2010, 03:31 PM
I don't know what those letters mean. But mine is in /usr/lib, actually. In /usr/lib/mysql, I have libmysqld.a, among other things.
I totally forgot the other day (yesterday?) that the 'mysql_config' utility can be used to obtain the proper CFLAGS and LDFLAGS settings for the mysql library.
I suppose the OP should/could have used a g++ statement as:
g++ main.cpp -o main `mysql_config --include --libs`
Powered by vBulletin® Version 4.2.2 Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.