Thread: C++ and mysql. I'm getting an error i dont understand.

  1. #1
    Unregistered
    Guest

    Angry C++ and mysql. I'm getting an error i dont understand.

    hi,
    i'm trying to interface with a mysql db from C++ and I'm getting weird compilation errors. i'm running Slackware7.0.

    this is the program:
    Code:
    #include <mysql/mysql.h>
    #include <stdio.h>
    
    #define host "127.0.0.1"
    #define username "root"
    #define password ""
    #define db "ccnums"
    
    
    
    int main()
    {
    	MYSQL *conn;
    	conn = mysql_init(NULL);
    	mysql_real_connect(conn, host, username, password, db, 0, NULL, 0);
    	
    	MYSQL_RES *res_set;
    	MYSQL_ROW row;
    	unsigned int i;
    	
    	mysql_query(conn, "select * from nums");
    	res_set = mysql_store_result(conn);
    	
    	unsigned int numrows = mysql_num_rows(res_set);
    	while((row = mysql_fetch_row(res_set)) != NULL) 
    	{
    		for (i=0; i<mysql_num_fields(res_set); i++)
    			printf("%s\n", row[i] != NULL ? row[i] : "NULL");
    	}
    	
    	mysql_close(conn);
    	return 0;
    }
    and the errors:
    Code:
    /tmp/ccVA60Wf.o: In function `main':
    /tmp/ccVA60Wf.o(.text+0xc): undefined reference to `mysql_init'
    /tmp/ccVA60Wf.o(.text+0x37): undefined reference to `mysql_real_connect'
    /tmp/ccVA60Wf.o(.text+0x4b): undefined reference to `mysql_query'
    /tmp/ccVA60Wf.o(.text+0x5a): undefined reference to `mysql_store_result'
    /tmp/ccVA60Wf.o(.text+0x6e): undefined reference to `mysql_num_rows'
    /tmp/ccVA60Wf.o(.text+0x88): undefined reference to `mysql_fetch_row'
    /tmp/ccVA60Wf.o(.text+0xaf): undefined reference to `mysql_num_fields'
    /tmp/ccVA60Wf.o(.text+0x10d): undefined reference to `mysql_close'
    collect2: ld returned 1 exit status
    i'd appreciate it if someone would clear this up for me.

    Martin

  2. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    6
    I'm using Mandrake 8.2 and get the same message!
    Very frustrating.
    I also looking for some help about this
    Anyone?

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    6
    I belived I have found the problem.
    Ibelived that there was a path error I have wrote!
    The command will look like this:
    $ gcc -I/usr/local/mysql/include/mysql myprogram.c -L/usr/local/mysql/lib/libmysqlclient -o connect1
    I still get the following error:


    /tmp/ccm4jLAL.o: In function `main':
    /tmp/ccm4jLAL.o(.text+0xc): undefined reference to `mysql_init'
    /tmp/ccm4jLAL.o(.text+0x5e): undefined reference to `mysql_real_connect'
    /tmp/ccm4jLAL.o(.text+0x99): undefined reference to `mysql_close'
    collect2: ld returned 1 exit status
    Last edited by JanP; 10-06-2002 at 06:30 AM.

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    6
    I tried this:
    $ gcc main.c -l/usr/local/mysql/include/mysql -L/usr/local/mysql/lib/libmysqlclient -o connect1
    and I get this:
    /usr/bin/ld: cannot find -l/usr/local/mysql/include/mysql
    collect2: ld returned 1 exit status
    So exactly what do I have to do?

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    6
    So, if I understand You the command will look like this:
    gcc main.c -I/usr/local/mysql/include/mysql -L/usr/local/mysql/lib/ -llibmysqlclient -o connect
    I get the result:
    /usr/bin/ld: cannot find -llibmysqlclient
    collect2: ld returned 1 exit status
    The directory
    /usr/local/mysql/lib
    The file/lib
    -rw-rw-r-- 1 root mysql 280506 feb 25 2002 libmysqlclient.a
    Last edited by JanP; 10-06-2002 at 07:38 AM.

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    6

    Thanks Salem. I'm very thanks full.
    I'm not sure that the new error that apears
    is in this case, but anyway here they are:
    /usr/local/mysql/lib//libmysqlclient.a(my_compress.o): In function `my_uncompress':
    my_compress.o(.text+0xa2): undefined reference to `uncompress'
    /usr/local/mysql/lib//libmysqlclient.a(my_compress.o): In function `my_compress_alloc':
    my_compress.o(.text+0x13a): undefined reference to `compress'
    collect2: ld returned 1 exit status
    Any suggestions?

  7. #7
    Registered User
    Join Date
    Oct 2002
    Posts
    6
    Hi again
    I have to tell that I get a solution for the last problem.
    I have to add -lz to the command.
    That has the gcc locking in the right library for the 'compress'

    Now I'm happy again.

Popular pages Recent additions subscribe to a feed