如何制作一个与mysql api链接的C ++ DLL文件 [英] How to make a C++ DLL file which link with mysql api

查看:64
本文介绍了如何制作一个与mysql api链接的C ++ DLL文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,下午好......

i尝试用c ++创建一个dll文件并用mysql api链接



i已经链接了mysql lib和标题到视觉工作室



当我尝试构建它时..

i收到以下错误消息:



hello and good afternoon...
i try to create a dll file in c++ and link with mysql api

i already link mysql lib and header to visual studio

when i try to build it..
i got these error messages :

Severity	Code	Description	Project	File	Line	Suppression State
Error	LNK2019	unresolved external symbol _mysql_init@4 referenced in function "public: void __thiscall MySQLConnector::openConnection(void)" (?openConnection@MySQLConnector@@QAEXXZ)	MySQLConnectorDLL	C:\Users\user\Desktop\MySQLConnectorDLL\MySQLConnectorDLL\MySQLConnector.obj	1	




Severity	Code	Description	Project	File	Line	Suppression State
Error	LNK2019	unresolved external symbol _mysql_real_connect@32 referenced in function "public: void __thiscall MySQLConnector::openConnection(void)" (?openConnection@MySQLConnector@@QAEXXZ)	MySQLConnectorDLL	C:\Users\user\Desktop\MySQLConnectorDLL\MySQLConnectorDLL\MySQLConnector.obj	1	




Severity	Code	Description	Project	File	Line	Suppression State
Error	LNK2019	unresolved external symbol _mysql_close@4 referenced in function "public: void __thiscall MySQLConnector::closeConnection(void)" (?closeConnection@MySQLConnector@@QAEXXZ)	MySQLConnectorDLL	C:\Users\user\Desktop\MySQLConnectorDLL\MySQLConnectorDLL\MySQLConnector.obj	1	




Severity	Code	Description	Project	File	Line	Suppression State
Error	LNK1120	3 unresolved externals	MySQLConnectorDLL	C:\Users\user\Desktop\MySQLConnectorDLL\Debug\MySQLConnectorDLL.dll	1	





我的尝试:



我在我的代码中所做的是:



在我的头文件中:



What I have tried:

what i have did in my code is :

in my header file :

#pragma once

#include "mysql.h"

class MySQLConnector {
public :
	void openConnection();
	void closeConnection();
	bool connectionStatus();
private :
	bool _connectionStatus;
	MYSQL *_con, *_connectionString;
};




我的cpp文件中的




in my cpp file

#pragma once

#include "C:\BookScanner\extension\cpp files\header\CppMySQL.h"

#include <mysql.h>

void MySQLConnector::openConnection(){
	_con = mysql_init(NULL);
	_connectionString = mysql_real_connect(_con, "localhost", "root", "", "bookbardatabase", 3306, NULL, 0);

	_connectionStatus = _connectionString ? true: false;
}

void MySQLConnector::closeConnection()
{
	mysql_close(_con);
}

bool MySQLConnector::connectionStatus()
{
	return _connectionStatus;
}





这是我的dll文件,我没有建立它



and this is my dll file, i failed to build it

#include "C:\BookScanner\extension\cpp files\header\CppMySQL.h"
#include "C:\BookScanner\extension\cpp files\cpp\CppMySQL.cpp"

MySQLConnector connector;

extern "C" __declspec(dllexport) void openConnection() {
	connector.openConnection();
}

extern "C" __declspec(dllexport) void closeConnection() {
	connector.closeConnection();
}

extern "C" __declspec(dllexport) bool connectionStatus() {
	return connector.connectionStatus();
}

推荐答案

您必须将DLL链接到MySQL库。这可以在项目设置中完成,也可以使用 #pragma comment(lib,filename)语句完成(参见评论(C-C ++) [ ^ ])。这应解决未解决的外部符号链接器错误。



您还应阅读解决方法:创建和使用动态链接库(C ++) [ ^ ]并根据它实现您的代码。



最后一点:永远不要包含源文件( *。c / * .cpp )使用 #include 语句。
You have to link your DLL with the MySQL library. This can be done in the project settings or using a #pragma comment (lib, "filename") statement (see comment (C-C++)[^]). That should solve the unresolved external symbol linker errors.

You should also read Walkthrough: Creating and Using a Dynamic Link Library (C++)[^] and implement your code according to that.

A final note: Never include source files (*.c / *.cpp) using #include statements.


这篇关于如何制作一个与mysql api链接的C ++ DLL文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆