[問題] 請問C++在DLL內使用Sqlite方法

看板C_and_CPP (C/C++)作者時間11年前 (2014/09/09 10:16), 編輯推噓1(109)
留言10則, 3人參與, 最新討論串1/1
開發平台(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()是正常的 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 59.124.196.98 ※ 文章網址: http://www.ptt.cc/bbs/C_and_CPP/M.1410228969.A.C8A.html

09/09 11:17, , 1F
我猜是因為name mangling?不確定我有沒有猜對
09/09 11:17, 1F

09/09 11:30, , 2F
extern "C"
09/09 11:30, 2F

09/09 11:31, , 3F
猛然發現樓上也猜一樣的東西 XD
09/09 11:31, 3F

09/09 12:03, , 4F
請問是在create_DLL.cpp的#define EXPORT __declspec(dll
09/09 12:03, 4F

09/09 12:04, , 5F
加上extern "C" double add2( char *num );,這樣對嗎?THX
09/09 12:04, 5F

09/09 14:16, , 6F
使用extern "C" __declspec( dllexport ) double add2..
09/09 14:16, 6F

09/09 14:16, , 7F
但仍有相同的錯誤
09/09 14:16, 7F

09/11 10:56, , 8F
麻煩先進高手幫我再看一下,謝謝~
09/11 10:56, 8F

09/11 15:37, , 9F
在C++的header裡面要給C用的宣告都要用extern "C"{}
09/11 15:37, 9F

09/11 15:37, , 10F
包起來
09/11 15:37, 10F
文章代碼(AID): #1K3cBfoA (C_and_CPP)
文章代碼(AID): #1K3cBfoA (C_and_CPP)