我们被要求创建一个接受来自html的输入的程序。我们正在讨论mysql / mysql webserver,我正在尝试编译这个程序。
#include <stdio.h>
#include <string.h>
#include <mysql/mysql.h>
int processQueryResult(MYSQL *pConn, MYSQL_RES *pRes)
{
MYSQL_ROW aRow;
unsigned int iCounter, iTotal;
aRow = mysql_fetch_row (pRes);
while (NULL != aRow)
{
/* Basic formatting for now */
iTotal = mysql_num_fields (pRes);
for (iCounter = 0; iCounter < iTotal; iCounter++)
{
/* Check if empty data first before displaying */
if (NULL != aRow[iCounter])
{
printf ("%s\t", aRow[iCounter]);
}
else
{
printf ("NULL\t");
}
}
printf ("\n");
aRow = mysql_fetch_row (pRes);
}
/* Check if no error */
if (mysql_errno (pConn) == 0)
{
printf ("%lu rows returned\n\n", (unsigned long) mysql_num_rows (pRes));
}
return 0;
}
int sendQuery(MYSQL *pConn, char *pCommand)
{
MYSQL_RES *pRes;
if (mysql_query (pConn, pCommand) != 0) /* the query failed */
{
return 1;
}
pRes = mysql_store_result (pConn);
if (NULL != pRes) /* a result set was returned */
{
processQueryResult (pConn, pRes);
mysql_free_result (pRes);
}
else
{
if (mysql_field_count (pConn) == 0)
{
/* Modified table like INSERT command */
printf ("%lu rows affected\n", (unsigned long) mysql_affected_rows(pConn));
}
else /* an error occurred */
{
return 1;
}
}
return 0;
}
int getSQLInput(char *pBuffer)
{
int iCount = 0;
int curData;
do
{
curData = fgetc(stdin);
*pBuffer = curData;
} while(*pBuffer == '\n');
do
{
curData = fgetc(stdin);
pBuffer[++iCount] = curData;
} while ((curData != '\n') && (curData != EOF));
if (pBuffer[iCount-1] != ';')
{
pBuffer[iCount++]= ';';
}
pBuffer[iCount]= '\0';
fputc('\n', stdout);
return iCount;
}
int main(int argc, char **argv)
{
MYSQL *pConn;
char aCommand[1024];
char aServer[1024];
char aUser[1024];
char aPassword[1024];
char aDatabase[1024];
printf("Enter Server: ");
scanf("%s", aServer);
printf("Enter Database: ");
scanf("%s", aDatabase);
printf("Enter user: ");
scanf("%s", aUser);
printf("Enter password: ");
scanf("%s", aPassword);
pConn = mysql_init(NULL);
/* Connect to database */
if (!mysql_real_connect(pConn, aServer, aUser, aPassword, aDatabase, 0, NULL, 0)) {
printf("Connect Error: %s\n", mysql_error(pConn));
return 1;
}
printf("SQL command > ");
getSQLInput(aCommand);
while (strcmp(aCommand, "exit;") != 0)
{
if (0 != sendQuery(pConn, aCommand))
{
printf("QUERY ERROR: %s\n", mysql_error(pConn));
}
printf("SQL command > ");
getSQLInput(aCommand);
}
mysql_close(pConn);
return 0;
}
当我尝试编译它时,它显示了这个错误列表
cabox@box-codeanywhere:~/workspace$ gcc basic.c -o basic.cgi
/tmp/cct4DDaf.o: In function `processQueryResult':
basic.c:(.text+0x18): undefined reference to `mysql_fetch_row'
basic.c:(.text+0x2d): undefined reference to `mysql_num_fields'
basic.c:(.text+0xad): undefined reference to `mysql_fetch_row'
basic.c:(.text+0xc8): undefined reference to `mysql_errno'
basic.c:(.text+0xd8): undefined reference to `mysql_num_rows'
/tmp/cct4DDaf.o: In function `sendQuery':
basic.c:(.text+0x114): undefined reference to `mysql_query'
basic.c:(.text+0x12b): undefined reference to `mysql_store_result'
basic.c:(.text+0x155): undefined reference to `mysql_free_result'
basic.c:(.text+0x163): undefined reference to `mysql_field_count'
basic.c:(.text+0x173): undefined reference to `mysql_affected_rows'
/tmp/cct4DDaf.o: In function `main':
basic.c:(.text+0x32c): undefined reference to `mysql_init'
basic.c:(.text+0x378): undefined reference to `mysql_real_connect'
basic.c:(.text+0x38c): undefined reference to `mysql_error'
/tmp/ccwRjdCw.o: In function `processQueryResult':
basic.c:(.text+0x18): undefined reference to `mysql_fetch_row'
basic.c:(.text+0x2d): undefined reference to `mysql_num_fields'
basic.c:(.text+0xad): undefined reference to `mysql_fetch_row'
basic.c:(.text+0xc8): undefined reference to `mysql_errno'
basic.c:(.text+0xd8): undefined reference to `mysql_num_rows'
/tmp/ccwRjdCw.o: In function `sendQuery':
basic.c:(.text+0x114): undefined reference to `mysql_query'
basic.c:(.text+0x12b): undefined reference to `mysql_store_result'
basic.c:(.text+0x155): undefined reference to `mysql_free_result'
basic.c:(.text+0x163): undefined reference to `mysql_field_count'
basic.c:(.text+0x173): undefined reference to `mysql_affected_rows'
/tmp/ccwRjdCw.o: In function `main':
basic.c:(.text+0x32c): undefined reference to `mysql_init'
basic.c:(.text+0x378): undefined reference to `mysql_real_connect'
basic.c:(.text+0x38c): undefined reference to `mysql_error'
basic.c:(.text+0x3f4): undefined reference to `mysql_error'
basic.c:(.text+0x44b): undefined reference to `mysql_close'
collect2: error: ld returned 1 exit status
答案 0 :(得分:3)
您没有将程序链接到mysql库:尝试添加
`mysql_config --libs`
链接器调用中的。
有关详细说明,请参阅https://dev.mysql.com/doc/refman/5.7/en/c-api-building-clients.html。
答案 1 :(得分:3)
将MySQL库链接到构建命令。例如:
gcc -o <<OutputFile>> <<SourceFileName>> `mysql_config --cflags --libs`
还要确保在build / linking命令的末尾添加mysql库。链接库的顺序很重要。
这link可能会有所帮助。