Reputation: 39
We were asked to create a program that accepts input from an html. We were dabbling on mysql/mysql webserver and I was trying to compile this program.
#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;
}
When I try to compile it, it shows this list of errors
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
Upvotes: 1
Views: 6374
Reputation: 259
Link MySQL libraries to your build command. For example:
gcc -o <<OutputFile>> <<SourceFileName>> `mysql_config --cflags --libs`
Also make sure you add mysql library in the end of your build/linking command. Order of link library matters.
This link could be helpful.
Upvotes: 3
Reputation: 9629
You do not link your program to the mysql library: Try to add
`mysql_config --libs`
in your linker invocation.
See https://dev.mysql.com/doc/refman/5.7/en/c-api-building-clients.html for more explanations.
Upvotes: 4