【问题标题】:Settings and configurations for C programming with Visual Studio Express C++?使用 Visual Studio Express C++ 进行 C 编程的设置和配置?
【发布时间】:2012-05-30 02:59:02
【问题描述】:

我想知道在使用 Microsoft VS Express 2010 编译器学习 C 语言时可以试验哪些配置和设置。如果我只是启动一个空项目并添加我的 .h 和 .c 文件,它适用于 C 文件。我不必将模式从 C++ 更改为 C,我可以编译一个可执行文件并从命令域运行它。是否可以从 IDE 中启动需要命令行参数的项目?如果是,如何?我有一个模块化程序,它进行堆排序,从命令行获取一个列表参数。我可以从使用命令行参数调用的命令行运行它,但是下次我这样做时,我想从 IDE 中使用命令行参数启动我的程序。这可能吗?

#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;
  }
}

【问题讨论】:

    标签: c visual-studio-2010 visual-c++


    【解决方案1】:

    IDE 可以使用命令行参数启动您的程序。

    添加命令行参数:

    1. 解决方案资源管理器中右键单击您的解决方案。
    2. 在侧边栏中选择配置属性->调试
    3. 将您一直使用的命令行参数添加到 Command Arguments 字段。

    此外,在您的代码中设置断点以在必要时停止执行。

    还有一件事:如果您单击 Command Arguments Configuration Properties GUI 中的大多数字段,您将获得一个带有文本区域的新对话框,用于编辑字段。除此之外,您还会看到一个 Macros>> 按钮,该按钮显示您可以在许多配置字段中使用的 IDE 宏。

    【讨论】:

      【解决方案2】:

      右击项目,转到Properties

      1. 单击配置属性。
      2. C/C++
      3. 高级
      4. 编译为(改成Compile as C code /TC

      应用上述设置并将源文件更改为.c 扩展名。

      【讨论】:

        【解决方案3】:

        其他研究员已经指出了如何进行 IDE 更改:

        当您使用 C++ (g++) 编译器进行编译并将“C”代码作为 C++ 项目的一部分时,您应该使用适当的宏来保护您的 C 代码。内容如下:

        #ifdef __cplusplus
        extern "C" {
        #endif //__cplusplus
        
        `your C Code here `
        
        
        
        #ifdef __cplusplus
        }
        #endif //__cplusplus
        

        【讨论】:

          猜你喜欢
          • 2015-02-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-04-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多