[问题] 请问C++在DLL内使用Sqlite方法

楼主: jerelee   2014-09-09 10:16:06
开发平台(Platform): (Ex: VC++, GCC, Linux, ...)
Eclipse+MinGW
额外使用到的函数库(Library Used): (Ex: OpenGL, ...)
sqlite
问题(Question):
请问C++在DLL内使用Sqlite,但产生出DLL后,再用C去CALL这个DLL会出有
No source available for "0x0" ,麻烦各位先进高手,谢谢~
喂入的资料(Input):
"20"
预期的正确结果(Expected Output):
add2(20):0.0036
错误结果(Wrong Output):
No source available for "0x0"
程式码(Code):(请善用置底文网页, 记得排版)
打包成DLL的指令
gcc -O0 -g3 -Wall -c -fmessage-length=0 -o sqlite3.o sqlite3.c
g++ -DBUILD_DLL -O0 -g3 -Wall -c -fmessage-length=0 -o create_DLL.o create_DLL.cpp
g++ -shared -oDLL.dll create_DLL.o sqlite3.o
C++的DLL程式
/*
============================================================================
Name : create_DLL.cpp
Author : test
============================================================================
*/
#ifdef BUILD_DLL
#define EXPORT __declspec(dllexport)
#else
#define EXPORT __declspec(dllimport)
#endif
#include <stdio.h>
#include <sqlite3.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
int Callback_ShowList( void *context, int count, char **values, char ** columnName )
{
int i;
context = NULL;
for( i=0 ; i<count ; ++i )
printf( "\t\t%s = %s\n" , columnName[i] , values[i] ? values[i] : "NULL" );
printf( "\n" );
return SQLITE_OK;
}
__declspec( dllexport ) double add2(char *num)
{
int row, cols,rc=0;
sqlite3_stmt * stmt;
sqlite3 *db ;
char *errMsg = NULL;
/* 开启 database 档 */
sqlite3_initialize( );
rc = sqlite3_open("DLLDB.db", &db);
if ( rc != SQLITE_OK)
{
sqlite3_close( db );
return NULL;
}
const char *sql = "SELECT * FROM TABLE1 where column1=?";
sqlite3_prepare_v2(db, sql, strlen (sql) + 1, & stmt, NULL);
sqlite3_bind_text(stmt, 1, num, -1, SQLITE_TRANSIENT);
while (1) {
int s;
s = sqlite3_step(stmt);
if (s == SQLITE_ROW) {
int bytes;
const unsigned char * text;
bytes = sqlite3_column_bytes(stmt, 0);
text = sqlite3_column_text(stmt, 0);
double text2 = sqlite3_column_double(stmt, 1);
return text2;
}
else if (s == SQLITE_DONE) {
break;
}
else {
return NULL;
}
}
}
C去CALL这个DLL程式
/*
============================================================================
Name : call_DLL.c
Author : test
============================================================================
*/
#include<stdio.h>
#include<windows.h>
int main(){
HANDLE ldll;
char (*add2)(char);
ldll = LoadLibrary("DLL.dll");
if(ldll>(void*)HINSTANCE_ERROR){
add2 = GetProcAddress(ldll, "add2");
double x=add2("20");
printf("add2(20): %lf ", x);//, mul(4,5)
} else {
printf("ERROR.");
}
}
补充说明(Supplement):
如果直接在create_DLL.cpp另外加上main()去CALL add2()是正常的
作者: a27417332 (等号卡比)   2014-09-09 11:17:00
我猜是因为name mangling?不确定我有没有猜对
作者: Killercat (杀人猫™)   2014-09-09 11:30:00
extern "C"猛然发现楼上也猜一样的东西 XD
楼主: jerelee   2014-09-09 12:03:00
请问是在create_DLL.cpp的#define EXPORT __declspec(dll加上extern "C" double add2( char *num );,这样对吗?THX使用extern "C" __declspec( dllexport ) double add2..但仍有相同的错误
作者: Killercat (杀人猫™)   2014-09-11 15:37:00
在C++的header里面要给C用的宣告都要用extern "C"{}包起来

Links booklink

Contact Us: admin [ a t ] ucptt.com