所以我試圖在Linux(MonoDevelop)上使用C語言中的C庫和SWIG。所有東西都編譯並運行得很好,但是當我調用MsgBus_UseString時,控制檯輸出顯示亂碼而不是「HappyHappyJoyJoy」。
這裏是我主要在C#:獲得一個C結構來記住C#與SWIG的變化?
public static void Main (string[] args)
{
SWIGTYPE_p_msgbus msgBusObj = msgbus_client.MsgBus_Create();
ret = msgbus_client.MsgBus_setString(msgBusObj, "HappyHappyJoyJoy");
Console.WriteLine ("Set setString ret = "+ret);
ret = msgbus_client.MsgBus_UseString(msgBusObj);
Console.WriteLine ("UseString ret = "+ret);
}
下面是函數MsgBus_UseString看起來像在C LIB什麼:
static int MsgBus_UseString(msgbus_t * pObj)
{
if (pObj){
printf("The String = %s", pObj->TheString);
return Connect(pObj->TheString);
}
}
的MsgBus_setString功能在C LIB:
int MsgBus_setString(msgbus_t * pObj, const char * theStringIn)
{
int status = 2; // invalid msgbus_t pointer
if(pObj) {
pObj->TheString = (char*)theStringIn;
status = 0;
}
printf(fp, "pObj->TheString = %s \n", pObj->TheString);
return status;
}
這是我的SWIG界面的樣子:
/* Creates wrappers so that msgbus_client can be used in C# */
%module msgbus_client
%{
#include "msgbus_client.h"
#include "msgbus_var.h"
%}
%inline %{
typedef signed char int8_t;
typedef short int int16_t;
typedef int int32_t;
typedef long long int int64_t;
typedef unsigned char uint8_t;
typedef unsigned short int uint16_t;
typedef unsigned int uint32_t;
typedef unsigned long long int uint64_t;
typedef long int _time_t;
%}
%include "msgbus_client.h"
%include "msgbus_var.h"
這裏是在msgbus_client.h文件類型定義:
typedef struct msgbus msgbus_t;
我一直在掙扎與此相當長的一段時間。 MsgBus_setString函數中的printf返回「HappyHappyJoyJoy」,但由於某種原因,調用MsgBus_UseString函數時會丟失該數據。正如你所看到的,我將它們放在一起,並且「set」函數返回0(無錯誤),而「use」函數返回-1(錯誤)。這是有道理的,因爲printf顯示TheString是一堆隨機/不可讀字符。 msgbus結構非常大,所以我想避免包裝整個事情,但如果沒有其他方法,我會做。
不知道它是必要的,但爲了以防萬一,這裏是在C lib中創建功能:
msgbus_t * MsgBus_Create()
{
struct msgbus * pObj = NULL;
pObj = (struct msgbus*)calloc(1, sizeof(struct msgbus));
if (pObj){
pObj->TheString = NULL;
}
return pObj;
}
如果這一點不明確,我痛飲福利局,但基於關閉文檔這應該工作。我明顯錯過了一些東西,但我似乎無法弄清楚它是什麼。任何幫助是極大的讚賞。
編輯: 感謝您的快速回復。
這裏是msgbus結構的定義:
struct msgbus{
int fd;
int run;
pthread_t thread;
//.
//. More Stuff
//.
tlv_buffer_t * pSend;
tlv_buffer_t * pResp;
char * TheString; /*!< The String */
};
的msgbus結構的定義是msgbus_client.c,是在msgbus_client.h有以下類型定義:
typedef struct msgbus msgbus_t;
哪上面已經提到過。下面是確實的SWIGing腳本:
# Begin Swiggin! #
echo "Starting to SWIG! ... "
swig -csharp -outdir msgbus_swig msgbus_client.i
Switches="-c -I/home/includes "\
"-Wall -Wundef -Wstrict-prototypes -Wno-trigraphs -g -Wall -D__linux__ "\
# Build binaries #
g++ $Switches -o "$msgbusBin/msgbus_client_wrap.o" "msgbus_client_wrap.c"
# Generate Shared Lib #
g++ -isystem /home/include \
-isystem /home/another/include \
-D__packed__= -DIMAGE_APPS_PROC -DFEATURE_Q_SINGLE_LINK -DFEATURE_Q_NO_SELF_QPTR \
-DFEATURE_LINUX -DFEATURE_NATIVELINUX -DFEATURE_DSM_DUP_ITEMS -DTARGET_desktop \
-I/home/more/include -Wall -Wundef \
-Wstrict-prototypes -Wno-trigraphs -g -Wall -D__linux__ -fpic -shared \
-L/home/lib \
-Wl,-soname,libmsgbus_client.so -o "libmsgbus_client.so" \
"$msgbusBin/msgbus_client.o" "$msgbusBin/msgbus_var.o" "$msgbusBin/msgbus_client_wrap.o" -lmificommon -lc -lrt -lpthread
echo "SWIG complete!"
所以我複製了很多,所以有在那裏,我不完全瞭解交換機++從化妝日誌調用克。如果我需要顯示其他內容,請告訴我。再次感謝你的幫助。
你能告訴你如何定義struct msgbus_t嗎?你也應該顯示你用來生成包裝的命令行命令。 –
@ Fermat2357, 我編輯了添加您所請求的信息的問題。感謝你的幫助。 – Drellgor
一個問題是,您試圖將一個C#'字符串'直接編組到'char *'。但是需要以某種方式複製字符串。爲此,您需要爲結構中的字符串分配空間。我將創建一個示例並稍後發佈。 –