【问题标题】:Dev C++ to compile C source fileDev C++编译C源文件
【发布时间】:2010-10-23 16:10:38
【问题描述】:

如何使用 Dev C++ 编译 C 源文件。我认为它会自动执行此操作,但由于某种原因,它编译时出现了几个错误,我认为这是因为您必须对其进行更改才能编译 C 文件。

示例测试代码:

 #include <stdio.h>



main ()        
{ int i,j;
double x,x_plus_one();
char ch;

i = 0;
x = 0;

printf (" %f", x_plus_one(x));
printf (" %f", x);

j = resultof (i);

printf (" %d",j);
}


double x_plus_one(x)          

double x;

{
  x = x + 1;
  return (x);
}


resultof (j)             

int j;

{
   return (2*j + 3);       
}

【问题讨论】:

  • 什么错误?该文件是否具有 .c 扩展名?
  • 它只是给出了基本的错误,比如太多的参数,偏离了基本的 C 语法

标签: c dev-c++


【解决方案1】:

我想你是想写这个:

#include <stdio.h>

double x_plus_one(double x);
int resultof(int j);


main()        
{ int i,j;
double x;//,x_plus_one;
char ch;

 i = 0;
 x = 0;

printf (" %f", x_plus_one(x));
printf (" %f", x);

j = resultof (i);

printf (" %d",j);
}


double x_plus_one(double x)

//double x;

{
  x = x + 1;
  return (x);
}


int resultof (int j)             

//int j;

{
   return (2*j + 3);       
}

另存为 main.cpp 并用于编译

g++.exe -D__DEBUG__ -c main.cpp -o main.o -I"C:/Program Files/CodeBlocks/MinGW/include" -g3

g++.exe -D__DEBUG__ main.o -o Project1.exe -L"C:/Program Files/CodeBlocks/MinGW/lib" -static-libgcc -mwindows -g3


Compilation results...
--------
- Errors: 0
- Warnings: 0
- Output Filename: F:\My Folder\C++ projects\test01\Project1.exe
- Output Size: 38.990234375 KiB
- Compilation Time: 1.92s

【讨论】:

    【解决方案2】:

    也将 main 更改为 int main()。并按照尼尔指出的那样进行修改。

    【讨论】:

    • 这不是必需的,因为在 C 中,默认值将始终假定为 int
    【解决方案3】:

    这是 ANSI 之前的代码。我不确定 gcc 编译器是否支持它,无论如何使用它都是不好的做法。将您的功能更改为:

    double x_plus_one( double x) {
      x = x + 1;
      return (x);     
    }
    

    您需要将其声明为:

    double x_plus_one( double x);
    

    您可能还想尝试使用 -traditional 标志进行编译,但我尚未对此进行测试。

    【讨论】:

    • 但我检查是否支持编译器选项中的所有 ANSI 支持
    • 我想我认为它已经处理好了,但我想不是
    • 其他功能也一样,你猜老方法行不通
    猜你喜欢
    • 2013-01-08
    • 2019-06-27
    • 2011-05-16
    • 2014-04-13
    • 1970-01-01
    • 1970-01-01
    • 2020-08-28
    • 1970-01-01
    • 2015-08-29
    相关资源
    最近更新 更多