顯示具有 C/Cpp 標籤的文章。 顯示所有文章
顯示具有 C/Cpp 標籤的文章。 顯示所有文章

27 12月 2013

[Java] 透過SWIG 從JNI (C/C++) callback 回 Java

最近寫 Android,一般 Java 層可以透過 JNI 呼叫 C/C++ 撰寫的 .so 檔,但是要從 C/C++ 呼叫回 Java 層呢!? 一般需要 JNIEnv 這個特殊的變數,才可以透過它來呼叫到 Java Library。

不過今天這邊要介紹的是另外一個方法 - 透過 SWIG 的 directors 的 feature
SWIG 可以將 C++ 的 class 包裝成一個 Java layer 的 class ,而 Java layer 可以去繼承 Java wrap C++ 的 class,然後 override 它的 virtual function 後,C/C++ layer 呼叫到這個 virtual function 時,就會被轉 call 到 Java layer了。

11 12月 2013

[JNI] 在C/C++中透過 SWIG 取得 JNIEnv or JavaVM

在 C/C++ layer 裡,想要 call Java layer 的 function 的話,適必需要 JNIEnv or JavaVM 這兩個 instance pointer,而透過 SWIG 包裝起來的 C/C++ layer 是不能直接接觸到 JNI layer,必須要一些手法才行。

[C/C++] 在 Windows 上 Build static libCURL library

libcurl 是個跨平台的 library ,支援非常多的 protocol,重點還是 open source!
以下就是簡單的 build 出一個 libcurl 的 static library 教學

1. 首先 download libcurl 的 source code 壓縮檔

libCURL download page

2. 在 command line 準備好 VC 的 compile 環境

"{VC Install Path}\VC\vcvarsall.bat" x86
設置完環境後,才能執行 namke

3. 開始 build libcurl

cd {libcurl extract dir}\winbuild
nmake /f Makefile.vc mode=static DEBUG=yes MAXHINE=x86
mode = static 也可以是 dll ,只是我們目標是 static library
build 完後,可以在 {libcurl extract dir}\builds 找到 lib 還有 header 檔

其他參數說明
  • VC=<6,7,8,9,10>
    • VC版本(不一定要填)
  • WITH_SSL=<dll or static>
    • 使用OpenSSL詳細參閱,會有 License 問題
  • ENABLE_WINSSL=<yes or no>
    • Enable native 的 Windows SSL 功能
    • 記得,如果要 enable 的話,ENABLE_SSPI一定也要 enable !
  • GEN_PDB=<yes or no>
    • 產生 .pdb 檔
  • DEBUG=<yes or no>
    • Debug or Release mode
  • MACHINE=<x86 or x64>
    • Target architecture
  • RTLIBCFG
    • 不指定就是 default,compile 參數會加上 /MD or /MDd (Multi-threaded)
    • =static,compile 參數會加上 /MT or /MTd (Multi-threaded DLL)

4. VC project setting 設置

Project Property | Linker | General | Additional Library Directories 
加上放有 build 出的 .lib 的目錄

Project Property | Linker | Input | Additional Dependencies 
加上 libcurl_a.lib or libcurl_a_debug.lib

Project Property | C/C++ | Preprocessor 
加上 CURL_STATICLIB 這個 macro 才行
否則會出現類似這樣的錯誤訊息:
1>A.obj : error LNK2019: unresolved external symbol __imp__curl_global_init referenced in function "bool __cdecl DownloadProtectedKey(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?DownloadProtectedKey@@YA_NAAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V12@11@Z)
1>A.obj : error LNK2019: unresolved external symbol __imp__curl_slist_append referenced in function "bool __cdecl DownloadProtectedKey(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?DownloadProtectedKey@@YA_NAAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V12@11@Z)
1>A.obj : error LNK2019: unresolved external symbol __imp__curl_slist_free_all referenced in function "bool __cdecl DownloadProtectedKey(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?DownloadProtectedKey@@YA_NAAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V12@11@Z)
1>A.obj : error LNK2019: unresolved external symbol __imp__curl_easy_strerror referenced in function "bool __cdecl DownloadProtectedKey(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?DownloadProtectedKey@@YA_NAAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V12@11@Z)
1>A.obj : error LNK2019: unresolved external symbol __imp__curl_easy_init referenced in function "bool __cdecl DownloadProtectedKey(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?DownloadProtectedKey@@YA_NAAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V12@11@Z)
1>A.obj : error LNK2019: unresolved external symbol __imp__curl_easy_setopt referenced in function "bool __cdecl DownloadProtectedKey(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?DownloadProtectedKey@@YA_NAAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V12@11@Z)
1>A.obj : error LNK2019: unresolved external symbol __imp__curl_easy_perform referenced in function "bool __cdecl DownloadProtectedKey(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?DownloadProtectedKey@@YA_NAAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V12@11@Z)
1>A.obj : error LNK2019: unresolved external symbol __imp__curl_easy_cleanup referenced in function "bool __cdecl DownloadProtectedKey(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?DownloadProtectedKey@@YA_NAAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V12@11@Z)


12 11月 2013

[Linux/Android] Export API for SharedLibrary (.so)

當開發一個 SharedLibrary (.so) 時,不想 Export 太多 API 出去(可能涉及商業機密或是單純的爽字),Windows or Linux 都有做法可以做到,這邊只說明一下 Linux / Android 的做法。

用 nm 觀察 .so 的 Symbol Table

nm -gC MyLib.so
  • -g : 只列出 external symbol
  • -C : 將 C++ 的 symbol 轉換成易讀的版本

14 8月 2013

[Win32] 在C/C++中偵測精準的程式時間(Performance Time)

在Windows底下,要偵測精準的Perfromance Time,可以透過QueryPerformanceFrequency() & QueryPerformanceCounter() 來取得。

由於取得的時間存在LARGE_INTEGER的structure,要取出來計算所花費的時間需要多點程式碼才行。這邊寫了一個簡單的Wrapper Class,讓這個AutoQueryPerformance的instance life-time結束後,自行dump出所花費的時間。

AutoQueryPerformance

#include "windows.h"

// Auto dump the life-time of AutoQueryPerformance
// Ex:
// {
//     AutoQueryPerformance autoQuery("Test", true);
//     ...
// } // it will dump message when @autoQuery destroy
class AutoQueryPerformance
{
public:
    AutoQueryPerformance(const char* info, INT loop_count=1, BOOL autoDump=TRUE)
        : m_count_time(0), m_loop_count(loop_count), m_auto(autoDump)
    {
        m_info = info;
        ::ZeroMemory(&m_cpu_freq, sizeof(LARGE_INTEGER));
        ::QueryPerformanceFrequency(&m_cpu_freq);

        if( autoDump )
            begin();
    }
    ~AutoQueryPerformance()
    {
        pause();
        if( autoDump )
            printf("[AutoQueryPerformance][%s] spend time=%.4f", m_info.c_str(), getTime());
    }
    inline void begin()
    {
        ::ZeroMemory(&m_start_clock, sizeof(LARGE_INTEGER));;
        ::QueryPerformanceCounter(&m_start_clock);
    }
    inline void pause()
    {
        ::ZeroMemory(&m_cur_clock, sizeof(LARGE_INTEGER));
        ::QueryPerformanceCounter(&m_cur_clock);
        m_count_time += m_cur_clock.QuadPart-m_start_clock.QuadPart;
    }
    inline void stop()
    {
        pause();
        m_count_time = 0;
    }
    inline float getTime()
    {
        return (m_count_time/(float)m_cpu_freq.QuadPart) / m_loop_count;
    }
private:
    LARGE_INTEGER m_cpu_freq;
    LARGE_INTEGER m_start_clock;
    LARGE_INTEGER m_cur_clock;
    LONGLONG      m_count_time;
    INT           m_loop_count;//for for-loop to calculate correct average time
    std::string   m_info;
    BOOL          m_auto;
};
#define AUTO_QUERY_PERFORMANCE(x) AutoQueryPerformance __at_qp = AutoQueryPerformance(x);


使用範例

void TestingTotalTime(int loop_count)
{
    AutoQueryPerformance ap("TestingTotalTime");
    int sum = 0;
    for(int i=0 ; i<loop_count; i++)
       sum += i;
}

void TestingSingleTime(int loop_count)
{
    AutoQueryPerformance ap("TestingSingleTime", loop_count);
    for(int i=0 ; i<loop_count; i++)
       //HeavyFunction call here;
}

Reference

25 5月 2013

[C/C++] 研究long long * float overflow 可能導致的誤差

最近看bug看到一個奇怪的現象,在64bit的AP執行時,long long * float會有誤差出現,但是32bit的AP卻沒有這樣子的問題出現,直覺上不是應該32bit的AP比較會有問題嗎!?

先來看一下 long long 跟 float ,long long的size是8個byte,float的size是4個byte
long long * float,compiler會將結果存到float的temp變數裡,此時誤差就可能會出現了,但是這個誤差是「有條件」地出現,並不是任何long long的數字乘上float都會產生誤差的

以一個簡單的程式來驗證是否會產生誤差
bool check_longlong(long long v1)
{
    float f = 1.0f;
    long long v2 = long long( v1 * f );
    printf("ori value=%lld, new value=%lld, ori==new=%d", v1, v2, v1==v2);
    return v1==v2;
}

int _tmain(int argc, _TCHAR* argv[])
{
    check_longlong(0xF0000000000);//true - OK
    check_longlong(0x0FFFFFFFFFF);//false - Error!
    check_longlong(0x0FFFFFF);//true - OK
    check_longlong(0x1FFFFFF);//false - Error!
    return 0;
}


24 5月 2013

[C/C++] 使用STL時,混用Debug & Release Mode的DLL or LIB可能會造成 Crash!

當你的Release mode的EXE,使用Debug mode的DLL,中間如果有操作(new/delete)STL的container,很容易讓AP crash,包括下列的error ...
  • Debug Assertion Failed
  • _BLOCK_TYPE_IS_VALID
  • _CrtIsValidHeapPointer

23 5月 2013

[C/C++] 取得LIB目前的DLL module path

如果寫了一個LIB or Cpp檔,讓多個DLL module include使用,該如果拿到目前DLL的module path?

GetModuleFileName() 參數hModule傳NULL的話,只能知道執行檔exe的路徑
要想知道自己在哪個DLL module的話,必須埋一些code才行

EXTERN_C IMAGE_DOS_HEADER __ImageBase
//...
WCHAR dll_path[MAX_PATH] = {0};
GetModuleFileNameW((HINSTANCE)&__ImageBase, dll_path, _MAX_PATH);

Reference :

22 3月 2013

[C/C++] General pointer type for support x86 & x64

一個pointer的size在x86跟x64的環境裡是不一樣大的

  • x86's pointer size = 4 bytes
  • x64's pointer size = 8 bytes
如果你在程式中用int or long去儲存一個pointer的話,可能在compile成x86可以work,但是compile成x64執行的話,可能就會exception了

所以建議是用標準的 uintptr_t / intptr_t 去儲存pointer,uintptr_t在copmpile成x86時的size是4 bytes,x64是8 bytes,它在C++11時才被列入標準,但在大部分的C++03的compiler已經支援了

include header : <cstdint>

Reference : 

10 3月 2013

[wxWidgets] function 傳入wxString的不定參數

在C/C++裡有不定參數的功能,讓一個function可以傳入不固定數目的參數,在function裡,透過va_list, va_arg() 把參數一個個抓出來。

在wxWidgets裡,擁有特有的字串類別wxString,用法跟std::string, std::wstring稍稍不同(多了很多API)。

std::string/std::wstring可以透過c_str()取得的內部字串buffer (char*/wchar_t*)
wxString也可以透過c_str()取得內部字串的buffer,型別是wxChar

wxChar是wxWidgets定義的,會根據compile定義的wxUSE_UNICODE(是否為unicode的build)分別對應到unicode : wchar_t, ansi : char

由於wxString是wxWidgets包裝起來的class,若將它傳進function當不定參數之一,va_arg()將無法取得wxString正確的字串內容,所以如果要將wxString的字串傳入不定參數的function中,需以c_str()傳入wxChar*字串的pointer,才能在function中取得字串的內容。

// the parameter must be wxChar*
// -> please convert wxString by c_str() before push it
//    _T("") would be convert to wxChar*
// Ex: wxString s = _T("Falldog");
//     ComposeContent(2, s.c_str(), _T("'s blogger"));
wxString ComposeString(int count, ...)
{
    wxString res;
    va_list vl;
    va_start(vl, count);
    for(int i=0 ; i<count ; ++i)
        res += va_arg(vl,wxChar*);
    va_end(vl);
    return res;
}

08 7月 2009

[C/C++] switch中的case中,放變數宣告會錯!?

相信很多寫C/C++的人一定有這樣的經驗,就是在switch的case中寫入變數的宣告,結果compile卻不會過,一定會覺得很莫名奇妙,而且錯誤訊息千奇百怪...

其實,是可以在switch裡的case中宣告變數的,只是要記得在前後加上 { ... }
Ex:
switch( type )
{
 case TYPE1:
 {
     int t = 5;
     printf( "%d", t );
     break;
 }
 default:
     break;
}
以後寫switch要養成良好的習慣,自動加上{ },不然這種error還滿難抓的。

27 12月 2007

[C/C++] string使用strtok的問題

string ip1 = "140.113.100.1";
string ip2 = ip1;
strtok( (char*)ip1.c_str(), "." );
cout<<ip1<<endl;
cout<<ip1<<endl;

這樣子的結果,會讓ip1與ip2的內容變成一樣的!!! 為什麼呢? 這一切都是strtok的陰謀啦!!!

首先 先了解一下strtok的處理動作:
char * strtok( char * input, const char * delimiter);
每呼叫一次,便將input裡的第一個delimiter字元,改成'\0' (內碼為0)。因為strtok會將input的字串的內容值改掉,因為當input=="140.113.100.1",呼叫strtok一次後,會變成"140\0113.100.1",呼叫三次後,會變成"140\0113\0100\01"。

所以在呼叫strtok時,自以為小聰明的把原本strtok(ip1.c_str(),".");產生的error改掉。改成strtok((char*)ip1.c_str(),".");將ip1.c_str()的型別const char *強制轉換成char *,才會產生這種結果。產生error的時候,就是在提醒你,可能會有問題囉。


再來,明明ip1與ip2是不一樣的變數,怎麼改到ip1後,ip2也變了呢!?
這邊的話,就跟string的assignment function的機制有關係了,據我測試後的猜測,應該是當ip2=ip1;時,兩個的字串的指標是指到同一塊memory的(for 效率)。而當ip1或ip2其中任一個將字串裡的值改變後,string的assignment function才會再allocate另一塊memory給改變字串值的變數使用。

因此,上面的code的結果,才會讓ip1與ip2的印出的結果是一樣。
所以,結論就是,想要對c++的string切token的話,就不要用c的strtok()!!!

27 9月 2007

[C/C++] 用C++的Vector產生動態二維陣列

vector是c++很好用的standard library,以往自己在產生動態二維陣列時,都習慣用int ** array,然後再配合for迴圈去new出想要的array size

    //2維的array[n][n]
    int ** array_2D = new int*[n];
    for(int i=0;i<n;i++){
        array_2D[i]=new int[n];
    }


但是當量一大時,用for loop與new配置出來的效果不是很好,因此,後來用了vector,發覺此方似乎比原本上面的方法好多了

 vector<int> row;
 row.assign(n,0);//配置一個row的大小
 vector< vector<int> > array_2D;
 array_2D.assign(n,row);//配置2維

[Linux] 在Linux底下的getch() & getche()

因為C/C++語言中的getch() & getche()都只能在windows底下執行
但是在Unix/Linux底下 就必須自己寫個function囉~

#include<termios.h>
char getch(){
    int i;//判斷是否有read到值
    char ch;
    struct termios _old, _new;

    tcgetattr (0, &_old);
    memcpy (&_new, &_old, sizeof (struct termios));
    _new.c_lflag &= ~(ICANON | ECHO);
    tcsetattr (0, TCSANOW, &_new);
    i = read (0, &ch, 1);
    tcsetattr (0, TCSANOW, &_old);
    if (i == 1)/* ch is the character to use */ 
        return ch;
    else/* there was some problem; complain, return error, whatever */ 
        printf("error!n");;
    return 0;
}