繁体   English   中英

MySQL错误-程序由于错误而无法编译

[英]MySQL error - program won't compile because of error

我们被要求创建一个接受html输入的程序。 我们在mysql / mysql网络服务器上涉猎,我试图编译该程序。

  #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

您不程序链接到mysql库:尝试添加

`mysql_config --libs`

在链接器调用中。

有关更多说明,请参见https://dev.mysql.com/doc/refman/5.7/en/c-api-building-clients.html

将MySQL库链接到您的build命令。 例如:

gcc -o <&ltOutputFile>> <&ltSourceFileName>> `mysql_config --cflags --libs`

还要确保在构建/链接命令的末尾添加mysql库。 链接库的顺序很重要。

链接可能会有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM