#1
  1. Huh?
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Location
    Scotland, UK
    Posts
    223
    Rep Power
    11

    undefined reference when linking library


    Hi :) ,

    i am trying to write a short c function that will connect to a mysql database. i am using dev c++ and my code compiles fine. the problem occurs when it tries to link the libraries. For each mysql function i try to use i get an undefined error.

    eg

    Code:
    undefined reference to 'mysql_init@4'
    Code:
    undefined reference to 'mysql_real_connect@32'
    I have the libmysql.lib included in the library path and the mysql.h file in the include path.

    Having checked the contents of the libmysql.lib file it has the corresponding functions (ie, mysql_init, mysql_real_connect etc).

    I searched the forum but all i could find was suggestions to make sure the .lib and .h files were included. Google search also proved fruitless.

    Any ideas? :confused:

    TIA :)
    -----------------------------------------------------------
    4dr14n
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Flint, MI
    Posts
    328
    Rep Power
    12
    Adrian,

    Those functions look like they have been subjected to C++ name mangling. The simplest solution is to wrap this code around the mysql.h include in your files:
    Code:
    extern "C" {
    #include <mysql.h>
    }
    The MySQL include file is supposed to do that for you, but it depends on your C++ compiler defining the __cplusplus macro. If your compiler/preprocessor doesn't do that, you'll need my code above.

    You might also want to define it as a C project instead of C++. That will also stop the compiler from name mangling.
    Clay Dowling
    Lazarus Notes
    Articles and commentary on web development
    http://www.lazarusid.com/notes/
  4. #3
  5. Huh?
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Location
    Scotland, UK
    Posts
    223
    Rep Power
    11
    thanks for the info!

    I got the program linking fine now. There was ambiguity about c/c++ like you said.

    Thanks for taking the time :)
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Bruchsal
    Posts
    1
    Rep Power
    0

    Same problem, solution does not work :(


    Hi,

    I have the same problem:
    [Linker error] undefined reference blabla....

    However, when encapsulating the libs into this

    extern "C"

    block, it gives me another error. It should actually be simple, but I can't figure it out:

    1> parse error before string constant
    2> parse error before '}' token

    What can I do?
    Thx a load in advance,
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Flint, MI
    Posts
    328
    Rep Power
    12
    Silver,

    I don't have your code to look at, so I can't say for certain, but there's probably a typo. extern is a reserved word that the compiler should always know about. Check the previous lines and make sure everything is terminated properly.
    Clay Dowling
    Lazarus Notes
    Articles and commentary on web development
    http://www.lazarusid.com/notes/
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    1
    Rep Power
    0
    Hello, I have the same problem and it doesn't help me to wrap the mysql.h. Do you have any other suggestions as to what it could be?
    I have searched the internet a lot, and there is not much help unfortunately..

    Below is part of my code - and I work in Code:Blocks.

    #include <iostream>
    #include <winsock2.h>
    #include <windows.h>
    extern "C"
    {
    #include <mysql.h>
    }
    #define SERVER "localhost"
    #define USER "root"
    #define PASSWORD "xxxxxxx"
    #define DATABASE "ETF"

    int main()
    {
    MYSQL *connect;
    connect=mysql_init(NULL);

IMN logo majestic logo threadwatch logo seochat tools logo