C++ Connector to MySQL - c++

EDITED:
My Problem is the errors at the bottom of this post.
Heres my additional include directories
C:\Program Files\boost
C:\Program Files\MySQL\MySQL Connector C++ 1.1.3\include
C:\Program Files\MySQL\MySQL Server 5.6\include
Additional Library Directories
C:\Program Files\MySQL\MySQL Server 5.6\lib
C:\Program Files\MySQL\Connector C++ 1.1.2\lib\opt
Additional Dependencies
libmysql.lib
mysqlcppconn-static.lib
Heres my code
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
#include <stdlib.h>
#include <Windows.h>
#include <mysql.h>
#include "mysql_connection.h"
#include <cppconn/driver.h>
#define host "localhost"
#define username "root"
#define password "root"
#define database "tests"
int main()
{
MYSQL* conn;
conn = mysql_init( NULL );
if( conn )
{
mysql_real_connect( conn, host, username, password, database, 0, NULL, 0 );
}
MYSQL_RES* res_set;
MYSQL_ROW row;
unsigned int i;
mysql_query( conn, "SELECT * FROM tbl_clients WHERE id = 1" );
res_set = mysql_store_result( conn );
unsigned int numrows = mysql_num_rows( res_set );
if( numrows )
{
row = mysql_fetch_row( res_set );
if( row != NULL )
{
cout << "Client ID : " << row[0] << endl;
cout << "Client Name: " << row[1] << endl;
}
}
if( res_set )
{
mysql_free_result( res_set );
}
if( conn )
{
mysql_close( conn );
}
return 0;
}
These are the errors I get
1>------ Build started: Project: okay, Configuration: Debug Win32 ------
1>welp.obj : error LNK2019: unresolved external symbol _mysql_num_rows#4 referenced in function _main
1>welp.obj : error LNK2019: unresolved external symbol _mysql_init#4 referenced in function _main
1>welp.obj : error LNK2019: unresolved external symbol _mysql_real_connect#32 referenced in function _main
1>welp.obj : error LNK2019: unresolved external symbol _mysql_query#8 referenced in function _main
1>welp.obj : error LNK2019: unresolved external symbol _mysql_store_result#4 referenced in function _main
1>welp.obj : error LNK2019: unresolved external symbol _mysql_free_result#4 referenced in function _main
1>welp.obj : error LNK2019: unresolved external symbol _mysql_fetch_row#4 referenced in function _main
1>welp.obj : error LNK2019: unresolved external symbol _mysql_close#4 referenced in function _main
1>C:\Users\Damian\documents\visual studio 2012\Projects\okay\Debug\okay.exe : fatal error LNK1120: 8 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Please help, This project is due in about 48 hours for my class, and I've spent so much time trying to figure this out.
Thanks

respectfully, your last two questions are compiler/linker questions. I understand your frustration, as I knew how to code early but knew nothing about compilers/linkers. When you get a moment take some time to read about:
headers
binary vs object file
libraries / shared object
compiler
linker
function mangling (C vs C++)
To answer your question, I am guessing you're doing this in Microsoft Visual Studio:
You need to go to Project Properties and set Additional Library Paths (i see you did that from your last post)
You need to specify the library, I'm going out on a limb here and assuming it will mysql.lib ... There's an "Additional Library" input somewhere in the Linker section, you'll be able to identify it because kernel32.lib will be int it. Add the mysql.lib to that section. Confirm the name by going to the mysql folder which holds the binaries.
Make sure you're using the static library, the .dll will give you new issues that you don't need to worry about. This is usually achieved by setting the correct library directory. Many c++ libraries will ship with precompiled "Static" and "Dynamic" folders containing the correct libraries.

This is error is happening due to linker problem. The linker is not able to find the required static or dynamic libraries (mysql.lib,mysqlcppconn-static.lib,libmysql.dll and libmysql.lib). The additional lib setting is wrong. Check this site give the path correctly Building MySQL Connector/C++ Windows Applications with Microsoft Visual Studio

Related

LNK2019 error when building MySQL codei n VIsual Studio C++

I've spent about two days now trying to get this to work but I still keep getting the same error with every method I do. I'm new to connecting MySQL to C++ so I'm a bit lost and every time I try to compile it, the thing spits out LNK2019 error. I'm using Visual Studio 2022.
#include <iostream>
#include <mysql.h>
using namespace std;
// DATABASE STUFF
struct connection_details {
const char* server, * user, * password, * database;
};
MYSQL* mysql_connection_setup(struct connection_details mysql_details) {
MYSQL* connection = mysql_init(NULL);
if (!(mysql_real_connect(connection, mysql_details.server, mysql_details.user, mysql_details.password, mysql_details.database, 0, NULL, 0))) {
cout << "Connection Error: " << mysql_error(connection) << endl;
exit(1);
}
return connection;
}
MYSQL_RES* mysql_execute_query(MYSQL* connection, const char* sql_query) {
if (mysql_query(connection, sql_query)) {
cout << "MYSQL Query Error: " << mysql_error(connection) << endl;
exit(1);
}
return mysql_use_result(connection);
}
// MAIN FUNCTION
int main(int argc, char const* argv[]) {
MYSQL* con;
MYSQL_RES* res;
MYSQL_ROW row;
struct connection_details mysql_db;
mysql_db.server = "localhost";
mysql_db.user = "root";
mysql_db.password = "tAblE4wTe3";
mysql_db.database = "test";
con = mysql_connection_setup(mysql_db);
res = mysql_execute_query(con, "select * from table1");
cout << "Displaying Database:\n" << endl;
while ((row = mysql_fetch_row(res)) != NULL) {
cout << row[0] << " | " << row[1] << " | " << row[2] << endl;
}
mysql_free_result(res);
mysql_close(con);
return 0;
}
Errors it shows:
1>------ Build started: Project: Test, Configuration: Debug x64 ------
1>main.obj : error LNK2019: unresolved external symbol mysql_error referenced in function "struct MYSQL * __cdecl mysql_connection_setup(struct connection_details)" (?mysql_connection_setup##YAPEAUMYSQL##Uconnection_details###Z)
1>main.obj : error LNK2019: unresolved external symbol mysql_init referenced in function "struct MYSQL * __cdecl mysql_connection_setup(struct connection_details)" (?mysql_connection_setup##YAPEAUMYSQL##Uconnection_details###Z)
1>main.obj : error LNK2019: unresolved external symbol mysql_real_connect referenced in function "struct MYSQL * __cdecl mysql_connection_setup(struct connection_details)" (?mysql_connection_setup##YAPEAUMYSQL##Uconnection_details###Z)
1>main.obj : error LNK2019: unresolved external symbol mysql_query referenced in function "struct MYSQL_RES * __cdecl mysql_execute_query(struct MYSQL *,char const *)" (?mysql_execute_query##YAPEAUMYSQL_RES##PEAUMYSQL##PEBD#Z)
1>main.obj : error LNK2019: unresolved external symbol mysql_use_result referenced in function "struct MYSQL_RES * __cdecl mysql_execute_query(struct MYSQL *,char const *)" (?mysql_execute_query##YAPEAUMYSQL_RES##PEAUMYSQL##PEBD#Z)
1>main.obj : error LNK2019: unresolved external symbol mysql_free_result referenced in function main
1>main.obj : error LNK2019: unresolved external symbol mysql_fetch_row referenced in function main
1>main.obj : error LNK2019: unresolved external symbol mysql_close referenced in function main
1>C:\Users\Administrator\Documents\Visual Studio 2022\C++ Projects\Test\x64\Debug\Test.exe : fatal error LNK1120: 8 unresolved externals
1>Done building project "Test.vcxproj" -- FAILED.
These are the include/library folder locations:
C:\Users\Administrator\Documents\Visual Studio 2022\mysql stuff\libbinlogevents
C:\Users\Administrator\Documents\Visual Studio 2022\mysql stuff\libbinlogstandalone
C:\Users\Administrator\Documents\Visual Studio 2022\mysql stuff\libchangestreams
C:\Users\Administrator\Documents\Visual Studio 2022\mysql stuff\libmysql
C:\Users\Administrator\Documents\Visual Studio 2022\mysql stuff\libservices
C:\Program Files\MySQL\Connector C++ 8.0\lib64\vs14
C:\Users\Administrator\Documents\Visual Studio 2022\mysql stuff\include
C:\Program Files\MySQL\Connector C++ 8.0\include
I've tried looking it up, and every forum, video, etc. that I try using to resolve doesn't work. I'm not sure if it's a 32-bit or 64-bit issue, though I don't think it is since all files point to 64-bit folders. I'm thinking it's something in my code but I'm very new to this so I can't really pinpoint it. Any help would be appreciated.
EDIT: I managed to solve it, just started the project from scratch and followed the visual studio docs and this video.

LNK2019 error with Eclipse Paho MQTT C for a Visual Studio C++ project (win64)

I want to use the Eclipse Paho MQTT C library in a simple C++ program. For the library i used the the pre-build binaries for windows - see https://projects.eclipse.org/projects/technology.paho/downloads
C client for Windows 1.3.0 - 64 bit
I found the exact same issue in this talk LNK2019 error when compiling a Visual C++ Win32 project with Eclipse Paho MQTT.
For linkage in include I have the following setting:
C/C++ - Additional include: xxx\paho\eclipse-paho-mqtt-c-win64-1.3.0\include
Linker - additional Additional Library Directories
Linker - input - additional dependencies:
eclipse-paho-mqtt-c-win64-1.3.0\lib\paho-mqtt3cs.lib
eclipse-paho-mqtt-c-win64-1.3.0\lib\paho-mqtt3c.lib
eclipse-paho-mqtt-c-win64-1.3.0\lib\paho-mqtt3a.lib
eclipse-paho-mqtt-c-win64-1.3.0\lib\paho-mqtt3as.lib
My code is as follows:
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
extern "C" {
#include <MQTTClient.h>
#include <MQTTClientPersistence.h>
}
#define ADDRESS "xxx"
#define CLIENTID "ExampleClientSub"
#define TOPIC "xxx"
#define PAYLOAD "Hello World!"
#define QOS 1
#define TIMEOUT 10000L
int main()
{
MQTTClient client;
MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
int rc;
int ch;
MQTTClient_create(&client, ADDRESS, CLIENTID,
MQTTCLIENT_PERSISTENCE_NONE, NULL);
conn_opts.keepAliveInterval = 20;
conn_opts.cleansession = 1;
conn_opts.username = "roman.busse";
conn_opts.password = "VojUriLKhOsmzUJQ1lld";
MQTTClient_setCallbacks(client, NULL, connlost, msgarrvd, delivered);
if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
{
printf("Failed to connect, return code %d\n", rc);
exit(EXIT_FAILURE);
}
printf("Subscribing to topic %s\nfor client %s using QoS%d\n\n"
"Press Q<Enter> to quit\n\n", TOPIC, CLIENTID, QOS);
MQTTClient_subscribe(client, TOPIC, QOS);
do
{
ch = getchar();
} while (ch != 'Q' && ch != 'q');
MQTTClient_unsubscribe(client, TOPIC);
MQTTClient_disconnect(client, 10000);
MQTTClient_destroy(&client);
return 0;
}
So like you can see i said to the compiler: "Yes this is a C lib". But all in all I get the same LNK2019 errors...
Severity Code Description Project File Line Suppression State
Error LNK2019 unresolved external symbol _MQTTClient_setCallbacks
referenced in function
_main paho_test C:\Users\rtreiber\documents\visual studio 2017\Projects\paho_test\paho_test\paho_test.obj 1
Error LNK2019 unresolved external symbol _MQTTClient_create referenced
in function _main paho_test C:\Users\rtreiber\documents\visual studio
2017\Projects\paho_test\paho_test\paho_test.obj 1
Error LNK2019 unresolved external symbol _MQTTClient_connect
referenced in function
_main paho_test C:\Users\rtreiber\documents\visual studio 2017\Projects\paho_test\paho_test\paho_test.obj 1
Error LNK2019 unresolved external symbol _MQTTClient_disconnect
referenced in function
_main paho_test C:\Users\rtreiber\documents\visual studio 2017\Projects\paho_test\paho_test\paho_test.obj 1
Error LNK2019 unresolved external symbol _MQTTClient_subscribe
referenced in function
_main paho_test C:\Users\rtreiber\documents\visual studio 2017\Projects\paho_test\paho_test\paho_test.obj 1
Error LNK2019 unresolved external symbol _MQTTClient_unsubscribe
referenced in function
_main paho_test C:\Users\rtreiber\documents\visual studio 2017\Projects\paho_test\paho_test\paho_test.obj 1
Error LNK2019 unresolved external symbol _MQTTClient_freeMessage
referenced in function "int __cdecl msgarrvd(void *,char *,int,struct
MQTTClient_message *)"
(?msgarrvd##YAHPAXPADHPAUMQTTClient_message###Z) paho_test C:\Users\rtreiber\documents\visual
studio 2017\Projects\paho_test\paho_test\paho_test.obj 1
Error LNK2019 unresolved external symbol _MQTTClient_free referenced
in function "int __cdecl msgarrvd(void *,char *,int,struct
MQTTClient_message *)"
(?msgarrvd##YAHPAXPADHPAUMQTTClient_message###Z) paho_test C:\Users\rtreiber\documents\visual
studio 2017\Projects\paho_test\paho_test\paho_test.obj 1
Error LNK2019 unresolved external symbol _MQTTClient_destroy
referenced in function
_main paho_test C:\Users\rtreiber\documents\visual studio 2017\Projects\paho_test\paho_test\paho_test.obj 1 Error LNK1120 9
unresolved externals paho_test C:\Users\rtreiber\documents\visual
studio 2017\Projects\paho_test\Debug\paho_test.exe 1
So any ideas?

MSVS 2012 Express - Boost - Linker error LNK2019

I'm trying to build a project that's using some functionality of the file system part of the Boost library and I keep getting linker errors.
I followed the Boost documentation to build it and it built successfully and then moved all the lib files from the stage directory to C:/boost/lib and the hpp files to C:/boost/include. I'm using Microsoft Visual Studio 2012 Express Edition. I've made sure to add the files (libboost_filesystem-vc110-mt-1_54.lib and libboost_system-vc110-mt-1_54.lib) in the properties page to the files that need to be linked in (I also tried it with the #pragma's explicitly). I tried both the .lib files containing gd and the ones that dont (the debugging ones and the ones that aren't for debugging).
My question is, how do I fix this? Did I build the files wrong? Did I specify some sort of linker property wrong?
Here's the errors (I omitted some to keep it short, if needed I can add them all):
Error 1 error LNK2019: unresolved external symbol "class boost::system::error_category const & __cdecl boost::system::system_category(void)" (?system_category#system#boost##YAAEBVerror_category#12#XZ) referenced in function "void __cdecl boost::system::`dynamic initializer for 'native_ecat''(void)" (??__Enative_ecat#system#boost##YAXXZ) C:\Visual Studio 2012 Projects\MMS_Solution\MMS_Prj_FindFile\MMS_Prj_FindFile.obj MMS_Prj_FindFile
Error 2 error LNK2019: unresolved external symbol "class boost::system::error_category const & __cdecl boost::system::generic_category(void)" (?generic_category#system#boost##YAAEBVerror_category#12#XZ) referenced in function "void __cdecl boost::system::`dynamic initializer for 'errno_ecat''(void)" (??__Eerrno_ecat#system#boost##YAXXZ) C:\Visual Studio 2012 Projects\MMS_Solution\MMS_Prj_FindFile\MMS_Prj_FindFile.obj MMS_Prj_FindFile
[...]
Error 5 error LNK2019: unresolved external symbol "public: class boost::filesystem::path __cdecl boost::filesystem::path::root_path(void)const " (?root_path#path#filesystem#boost##QEBA?AV123#XZ) referenced in function main C:\Visual Studio 2012 Projects\MMS_Solution\MMS_Prj_FindFile\MMS_Prj_FindFile.obj MMS_Prj_FindFile
Error 6 error LNK2019: unresolved external symbol "public: class boost::filesystem::path __cdecl boost::filesystem::path::root_name(void)const " (?root_name#path#filesystem#boost##QEBA?AV123#XZ) referenced in function main C:\Visual Studio 2012 Projects\MMS_Solution\MMS_Prj_FindFile\MMS_Prj_FindFile.obj MMS_Prj_FindFile
[...]
Error 18 error LNK1120: 17 unresolved externals C:\Visual Studio 2012 Projects\MMS_Solution\x64\Debug\MMS_Prj_FindFile.exe MMS_Prj_FindFile
Here's the linker options (if others are needed I can add them):
Linker -> General
Enabled Incremental Linking = Yes (/INCREMENTAL)
Ignore Import LIbrary = No
Register Output = No
Per-user Redirection = No
Additional Library Directories = C:\openssl\lib;C:\boost\lib
Link Library Dependencies = Yes
Use Library Dependency Inputs = No
Prevent Dll Binding =
Linker -> Input
All of these are blank except for
Additional Dependencies = ssleay32.lib;libeay32.lib;Ws2_32.lib;libboost_system-vc110-mt-1_54.lib;libboost_filesystem-vc110-mt-1_54.lib;%(AdditionalDependencies)
Here's the code:
//Boost Includes
#include <boost/filesystem.hpp>
//Boost linking because visual studio won't link it (ugh)
#pragma comment (lib, "libboost_system-vc110-mt-gd-1_54.lib")
#pragma comment (lib, "libboost_filesystem-vc110-mt-gd-1_54.lib")
//Normal Includes
#include <iostream>
#include <string>
namespace bfs = boost::filesystem;
int main(int argc, char* argv[])
{
std::vector<std::string> foundPaths;
bfs::directory_iterator eit;
for(bfs::directory_iterator it("."); it != eit; it++)
{
if(!bfs::is_regular_file(it->status()))
continue;
bfs::path foundPath = it->path();
foundPaths.push_back("Root name: " + foundPath.root_name().string() + "\n" +
"Root dir : " + foundPath.root_directory().string() + "\n" +
"Root path: " + foundPath.root_path().string() + "\n" +
"Rel path: " + foundPath.relative_path().string() + "\n" +
"Prnt path: " + foundPath.parent_path().string() + "\n" +
"File name: " + foundPath.filename().string() + "\n" +
"Stem : " + foundPath.stem().string() + "\n" +
"Extension: " + foundPath.extension().string() + "\n");
}
for(std::vector<std::string>::iterator it = foundPaths.begin(); it != foundPaths.end(); ++it)
{
std::cout << *it << std::endl;
}
return 0;
}
When building Boost, make sure that you're using the parameter "address-model=64" if you're building for 64 bit. It says in the documentation that your compiler should choose the right one if it is configured correctly but apparently mine was not and was building 32 bit binaries when I wanted 64 bit binaries.

Linker error "... .obj : error LNK2019: unresolved external symbol referenced in function"

I have read all the previous replies or solutions for the same linker problem. I understand that the Linker is unable to access the library file that has the functions defined but still I have no luck in solving it!
The errors:
1>trial_12th.obj : error LNK2019: unresolved external symbol _viStatusDesc#12 referenced in function _main
1>trial_12th.obj : error LNK2019: unresolved external symbol _viClose#4 referenced in function _main
1>trial_12th.obj : error LNK2019: unresolved external symbol _viRead#16 referenced in function _main
1>trial_12th.obj : error LNK2019: unresolved external symbol _viWrite#16 referenced in function _main
1>trial_12th.obj : error LNK2019: unresolved external symbol _viOpen#20 referenced in function _main
1>trial_12th.obj : error LNK2019: unresolved external symbol _viOpenDefaultRM#4 referenced in function _main
1>C:\Users\41kchoudhary\Documents\Visual Studio 2010\Projects\trial_12th\Debug\trial_12th.exe : fatal error LNK1120: 6 unresolved externals
I am trying to send and receive data from a mixed-signal oscilloscope. In doing so I am required to write a .cpp file using the pre-defined commands/functions defined using Microsoft Visual Studio C++. I have read the user manual for using these commands, and I also have the header files and libraries required to implement it.
I am using the following code:
#include <visa.h>
#include "stdafx.h"
#include <stdio.h>
#include <memory.h>
int main(int argc, char* argv[])
{
ViSession rm = VI_NULL, vi = VI_NULL;
ViStatus status;
ViChar buffer[256];
ViUInt32 retCnt;
// Open a default session
status = viOpenDefaultRM(&rm);
if (status < VI_SUCCESS) goto error;
// Open the GPIB device at primary address 1, GPIB board 8
status = viOpen(rm, "USB::0x0699::0x0377::C011104::INSTR", VI_NULL, VI_NULL,
&vi);
if (status < VI_SUCCESS) goto error;
// Send an ID query.
status = viWrite(vi, (ViBuf) "*idn?", 5, &retCnt);
if (status < VI_SUCCESS) goto error;
// Clear the buffer and read the response
memset(buffer, 0, sizeof(buffer));
status = viRead(vi, (ViBuf) buffer, sizeof(buffer), &retCnt);
if (status < VI_SUCCESS) goto error;
// Print the response
printf("id: %s\n", buffer);
// Clean up
viClose(vi); // Not needed, but makes things a bit more
// understandable
viClose(rm); // Closes resource manager and any sessions
// opened with it
return 0;
error:
// Report error and clean up
viStatusDesc(vi, status, buffer);
fprintf(stderr, "failure: %s\n", buffer);
if (rm != VI_NULL) {
viClose(rm);
}
return 1;
}
You need to add either visa32.lib or visa64.lib to your linker settings.
One way to do that is to use a pragma in your compiler source file:
#pragma comment(lib,"visa32.lib")
If it is still not found then adjust your lib paths in your IDE or include the full path in the pragma.
I had the same issue. I figured out that you have to add visa32.lib in additional dependencies under the linker properties of your project
Go to your
project properties -> Linker -> Additional Dependencies-> Click Down Arrow -> "Edit -> Type visa32.lib
Click Ok, Ok

OpenSSL fatal error LNK1120: 3 unresolved externals

I've installed OpenSSL to Windows 8, and did the necessary modifications to the OpenSSL library, without installing it as a native library. But I've a specific error message that I couldn't find it on internet.
1>------ Build started: Project: CryptoProject, Configuration: Debug Win32 ------
1> main.cpp
1>main.obj : error LNK2019: unresolved external symbol _BN_new referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol _BN_bn2dec referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol _BN_generate_prime referenced in function _main
1>C:\Users\...\...\...\Debug\EXAMPLE.exe : fatal error LNK1120: 3 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Hope you guys find something. So here it goes:
#include <iostream>
#include <string>
#include "openssl/bn.h"
using namespace std;
int main()
{
BIGNUM * q = BN_new();
BIGNUM * two = BN_new();
long int num_bits_q = 160;
long int num_bits_p = 1024;
BN_generate_prime(q, num_bits_q, 0, two, NULL,NULL,NULL);
cout << "q is: " << BN_bn2dec(q) << endl;
return 0;
}
I ran that code and it compiled successfully in visual studio 2012, I used OpenSSL-Win32. If OpenSSL-Win64 does not work use OpenSSL-Win32, I also had the same problem. Don't forget to include ssleay32.lib, I hope you know how to configure OpenSSl in visual studio.