顯示具有 VC 標籤的文章。 顯示所有文章
顯示具有 VC 標籤的文章。 顯示所有文章

2016年10月20日 星期四

如何在Visual Studio中使用Boost C++ Library

1. 環境版本,取得Boost
本文使用目前Boost最新版
  • Boost C++ Library 最新版(2016.9) V1.62
               可自行到網站下載: http://www.boost.org/
  • VisualStudio 2010
  • Windows 7
2.  Boost 基本說明


下載後為一個zip壓縮檔,解開後內容如下圖:


資料夾說明如下: (Copy from user manual)
*************************************************
boost_1_62_0\ .................The “boost root directory”
   index.htm .........A copy of www.boost.org starts here
   boost\ .........................All Boost Header files
   lib\ .....................precompiled library binaries
   libs\ ............Tests, .cpps, docs, etc., by library
     index.html ........Library documentation starts here
     algorithm\
     any\
     array\
                     …more libraries…
   status\ .........................Boost-wide test suite
   tools\ ...........Utilities, e.g. Boost.Build, quickbook, bcp
   more\ ..........................Policy documents, etc.
   doc\ ...............A subset of all Boost library docs
*************************************************
Boost Library 有兩種方式給你用
1. 不需要Build:  (Header Only)
     
      大部分Lib只需要include Header就行了, Library code都寫在header
      所以你用了之後跟你的code一起Build就行了, (Templete & inline functions)


2. 要Build:
    一些需要先build, 如下: 跟系統有關的
3.  VS2010 設定, Win32 console program為例
有了上面說明,就可以很容易知道怎麼用Boost,
對於不用build的Lib,只要include header就行了
此處以一個Win32 console program為例,要做的設定基本上只要去專案中
加入boost的路徑就行了:

加入Boost的路徑可能如下,或者看你是放在哪裡?
C:\Program Files\boost\boost_1_62_0

Property\Configuration Property\C/C++\General\Additional Include Directories




4. 簡單範例: 使用boost/array

下面這段code 來測試使用boost, (我隨便寫寫...有compiler過沒問題)
 主要是宣告一個boost int array, 大小10
全部填入3, 然後print出來

**************************************
#include "stdafx.h"
#include <boost/array.hpp>
using namespace boost;

int _tmain(int argc, _TCHAR* argv[])
{
      boost::array<int, 10> a;  
      a.fill( 3);

     for( int i = 0; i < a.size(); i++ )
          printf( " %d \n ", a[i] );

    return 0;
}
*****************************************

這個是Boost中 container 的static array, 用法可參考Boost線上說明
http://www.boost.org/doc/libs/1_62_0/doc/html/array.html

完畢!

2013年2月22日 星期五

如何解除VC的 C4996 warning

Visual studio 6 版以後的Compler 對於一些C 函式庫做安全改良(security enhancements),

如:
strcpy()  改良成 strcpy_s()

當你使用 strcyp(), 會產生下列 warning... (很煩...)

warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.

消除的方法是:
  1.  加入define 
 
#define _CRT_SECURE_NO_WARNINGS

  若放在程式碼中,要注意放的位置,要放在所有include header之前。
 
  最簡單的方式是放置在preprocessor中( solution exploer右鍵/properies/ C/C++ / preprocessor )
  或是放在 stdafx.h中(if you have)。

2. 加入 編譯指令
 
#pragma warning( disable:4996 )
 
把上述指令放在 .C/.CPP 檔中,呼叫函示之前即可


Apendix:
微軟對於 eliminate deprecation warnings for the older, less secure functions 說明:
http://msdn.microsoft.com/zh-tw/library/8ef0s5kh.aspx

相關secure enhance function 說明:
http://msdn.microsoft.com/en-us/library/wd3wzwts(VS.80).aspx