mysql in c++ initialize error occurs a simple program is executed in redhat9.0 , using gcc 3.2.2 com

Discussion in 'C++' started by yogesh, Feb 15, 2007.

  1. yogesh

    yogesh Guest

    mysql in c++ initialize error occurs a simple program is executed in
    redhat9.0 , using gcc 3.2.2 compiler version ...


    #include <stdio.h>
    #include <mysql.h>
    #include <string.h>

    int main()
    {
    MYSQL* mysql;
    MYSQL_RES* res;
    MYSQL_ROW row;
    char query[80];

    mysql = mysql_init( NULL );

    if( mysql == NULL ) {

    mysql_real_connect( mysql, "localhost", "username",
    "password","dbname", 0, "/tmp/mysql.sock", 0 );

    sprintf( query, "SELECT * FROM tablename" );
    mysql_real_query( mysql, query, (unsigned
    int)strlen(query) );
    res = mysql_use_result( mysql );

    while( row = mysql_fetch_row( res ) ) {
    printf( "%s %s\n", row[0], row[1] );
    }



    mysql_free_result( res );
    mysql_close( mysql );
    }

    else {
    printf( "mysql_init returned NULL\n" );
    }
    return 0;
    }
    "n1.cpp" 34L, 656C written

    if i run the code iam getting the error as follows

    [root@localhost yog]# gcc n1.cpp
    /tmp/cccUdCdL.o(.text+0x16): In function `main':
    : undefined reference to `mysql_init'
    /tmp/cccUdCdL.o(.text+0x4b): In function `main':
    : undefined reference to `mysql_real_connect'
    /tmp/cccUdCdL.o(.text+0x81): In function `main':
    : undefined reference to `mysql_real_query'
    /tmp/cccUdCdL.o(.text+0x8f): In function `main':
    : undefined reference to `mysql_use_result'
    /tmp/cccUdCdL.o(.text+0xa0): In function `main':
    : undefined reference to `mysql_fetch_row'
    /tmp/cccUdCdL.o(.text+0xd8): In function `main':
    : undefined reference to `mysql_free_result'
    /tmp/cccUdCdL.o(.text+0xe6): In function `main':
    : undefined reference to `mysql_close'
    /tmp/cccUdCdL.o(.eh_frame+0x11): undefined reference to
    `__gxx_personality_v0'
    collect2: ld returned 1 exit status
    yogesh, Feb 15, 2007
    #1
    1. Advertising

  2. Re: mysql in c++ initialize error occurs a simple program is executedin redhat9.0 , using gcc 3.2.2 compiler version ...

    yogesh wrote:
    > mysql in c++ initialize error occurs a simple program is executed in
    > redhat9.0 , using gcc 3.2.2 compiler version ...
    >
    >
    > #include <stdio.h>
    > #include <mysql.h>
    > #include <string.h>
    >
    > int main()
    > {
    > MYSQL* mysql;
    > MYSQL_RES* res;
    > MYSQL_ROW row;
    > char query[80];
    >
    > mysql = mysql_init( NULL );
    >
    > if( mysql == NULL ) {
    >
    > mysql_real_connect( mysql, "localhost", "username",
    > "password","dbname", 0, "/tmp/mysql.sock", 0 );
    >
    > sprintf( query, "SELECT * FROM tablename" );
    > mysql_real_query( mysql, query, (unsigned
    > int)strlen(query) );
    > res = mysql_use_result( mysql );
    >
    > while( row = mysql_fetch_row( res ) ) {
    > printf( "%s %s\n", row[0], row[1] );
    > }
    >
    >
    >
    > mysql_free_result( res );
    > mysql_close( mysql );
    > }
    >
    > else {
    > printf( "mysql_init returned NULL\n" );
    > }
    > return 0;
    > }
    > "n1.cpp" 34L, 656C written
    >
    > if i run the code iam getting the error as follows
    >
    > [root@localhost yog]# gcc n1.cpp
    > /tmp/cccUdCdL.o(.text+0x16): In function `main':
    > : undefined reference to `mysql_init'
    > /tmp/cccUdCdL.o(.text+0x4b): In function `main':
    > : undefined reference to `mysql_real_connect'
    > /tmp/cccUdCdL.o(.text+0x81): In function `main':
    > : undefined reference to `mysql_real_query'
    > /tmp/cccUdCdL.o(.text+0x8f): In function `main':
    > : undefined reference to `mysql_use_result'
    > /tmp/cccUdCdL.o(.text+0xa0): In function `main':
    > : undefined reference to `mysql_fetch_row'
    > /tmp/cccUdCdL.o(.text+0xd8): In function `main':
    > : undefined reference to `mysql_free_result'
    > /tmp/cccUdCdL.o(.text+0xe6): In function `main':
    > : undefined reference to `mysql_close'
    > /tmp/cccUdCdL.o(.eh_frame+0x11): undefined reference to
    > `__gxx_personality_v0'
    > collect2: ld returned 1 exit status
    >


    That's not an 'initialize error' it's a link error. Anyway it's not a
    C++ problem, you are using your compiler incorrectly. Most likely cause
    is that you failed to incude the correct library when you linked. But
    who knows.

    If that doesn't help then try asking on a mysql group or a g++ group.
    Its not a C++ problem.

    john
    John Harrison, Feb 15, 2007
    #2
    1. Advertising

  3. yogesh

    Kai-Uwe Bux Guest

    yogesh wrote:

    > mysql in c++ initialize error occurs a simple program is executed in
    > redhat9.0 , using gcc 3.2.2 compiler version ...
    >
    >
    > #include <stdio.h>
    > #include <mysql.h>
    > #include <string.h>
    >
    > int main()
    > {
    > MYSQL* mysql;

    [...]
    > /tmp/cccUdCdL.o(.text+0xd8): In function `main':
    > : undefined reference to `mysql_free_result'
    > /tmp/cccUdCdL.o(.text+0xe6): In function `main':
    > : undefined reference to `mysql_close'
    > /tmp/cccUdCdL.o(.eh_frame+0x11): undefined reference to
    > `__gxx_personality_v0'
    > collect2: ld returned 1 exit status



    You have a linker error. Very likely, you did not include the mysql library
    in the linker arguments passed to the compiler. Please refer to the
    documentation of g++ for the specifics of how to pass the information to
    the linker that your program needs the mysql library.

    Generally, for help on how to use g++, the gnu folks have their own news
    group down the hallway at

    gnu.g++.help

    Also note that particular platforms and most third party libraries are
    off-topic in this group. Please see the FAQ and the weekly welcome post.


    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Feb 15, 2007
    #3
  4. On Feb 15, 8:25 am, "yogesh" <> wrote:
    > mysql in c++ initialize error occurs a simple program is executed in
    > redhat9.0 , using gcc 3.2.2 compiler version ...
    >
    > #include <stdio.h>
    > #include <mysql.h>
    > #include <string.h>
    >
    > int main()
    > {
    > MYSQL* mysql;
    > MYSQL_RES* res;
    > MYSQL_ROW row;
    > char query[80];
    >
    > mysql = mysql_init( NULL );
    >
    > if( mysql == NULL ) {
    >
    > mysql_real_connect( mysql, "localhost", "username",
    > "password","dbname", 0, "/tmp/mysql.sock", 0 );
    >
    > sprintf( query, "SELECT * FROM tablename" );
    > mysql_real_query( mysql, query, (unsigned
    > int)strlen(query) );
    > res = mysql_use_result( mysql );
    >
    > while( row = mysql_fetch_row( res ) ) {
    > printf( "%s %s\n", row[0], row[1] );
    > }
    >
    > mysql_free_result( res );
    > mysql_close( mysql );
    > }
    >
    > else {
    > printf( "mysql_init returned NULL\n" );
    > }
    > return 0;}
    >
    > "n1.cpp" 34L, 656C written
    >
    > if i run the code iam getting the error as follows
    >
    > [root@localhost yog]# gcc n1.cpp
    > /tmp/cccUdCdL.o(.text+0x16): In function `main':
    > : undefined reference to `mysql_init'
    > /tmp/cccUdCdL.o(.text+0x4b): In function `main':
    > : undefined reference to `mysql_real_connect'
    > /tmp/cccUdCdL.o(.text+0x81): In function `main':
    > : undefined reference to `mysql_real_query'
    > /tmp/cccUdCdL.o(.text+0x8f): In function `main':
    > : undefined reference to `mysql_use_result'
    > /tmp/cccUdCdL.o(.text+0xa0): In function `main':
    > : undefined reference to `mysql_fetch_row'
    > /tmp/cccUdCdL.o(.text+0xd8): In function `main':
    > : undefined reference to `mysql_free_result'
    > /tmp/cccUdCdL.o(.text+0xe6): In function `main':
    > : undefined reference to `mysql_close'
    > /tmp/cccUdCdL.o(.eh_frame+0x11): undefined reference to
    > `__gxx_personality_v0'
    > collect2: ld returned 1 exit status


    This is a bit off topic here, you should ask these kinds of questions
    in a group for either gcc/g++ or for mysql. Your problem however is
    that you have forgotten to add the right libraries to the path of the
    linker.

    --
    Erik Wikström
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Feb 15, 2007
    #4
  5. Re: mysql in c++ initialize error occurs a simple program is executedin redhat9.0 , using gcc 3.2.2 compiler version ...

    >> if i run the code iam getting the error as follows
    >>
    >> [root@localhost yog]# gcc n1.cpp


    >>

    >
    > That's not an 'initialize error' it's a link error. Anyway it's not a
    > C++ problem, you are using your compiler incorrectly. Most likely cause
    > is that you failed to incude the correct library when you linked. But
    > who knows.
    >
    > If that doesn't help then try asking on a mysql group or a g++ group.
    > Its not a C++ problem.
    >


    In fact looking at your compilation there are two problems, 1) use g++
    for C++ code, 2) include the corect library

    Something like

    g++ n1.cpp -lmysql

    ***BUT*** I do not know what the mysql library is called, it might not
    be -lmysql it might be -lsomething_else. I think you need to RTFM.

    john
    John Harrison, Feb 15, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Antoon Pardon
    Replies:
    5
    Views:
    65
    Andrew Cooper
    Apr 23, 2014
  2. Tim Golden
    Replies:
    0
    Views:
    42
    Tim Golden
    Apr 22, 2014
  3. Frank Millman
    Replies:
    0
    Views:
    45
    Frank Millman
    Apr 22, 2014
  4. Antoon Pardon
    Replies:
    0
    Views:
    54
    Antoon Pardon
    Apr 22, 2014
  5. Chris Angelico
    Replies:
    0
    Views:
    55
    Chris Angelico
    Apr 22, 2014
Loading...

Share This Page