当前位置: 技术问答>linux和unix
安装好MySQL后没有/usr/local/mysql 目录
来源: 互联网 发布时间:2016-02-09
本文导语: 源程序如下: #include #include #include #include "sql.h" #include "sqlext.h" void check_return (RETCODE rc, HENV henv, HDBC hdbc, HSTMT hstmt ) { UCHAR state_str [SQL_MAX_M...
源程序如下:
#include
#include
#include
#include "sql.h"
#include "sqlext.h"
void
check_return (RETCODE rc,
HENV henv,
HDBC hdbc,
HSTMT hstmt )
{
UCHAR state_str [SQL_MAX_MESSAGE_LENGTH];
SDWORD native_error;
UCHAR error_msg [SQL_MAX_MESSAGE_LENGTH];
SWORD error_msg_avail = SQL_MAX_MESSAGE_LENGTH - 1;
SWORD error_msg_len;
RETCODE local_rc;
if (rc != SQL_ERROR && rc != SQL_SUCCESS_WITH_INFO )
{
return;
}
local_rc = SQL_Error (henv,hdbc,hstmt,state_str,&native_error,error_msg,error_msg_avail,&error_msg_len);
if (local_rc != SQL_SUCCESS && rc !=SQL_SUCCESS_WITH_INFO)
{
fprintf(stderr,"Uninterpretable error; exiting n");
exit (EXIT_FAILURE);
}
if (rc == SQL_SUCCESS_WITH_INFO)
{
fprintf(stderr,"Fatal Error %s: %sn", state_str,error_msg);
return;
}
fprintf(stderr,"Fatal Error %s: %sn",state_str,error_msg);
exit (EXIT_FAILURE);
}
/*----------------------------------------------------------------------*/
int main (void)
{
HENV henv = SQL_NULL_HENV;
HDBC hdbc = SQL_NULL_HDBC;
HSTMT hstmt = SQL_NULL_HSTMT;
RETCODE rc;
char buf[257];
short buflen;
printf ("Initialize the environment structure.n");
SQLAllocEnv (&henv);
printf ("Initialize the connection structure.n");
SQLAllocConnect (henv,&hdbc);
printf("Load the ODBC driver.n");
rc = SQLDriverConnect (hdbc,0,"DSN=baseball;UID=myloginWD=mypassword",SQL_NTS,(UCHAR*) buf,sizeof (buf),&buflen,SQL_DRIVER_COMPLETE);
check_return (rc,henv,hdbc,hstmt);
printf ("Initialize the statement structure.n");
SQLAllocStmt (hdbc,&hstmt);
/* now do something*/
printf ("Creat table table "foo".n");
rc = SQLExecdirect (hstmt,"CREATE TABLE foo (bar INTEGER)", SQL_NTS);
check_return (rc, henv, hdbc, hstmt);
printf ("Insert values into table "foo".n");
rc = SQLExecdirect (hstmt,"INSERT INTO foo(bar) VALUES (1)", SQL_NTS);
check_return (rc, henv, hdbc, hstmt);
rc = SQLExecdirect (hstmt,"INSERT INTO foo(bar) VALUES (2)", SQL_NTS);
check_return (rc, henv, hdbc, hstmt);
rc = SQLExecdirect (hstmt,"INSERT INTO foo(bar) VALUES (3)", SQL_NTS);
check_return (rc, henv, hdbc, hstmt);
printf ("Drop table "foo".n");
rc = SQLExecDirect (hstmt, "DROP TABLE foo", SQL_NTS);
check_return (rc,henv,hdbc,hstmt);
/* We're done:free resources and exit*/
printf ("Free the statement handle.n");
SQLFreeStmt (hstmt,SQL_DROP);
printf ("Disconnect from the data source.n");
SQLDisconnect (hdbc);
printf ("Free the connection structure.n");
SQLFreeConnect (hdbc);
printf ("Free the environment structure.n");
SQLFreeEnv (henv);
printf ("Goodbye!n");
exit (EXIT_SUCCESS);
}
我的程序编译、连接的结果是:
[root@localhost database_experiment]# gcc odbc.c
/tmp/ccldHxyK.o(.text+0x5a): In function `check_return':
: undefined reference to `SQLError'
/tmp/ccldHxyK.o(.text+0x130): In function `main':
: undefined reference to `SQLAllocEnv'
/tmp/ccldHxyK.o(.text+0x152): In function `main':
: undefined reference to `SQLAllocConnect'
/tmp/ccldHxyK.o(.text+0x18b): In function `main':
: undefined reference to `SQLDriverConnect'
/tmp/ccldHxyK.o(.text+0x1c7): In function `main':
: undefined reference to `SQLAllocStmt'
/tmp/ccldHxyK.o(.text+0x1ec): In function `main':
: undefined reference to `SQLExecdirect'
/tmp/ccldHxyK.o(.text+0x22b): In function `main':
: undefined reference to `SQLExecdirect'
/tmp/ccldHxyK.o(.text+0x25a): In function `main':
: undefined reference to `SQLExecdirect'
/tmp/ccldHxyK.o(.text+0x289): In function `main':
: undefined reference to `SQLExecdirect'
/tmp/ccldHxyK.o(.text+0x2c8): In function `main':
: undefined reference to `SQLExecDirect'
/tmp/ccldHxyK.o(.text+0x302): In function `main':
: undefined reference to `SQLFreeStmt'
/tmp/ccldHxyK.o(.text+0x320): In function `main':
: undefined reference to `SQLDisconnect'
/tmp/ccldHxyK.o(.text+0x33e): In function `main':
: undefined reference to `SQLFreeConnect'
/tmp/ccldHxyK.o(.text+0x35c): In function `main':
: undefined reference to `SQLFreeEnv'
collect2: ld returned 1 exit status
网上说要执行
ln -s /usr/local/mysql/lib /usr/lib/mysql
ln -s /usr/local/mysql/include /usr/include/mysql
然后执行
gcc -g connect_db.c -L/usr/lib/mysql -lmysqlclient -lz
但是我发现没有/usr/local/mysql/lib 这个目录,是什么原因,应该怎么办?
#include
#include
#include
#include "sql.h"
#include "sqlext.h"
void
check_return (RETCODE rc,
HENV henv,
HDBC hdbc,
HSTMT hstmt )
{
UCHAR state_str [SQL_MAX_MESSAGE_LENGTH];
SDWORD native_error;
UCHAR error_msg [SQL_MAX_MESSAGE_LENGTH];
SWORD error_msg_avail = SQL_MAX_MESSAGE_LENGTH - 1;
SWORD error_msg_len;
RETCODE local_rc;
if (rc != SQL_ERROR && rc != SQL_SUCCESS_WITH_INFO )
{
return;
}
local_rc = SQL_Error (henv,hdbc,hstmt,state_str,&native_error,error_msg,error_msg_avail,&error_msg_len);
if (local_rc != SQL_SUCCESS && rc !=SQL_SUCCESS_WITH_INFO)
{
fprintf(stderr,"Uninterpretable error; exiting n");
exit (EXIT_FAILURE);
}
if (rc == SQL_SUCCESS_WITH_INFO)
{
fprintf(stderr,"Fatal Error %s: %sn", state_str,error_msg);
return;
}
fprintf(stderr,"Fatal Error %s: %sn",state_str,error_msg);
exit (EXIT_FAILURE);
}
/*----------------------------------------------------------------------*/
int main (void)
{
HENV henv = SQL_NULL_HENV;
HDBC hdbc = SQL_NULL_HDBC;
HSTMT hstmt = SQL_NULL_HSTMT;
RETCODE rc;
char buf[257];
short buflen;
printf ("Initialize the environment structure.n");
SQLAllocEnv (&henv);
printf ("Initialize the connection structure.n");
SQLAllocConnect (henv,&hdbc);
printf("Load the ODBC driver.n");
rc = SQLDriverConnect (hdbc,0,"DSN=baseball;UID=myloginWD=mypassword",SQL_NTS,(UCHAR*) buf,sizeof (buf),&buflen,SQL_DRIVER_COMPLETE);
check_return (rc,henv,hdbc,hstmt);
printf ("Initialize the statement structure.n");
SQLAllocStmt (hdbc,&hstmt);
/* now do something*/
printf ("Creat table table "foo".n");
rc = SQLExecdirect (hstmt,"CREATE TABLE foo (bar INTEGER)", SQL_NTS);
check_return (rc, henv, hdbc, hstmt);
printf ("Insert values into table "foo".n");
rc = SQLExecdirect (hstmt,"INSERT INTO foo(bar) VALUES (1)", SQL_NTS);
check_return (rc, henv, hdbc, hstmt);
rc = SQLExecdirect (hstmt,"INSERT INTO foo(bar) VALUES (2)", SQL_NTS);
check_return (rc, henv, hdbc, hstmt);
rc = SQLExecdirect (hstmt,"INSERT INTO foo(bar) VALUES (3)", SQL_NTS);
check_return (rc, henv, hdbc, hstmt);
printf ("Drop table "foo".n");
rc = SQLExecDirect (hstmt, "DROP TABLE foo", SQL_NTS);
check_return (rc,henv,hdbc,hstmt);
/* We're done:free resources and exit*/
printf ("Free the statement handle.n");
SQLFreeStmt (hstmt,SQL_DROP);
printf ("Disconnect from the data source.n");
SQLDisconnect (hdbc);
printf ("Free the connection structure.n");
SQLFreeConnect (hdbc);
printf ("Free the environment structure.n");
SQLFreeEnv (henv);
printf ("Goodbye!n");
exit (EXIT_SUCCESS);
}
我的程序编译、连接的结果是:
[root@localhost database_experiment]# gcc odbc.c
/tmp/ccldHxyK.o(.text+0x5a): In function `check_return':
: undefined reference to `SQLError'
/tmp/ccldHxyK.o(.text+0x130): In function `main':
: undefined reference to `SQLAllocEnv'
/tmp/ccldHxyK.o(.text+0x152): In function `main':
: undefined reference to `SQLAllocConnect'
/tmp/ccldHxyK.o(.text+0x18b): In function `main':
: undefined reference to `SQLDriverConnect'
/tmp/ccldHxyK.o(.text+0x1c7): In function `main':
: undefined reference to `SQLAllocStmt'
/tmp/ccldHxyK.o(.text+0x1ec): In function `main':
: undefined reference to `SQLExecdirect'
/tmp/ccldHxyK.o(.text+0x22b): In function `main':
: undefined reference to `SQLExecdirect'
/tmp/ccldHxyK.o(.text+0x25a): In function `main':
: undefined reference to `SQLExecdirect'
/tmp/ccldHxyK.o(.text+0x289): In function `main':
: undefined reference to `SQLExecdirect'
/tmp/ccldHxyK.o(.text+0x2c8): In function `main':
: undefined reference to `SQLExecDirect'
/tmp/ccldHxyK.o(.text+0x302): In function `main':
: undefined reference to `SQLFreeStmt'
/tmp/ccldHxyK.o(.text+0x320): In function `main':
: undefined reference to `SQLDisconnect'
/tmp/ccldHxyK.o(.text+0x33e): In function `main':
: undefined reference to `SQLFreeConnect'
/tmp/ccldHxyK.o(.text+0x35c): In function `main':
: undefined reference to `SQLFreeEnv'
collect2: ld returned 1 exit status
网上说要执行
ln -s /usr/local/mysql/lib /usr/lib/mysql
ln -s /usr/local/mysql/include /usr/include/mysql
然后执行
gcc -g connect_db.c -L/usr/lib/mysql -lmysqlclient -lz
但是我发现没有/usr/local/mysql/lib 这个目录,是什么原因,应该怎么办?
|
你的mysql是不是装到/usr/share下面去了?
|
一般都是再/usr/local/include/mysql
|
其实你安装的时候可以指定相应的安装目录
./congfigure --prefix =/usr/local/mysql
不过也没关系
你在编译的时候把库和头文件指定到你安装目录当中就可以了!
gcc -g connect_db.c-I/usr/include/mysql -L/usr/lib/mysql -lmysqlclient -lz -lm
./congfigure --prefix =/usr/local/mysql
不过也没关系
你在编译的时候把库和头文件指定到你安装目录当中就可以了!
gcc -g connect_db.c-I/usr/include/mysql -L/usr/lib/mysql -lmysqlclient -lz -lm