开发平台(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()是正常的