Today, in Ubuntu, when I use eclipse to connect to the MySQL database, the mysql_init () undefined error always occurs. Later I found that I needed to add a static Link Library to the project. The following describes how to connect eclipse + CDT to MySQL using C ++.
First, install mysql. Enter sudo apt-getinstall mysql-server mysql-client in the terminal (or install it in the Ubuntu Software Center ). At the same time, you also need to install the Development Kit (c api does not appear in Ubuntu as in windows, and you need to continue to install the API). Install the Development Kit command:
Sudo apt-Get install libmysqlclient15-dev
2. Install eclipse and configure eclipse for the C/C ++ development environment. See http://blog.csdn.net/linrulei11/article/details/7298093
Third, after eclipse + CDT is built, you can create a C ++ project and enter the following testCode.
# Include <MySQL/MySQL. h>
# Include <stdio. h>
Int main (){
MySQL * conn;
Mysql_res * res;
Mysql_row row;
Char * Server = "localhost"; // database address
Char * user = "root"; // database username
Char * Password = "KK"; // Database Password
Char * database = "MySQL"; // database name used
Conn = mysql_init (null );
If (! Mysql_real_connect (Conn, server,
User, password, database, 0, null, 0 )){
Fprintf (stderr, "% s \ n", mysql_error (conn ));
Return-1;
}
If (mysql_query (Conn, "show tables ")){
Fprintf (stderr, "% s \ n", mysql_error (conn ));
Return-1;
}
Res = mysql_use_result (conn );
Printf ("MySQL tables in MySQL database: \ n ");
While (ROW = mysql_fetch_row (RES ))! = NULL)
Printf ("% s \ n", row [0]);
Mysql_free_result (RES );
Mysql_close (conn );
Return 0;
}
The project structure is as follows:
After Ctrl + B builds the project, nine errors will occur. All interface functions that cannot recognize MySQL. At this time, add "Project-> properties-> C/C ++ build-> Settings-> gcc C linker-> libraries" in eclipse to mysqlclient. Build again and there will be no errors. After running, the result is also output.