MySQL and linker errors

Started by
6 comments, last by mattd 14 years, 5 months ago
Okay, I give up. I have the MySQL libraries (5.1) downloaded and a database all set up. I added (to my C++ program) #include <my_global.h> #include <mysql.h> to my database class. I added libmysql.lib to my project options (along with setting up the library and include directories). The compiler is finding the .h files. It is finding the library files okay. But I'm still getting linker errors:

1>DatabaseClass.obj : error LNK2019: unresolved external symbol _mysql_real_connect@32 referenced in function "public: __thiscall C4::DatabaseClass::DatabaseClass(void)" (??0DatabaseClass@C4@@QAE@XZ)
1>DatabaseClass.obj : error LNK2019: unresolved external symbol _mysql_init@4 referenced in function "public: __thiscall C4::DatabaseClass::DatabaseClass(void)" (??0DatabaseClass@C4@@QAE@XZ)
1>DatabaseClass.obj : error LNK2019: unresolved external symbol _mysql_close@4 referenced in function "public: virtual __thiscall C4::DatabaseClass::~DatabaseClass(void)" (??1DatabaseClass@C4@@UAE@XZ)

Anyone have any ideas what is wrong? Searching the web seems to indicate I have performed all the needed steps. I even tried adding a couple extra libs (mysqlclient.lib, zlib.lib) to no effect. I guess I must have missed something though. Oddly, if I do an internal file search within the lib directory (where all the sql lib files live) for "mysql_init" (for instance), it comes up with 0 matches. The same search in the include folder comes up with matches as expected. Could I have bad .lib files?
Creation is an act of sheer will
Advertisement
If you're building a 32-bit project, are you using the 32-bit MySQL connector?
Similarly, if it's a 64-bit project, the 64-bit connector?

You could try dumping the lib's symbols using
dumpbin /exports libmysql.lib
Yes, I believe I have the right connector package.

This is a dump of the file. I notice it does not have the @32 or @4 on the end of the exported function names. Is that my problem? How would I go about fixing that?

Dump of file e:\dc\trunk\update server\code\mysql\lib\debug\libmysql.libFile Type: LIBRARY     Exports       ordinal    name                  _dig_vec_lower                  _dig_vec_upper                  bmove_upp                  client_errors                  delete_dynamic                  free_defaults                  get_defaults_options                  getopt_compare_strings                  getopt_ull_limit_value                  handle_options                  init_dynamic_array                  insert_dynamic                  int2str                  is_prefix                  list_add                  list_delete                  load_defaults                  modify_defaults_file                  my_end                  my_getopt_print_errors                  my_init                  my_malloc                  my_memdup                  my_no_flags_free                  my_path                  my_print_help                  my_print_variables                  my_realloc                  my_strdup                  myodbc_remove_escape                  mysql_affected_rows                  mysql_autocommit                  mysql_change_user                  mysql_character_set_name                  mysql_close                  mysql_commit                  mysql_data_seek                  mysql_debug                  mysql_disable_reads_from_master                  mysql_disable_rpl_parse                  mysql_dump_debug_info                  mysql_embedded                  mysql_enable_reads_from_master                  mysql_enable_rpl_parse                  mysql_eof                  mysql_errno                  mysql_error                  mysql_escape_string                  mysql_fetch_field                  mysql_fetch_field_direct                  mysql_fetch_fields                  mysql_fetch_lengths                  mysql_fetch_row                  mysql_field_count                  mysql_field_seek                  mysql_field_tell                  mysql_free_result                  mysql_get_character_set_info                  mysql_get_client_info                  mysql_get_client_version                  mysql_get_host_info                  mysql_get_parameters                  mysql_get_proto_info                  mysql_get_server_info                  mysql_get_server_version                  mysql_get_ssl_cipher                  mysql_hex_string                  mysql_info                  mysql_init                  mysql_insert_id                  mysql_kill                  mysql_list_dbs                  mysql_list_fields                  mysql_list_processes                  mysql_list_tables                  mysql_master_query                  mysql_more_results                  mysql_next_result                  mysql_num_fields                  mysql_num_rows                  mysql_options                  mysql_ping                  mysql_query                  mysql_read_query_result                  mysql_real_connect                  mysql_real_escape_string                  mysql_real_query                  mysql_refresh                  mysql_rollback                  mysql_row_seek                  mysql_row_tell                  mysql_rpl_parse_enabled                  mysql_rpl_probe                  mysql_rpl_query_type                  mysql_select_db                  mysql_send_query                  mysql_server_end                  mysql_server_init                  mysql_set_character_set                  mysql_set_local_infile_default                  mysql_set_local_infile_handler                  mysql_set_server_option                  mysql_shutdown                  mysql_slave_query                  mysql_sqlstate                  mysql_ssl_set                  mysql_stat                  mysql_stmt_affected_rows                  mysql_stmt_attr_get                  mysql_stmt_attr_set                  mysql_stmt_bind_param                  mysql_stmt_bind_result                  mysql_stmt_close                  mysql_stmt_data_seek                  mysql_stmt_errno                  mysql_stmt_error                  mysql_stmt_execute                  mysql_stmt_fetch                  mysql_stmt_fetch_column                  mysql_stmt_field_count                  mysql_stmt_free_result                  mysql_stmt_init                  mysql_stmt_insert_id                  mysql_stmt_num_rows                  mysql_stmt_param_count                  mysql_stmt_param_metadata                  mysql_stmt_prepare                  mysql_stmt_reset                  mysql_stmt_result_metadata                  mysql_stmt_row_seek                  mysql_stmt_row_tell                  mysql_stmt_send_long_data                  mysql_stmt_sqlstate                  mysql_stmt_store_result                  mysql_store_result                  mysql_thread_end                  mysql_thread_id                  mysql_thread_init                  mysql_thread_safe                  mysql_use_result                  mysql_warning_count                  set_dynamic                  strcend                  strcont                  strdup_root                  strfill                  strinstr                  strmake                  strmov                  strxmov  Summary          C9 .debug$S          14 .idata$2          14 .idata$3           8 .idata$4           8 .idata$5           E .idata$6
Creation is an act of sheer will
Oops, you were right mattd. I had the wrong connector. I DLed the 64 bit connector (because I have a 64 bit OS), but my application is a 32 bit app. I switched over to the 32 bit connector files and everything linked fine.

Thanks for pointing me in the right direction.
Creation is an act of sheer will
Ah good, I was stumped and was going to post some drastic stabs in the dark :)

EDIT: Those _blah@4 names are the result of __stdcall name mangling, in case you were wondering.
Okay, I'll try not to keep asking questions about this, but I have one more. I'm stumped on why I'm not getting a connection to my database.

I created my database. I use Toad to connect, so I know it's working (I even created a table within Toad, then checked that it was there in the database using the command line client. They are definitely connected, so that is working).

But in my program, I can't connect.

Obviously, I'm going to change my login details in this post, but I'll keep it consistent. Here is the constructor for the databse class, which is responsible for connecting to the database

DatabaseClass::DatabaseClass()	{    mysql = mysql_init(NULL);    if (mysql_real_connect(mysql, "localhost", "MyUserName", "MyPassword", "MyDatabase", 3306, NULL, 0) == NULL)		{		Connection = false;		Error = mysql_error(mysql);		}	else		Connection = true;	}


This returns the error: "Can't connect to MySQL server on 'localhost' (10061)"

However, in Toad, I connect just fine using the same login

Connection Type: TCP
Host: localhost
User: MyUserName
Password: MyPassword
Database: MyDatabase
Port: 3306

Any ideas?
Creation is an act of sheer will
Well, I got it, though I don't really understand why it was failing.

I changed "localhost" to "127.0.0.1" and it connected fine. I don't know why the original was not working though. I don't like things I don't understand, heh. But I guess I'll let it go, since it now works.
Creation is an act of sheer will
Maybe because of this:

Quote:
If host is NULL or the string "localhost", a connection to the local host is assumed. For Windows, the client connects using a shared-memory connection, if the server has shared-memory connections enabled. Otherwise, TCP/IP is used.


It does sound like it should fallback to TCP, but who knows.

This topic is closed to new replies.

Advertisement