2012-05-30 30 views
1

我想知道我可以嘗試哪些配置和設置,因爲我正在使用Microsofts VS Express 2010編譯器學習C語言。它適用於C文件,如果我只是開始一個空的項目並添加我的.h和.c文件。我不必將模式從C++更改爲C,我可以編譯可執行文件並從命令域運行它。是否可以從IDE啓動項目中獲取命令行參數?如果是,如何?我有一個模塊化的程序,可以從命令行獲取列表參數。我可以從命令行使用命令行參數運行它,但下次我這樣做時,我想從IDE內使用命令行參數啓動我的程序。這可能嗎?使用Visual Studio Express C++進行C編程的設置和配置?

#include <stdio.h> 
#include <stdlib.h> 

#include "sort.h" 


/* argc kommer att innehålla antalet argument på kommandoraden 
    argv är en vektor med argc strängar som representerar 
    argumenten. Observera att första argumentet, argv[0], är 
    programnamnet. 
*/ 
int main(int argc, char *argv[]) { 
    int *vector, n, i; 

    if(argc > 1) { 
    n = argc - 1; 
    vector = (int *) malloc(n * sizeof(int)); 

    for(i = 0; i<n; i++) 
     vector[i] = atoi(argv[i+1]); 

    sort(vector, n); 

    printf("Sorted input: %d", vector[0]); 
    for(i = 1; i<n; i++) 
     printf(" %d", vector[i]); 
    printf("\n"); 

    free(vector); 
    return 0; 
    } else { 
    fprintf(stderr, "Error: No input arguments.\n"); 
    printf("This program sorts number on the command line.\n"); 
    printf("Usage: %s n1 n2 n3 ...\n", argv[0]); 

    return 1; 
    } 
} 

回答

2

IDE可以通過命令行參數啓動程序。

要添加命令行參數:

  1. 上的Solution Explorer中您的解決方案單擊鼠標右鍵。
  2. 選擇配置屬性 - >調試在邊欄中。
  3. 將您一直使用的命令行參數添加到命令參數字段中。

此外,在代碼中設置斷點以在必要時暫停執行。

一兩件事:如果你在命令參數點擊 領域,或者大部分在配置屬性的字段 GUI,你會得到一個文本區域一個新的對話框編輯領域。除此之外,您將看到一個宏>>按鈕,該按鈕顯示可在許多配置字段中使用的IDE宏。

1

右鍵點擊該項目,進入Properties

  1. 單擊配置屬性。
  2. C/C++
  3. 高級
  4. 編譯爲(更改爲Compile as C code /TC

應用上面的設置和源文件更改爲.c擴展。

enter image description here

1

其他研究員已經指出瞭如何做一個IDE變化:

當你與一個C編譯++(G ++)編譯器,並有一個「C」代碼作爲C++項目的一部分你應該有適當的宏來守護你的C代碼。如下:

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

`your C Code here ` 



#ifdef __cplusplus 
} 
#endif //__cplusplus 
相關問題