[問題] 請問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