2013-08-23 49 views
1

我已經在OS X 10.6.8上的C編程語言中成功使用了MacFUSE,它工作的很好。 在某些時候,我需要開始對C++靜態庫(libSomething.a)進行函數調用。從this question人們說這是可能的唯一方法是修改C++源代碼,使其可以從C調用(即在函數名稱和返回類型前加上extern「C」)。不幸的是,我沒有訪問源代碼,只是一個靜態C++庫* .a文件。是否可以將macfuse鏈接到C++靜態庫?

是否有某種方法可以將MacFUSE轉換爲C++或Objective-C程序以允許在靜態庫內調用C++函數?

我希望社區中的C/C++/Objective-C專家權衡這一點。

我使用的Xcode 4.3

回答

1

你可以提供一個包裝,露出了C++類的C-API:

Something.h:

class Something { 
protected: 
    int x; 
public: 
    Something() { x = 0; } 
    void setX(int newX) { x = newX; } 
    int getX() const { return x; } 
}; 

wrapper.h:

#pragma once 
typedef void *SOMETHING; 

#ifdef __cplusplus 
extern "C" { 
#endif 

SOMETHING createSomething(); 
void destroySomething(SOMETHING something); 
void setSomethingX(SOMETHING something, int x); 
int getSomethingX(SOMETHING something); 

#ifdef __cplusplus 
} // extern "C" 
#endif 

wrapper.cpp:

#include <Something.h> 
#include "wrapper.h" 

SOMETHING createSomething() { 
    return static_cast<SOMETHING>(new Something()); 
} 

void destroySomething(SOMETHING something) { 
    delete static_cast<Something *>(something); 
} 

void setSomethingX(SOMETHING something, int x) { 
    static_cast<Something *>(something)->setX(x); 
} 

int getSomethingX(SOMETHING something) { 
    return static_cast<Something *>(something)->getX(); 
}