链接ODBC的链接库时出问题
我的代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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: %s\n ", state_str,error_msg);
return;
}
fprintf(stderr, "Fatal Error %s: %s\n ",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=pdmtest;UID=nate;PWD=nate ",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@kaven pdm]# gcc vc.c -o vc -L /usr/local/mysql/lib/ -lmysqlclient -lz
然后包如下错误:
/tmp/ccQL24B7.o: In function `check_return ':
vc.c:(.text+0x76): undefined reference to `SQL_Error '
/tmp/ccQL24B7.o: In function `main ':
vc.c:(.text+0x169): undefined reference to `SQLAllocEnv '
vc.c:(.text+0x187): undefined reference to `SQLAllocConnect '
vc.c:(.text+0x1db): undefined reference to `SQLDriverConnect '
vc.c:(.text+0x21e): undefined reference to `SQLAllocStmt '
vc.c:(.text+0x245): undefined reference to `SQLExecdirect '
vc.c:(.text+0x291): undefined reference to `SQLExecdirect '
vc.c:(.text+0x2d1): undefined reference to `SQLExecdirect '
vc.c:(.text+0x311): undefined reference to `SQLExecdirect '
vc.c:(.text+0x35e): undefined reference to `SQLExecDirect '
vc.c:(.text+0x3a2): undefined reference to `SQLFreeStmt '
vc.c:(.text+0x3b9): undefined reference to `SQLDisconnect '
vc.c:(.text+0x3d0): undefined reference to `SQLFreeConnect '
vc.c:(.text+0x3e7): undefined reference to `SQLFreeEnv '
collect2: ld 返回 1
请问如何解决?
[解决办法]
库文件存在么?
[解决办法]
你写的有点复杂了,除非对这个很熟悉,或者正在做这个的,可能会调试。
我都是直接使用c编写链接mysql数据库的代码,没什么大问题啊。给你帖一段看看吧。
#include <stdio.h>
#include <sys/file.h>
#include <mysql/mysql.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <time.h>
FILE* flowlog;
MYSQL mysql;
MYSQL_RES *res;
MYSQL_ROW sqlrow;
struct in_addr ipaddr;
int main(int argc, char *argv[])
{
int i;
struct tm *tm;
unsigned long timenum;
char sqlstring[800];
sprintf(sqlstring, "select * from outlier; ");
flowlog=fopen( "/usr/local/apache2/htdocs/flow.html ", "w+ "); //open the log file
if(flowlog==NULL)
printf( "Open file failed\n ");
fprintf(flowlog, " <html> <head> <title> Netflow Security Analysis System </title> </head> <body> \n ");
fprintf(flowlog, " <center> <h1> Netflow Security Analysis System </h1> </center> ");
fflush(flowlog);
mysql_init(&mysql);
if(mysql_real_connect(&mysql, "localhost " , "lijian ", "lijian ", "netflow " ,3306,(char *)NULL,0)==NULL)
{
fprintf(flowlog, "Connetion failed\n ");
return 1;
}else
{
if(mysql_query(&mysql,sqlstring))
return 1;
res=mysql_store_result(&mysql);
if(res)
fprintf(flowlog, "%s:Retrived %lu rows <br> ",sqlstring,(unsigned long) mysql_num_rows(res));
while(sqlrow=mysql_fetch_row(res))
{
fprintf(flowlog, " %s ", sqlrow[0]);
fprintf(flowlog, " %s ", sqlrow[1]);
fflush(flowlog);
}
mysql_free_result(res);
}
fprintf(flowlog, " </table> </center> ");
fflush(flowlog);
fprintf(flowlog, " </body> </html> ");
mysql_close(&mysql);
fclose(flowlog);
return 0;
}