淡然916 发表于 2014-7-29 20:54:16

一道关于c的问题—多文件分离

本帖最后由 淡然916 于 2014-8-13 10:05 编辑

#include<stdio.h>
#include"special_comp.c"
int specialComp(int x, int y){
      return ((y * (x + y)) - x) * x;
}
int main(){
      int a, b;
      a = 3;
      b = 9;
      printf("%d\n", specialComp(a,b));
      return 0;
}

将specialComp这个函数整体(包括函数头和函数体)移动到已经建立好的special_comp.c文件中




这怎么做啊,special_comp.c已经建好了

YIn 发表于 2014-7-31 22:44:20

自定义头文件(.h)

淡然916 发表于 2014-8-1 07:35:32

YIn 发表于 2014-7-31 22:44
自定义头文件(.h)

不懂啊

沉默默 发表于 2014-8-1 09:16:16

建立头文件,把函数声明放进头文件,然后建立源文件,把函数定义放在源文件,最后在主函数所在的文件里添加 # include 刚刚建立的头文件,然后就可以使用该函数了

oggplay 发表于 2014-8-1 10:03:37

方法很多啊。再建一个新文件,把specialComp函数拷贝到这个文件里,然后再编译链接。

淡然916 发表于 2014-8-1 20:48:58

oggplay 发表于 2014-8-1 10:03
方法很多啊。再建一个新文件,把specialComp函数拷贝到这个文件里,然后再编译链接。

将specialComp这个函数整体(包括函数头和函数体)移动到已经建立好的special_comp.c文件中
这个怎么做啊,就是上面的代码哪里复制到special_comp.c文件中

故乡的风 发表于 2014-8-2 08:20:41

文件special_comp.c
int specialComp(int x, int y){
      return ((y * (x + y)) - x) * x;
}

文件special_comp.h
int specialComp(int x, int y);

文件main.c
#include <stdio.h>

#include "special_comp.h"

int main(){
      int a, b;
      a = 3;
      b = 9;
      printf("%d\n", specialComp(a,b));
      return 0;
}

cable5881 发表于 2014-8-2 16:12:12

/1024/1024/1024
页: [1]
查看完整版本: 一道关于c的问题—多文件分离