Thread: getting a (.text+0x18): undefined reference to 'main'

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    6

    getting a (.text+0x18): undefined reference to 'main'

    i'm getting the above error when i try to compile this simple program that's attempting to test out embedded mysql:

    Code:
    #include <mysql/mysql.h>
    #include <stdio.h>
    
    int main(void)
    {
    	MYSQL msyql;
    	MYSQL_ROW row;
    	MYSQL_RES *result;
    
    	unsigned int num_fields;
    	unsigned int i;
    
    	mysql_init(&mysql);
    
    	if(!mysql_real_connect(&mysql,"192.168.1.100","root","password","db",o,NULL,o))
    	{
    		fprintf(stderr,"Failed to connect to database: Error: %s\n",
    			mysql_error(&mysql));
    
    	}
    	else
    	{
    		if(mysql_query(&mysql,"SELECT * FROM table"));
    //		error
    		else
    		{
    			result = mysql_store_result(&mysql);
    			num_fields = mysql_num_fields(result);
    			while(row = mysql_fetch_row(result))
    			{
    				unsigned long *lengths;
    				lengths = mysql_fetch_lengths(result);
    				for(i=0; i < num_fields; i++)
    				{
    					printf("[%.*s]\t",(int)lengths[i],row[i] ? row[i] : "NULL");
    				}
    				printf("\n");
    			}
    		}
    	}
    	return 0;
    }
    i'm using the following command to compile:

    gcc -filename.c -g -o filename -L /usr/lib/mysql -lmysqlclient


    any ideas? :S
    Last edited by jam; 04-22-2009 at 01:23 PM.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    gcc -filename.c -g -o filename -L /usr/lib/mysql -lmysqlclient

    Not familiar with gcc or compiling with the command line but that first '-' looks suspicious.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    53
    Code:
    gcc  -o [executable] ./[filename.c]  -lmysql
    this will be closer. i dont know how to link the mysgl library but it must say so somewhere in the documentation. as example if the math library should be included the you use the -lm switch just like that to link the math library. the commandline arguments giving in this case is most likely the problem.

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    6
    durrrr i'm stupid, i had the dash there and it shouldn't have been. thanks for pointing it out!

    jam

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    6
    ok, more problems

    this line is now giving me a seg fault:
    Code:
    result = mysql_store_result(&mysql);
    and i'm not sure why!

    any ideas?

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    6
    mmk, it seems that the reason i was getting a seg fault is that that line isn't working (&mysql isn't recieving the data from the DB) so when i try and do:
    Code:
    num_fields = mysql_num_fields(result);
    there's nothing there and it seg faults. i've replaced it with:
    Code:
    if(!(result = mysql_store_result(&mysql)))
    {
    	printf("error\n");
    }
    and it spits out the error instead, but i'm not sure why the command isn't working... the table exists and has data in it, and the code is connecting to the database ok...

    i can't see anything wrong with my syntax....

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    6
    ok now it seems my select command is failing O_O

    this is driving me nuts. can anyone shed any light? i'm going in circles

  8. #8
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Print out the error code it is returning. Then look at the documentation to see which of the 4 errors you got.

  9. #9
    Registered User
    Join Date
    Apr 2009
    Posts
    6
    i've sorted it now... it worked with `table` not table :P

    thanks for the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. undefined reference error while linking..help
    By rapture in forum C++ Programming
    Replies: 4
    Last Post: 12-04-2007, 04:54 AM
  3. Textbox
    By maxorator in forum Windows Programming
    Replies: 20
    Last Post: 09-25-2005, 10:04 AM
  4. How to: Use OpenGL with Jgrasp
    By Pickels in forum Game Programming
    Replies: 3
    Last Post: 08-30-2005, 10:37 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM