2016年7月19日 星期二

undefined reference to 'function()' 錯誤

發生 undefined reference to 'function()' 錯誤可能會有以下的情境

1. 未正確連結函式(靜態/動態)庫

這個通常是link沒找到,或是函式庫的版本對應錯誤。

2.於C++專案中引用C的函式

此解決方案有兩個:

主要都是使用 extern “C”{  }將部份定義包括起來。可利用 #ifdef __cplusplus 搭配作為辨別是否C++專案用

(1)  在使用C函式的函式宣告(Head)檔,將函式使用extern “C”{  } 包起來

 

(2) 在C++專案中,使用extern “C”{  } ,將引用 C函式宣告檔 包起來

2016年5月18日 星期三

[issue] erron,24 Too many open files

 

程式在測試時經過一段時間,資料庫模組會印出”Failed to connect to database: Error: could not create socket: Too many open files” 然後就Aborted了。

2016-05-18_123312

依據網路查詢資料解釋,說明是因為開啟文件描述符(File descriptor) 超過上限,造成其他模組無法開啟所引發的錯誤。

有一個指令可以查看目前開啟的文件清單 –>  lsof

開始測試程式,使用指令加上過濾條件後,明顯看到會持續增加,因此判斷某個模組沒有進行釋放資源的動作。

2016-05-18_140239

檢查各個模組原始碼,發現有一自寫的網路連線偵測模組,它使用了socket 函式,但是卻在正常或異常返回時,沒有加入close 進行釋放動作,造成每次檢查就使用一個fd ,最終導致fd使用完畢程式崩潰。

 

P.S:搜尋資料時,發現這個問題也很容易於popen()上出現,主要是popen的shell執行有關,因此使用上也要小心。

2016年2月14日 星期日

[C#] Windows 多媒體Library 資料

專案須建立一測試程式,並須使用喇叭(WaveOut)、麥克風(WaveIn)作為聲音傳遞使用。

目前使用為函式庫為”winmm.dll” ,window multimedia library 。紀錄一下主要使用函數列表與網站資料。

Multimedia Reference


Waveform Audio Reference

This section lists the functions, structures, and messages associated with waveform audio, which are documented under Multimedia Reference. These elements are grouped as follows.

Auxiliary Devices

Easy Playback

Errors

Opening and Closing

Pitch

Playback Rate

Playback Progress

Playing

Querying a Device

Recording

Retrieving Device Identifiers

Retrieving the Current Position

Sending Custom Messages

Volume


Audio Streaming Sample Code


2016年1月6日 星期三

Test blog on OLW

Test post to Olw

[C#] Thread 傳遞參數

Thread 可以讓一動作於背景持續執行,而不會造成UI 發生凍結(freeze)現象,在多工環境下常常使用的技能。
一般常用的Thread class來自於 System.Threading。通常Thread會搭配一個需要執行的Function。
   1: public void Hello()

   2: {

   3:  Console.WriteLine("Hello World");

   4: }

   5:  

   6: Thread tt = new Thread(new ThreadStart(Hello)); //指派Hello方法

   7: tt.Start(); //開始執行Thread
於第6行,Thread建構式僅接受兩種Class型態。一個就是上面所是的ThreadStart,另一個就是今天的主題ParameterizedThreadStart。
ThreadStart 指定的方法必須為無參數型態,而ParameterizedThreadStart 則可以指定方法夾帶參數,但只限制一個且型態為 object 。因此需要透過轉換型態方式來達到所需要的參數型態。
   1: public void showHello(object obj)

   2: {

   3:     string sz = (string)obj; //unboxing

   4:     Console.WriteLine("Say Hello " + sz + " !!");

   5: }

   6:  

   7: public void Hello()

   8: {

   9:     Thread tt;

  10:     tt = new Thread(new ParameterizedThreadStart(showHello));

  11:     tt.Start("Chris"); //boxing

  12:     Thread.Sleep(2000);

  13:     tt.Abort();

  14: }
其實使用ThreadStart 也是可以傳入參數,只是需要符合其指定的方法必須為無參數型態,因此我們必須使用Class 包裝方式將參數放入其中。
   1: public class PrintHello

   2: {

   3:     public string name;

   4:     public void Hello()

   5:     {

   6:         Console.WriteLine("Hello " + name + " !!");

   7:     }

   8: }

   9:  

  10: public void Hello2()

  11: {

  12:     Thread tt;

  13:     PrintHello p = new PrintHello(); // create class

  14:     tt = new Thread(new ThreadStart(p.Hello)); // use class method

  15:     p.name = "chris";  // configure parameter

  16:     tt.Start();

  17:     Thread.Sleep(2000);

  18:     tt.Abort();

  19: }
以上為C#  Thread 帶入參數應用紀錄