Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为依赖文件生成单独的.o e1.c_C - Fatal编程技术网

为依赖文件生成单独的.o e1.c

为依赖文件生成单独的.o e1.c,c,C,我需要单独生成.o,并且需要在创建可执行表以解决依赖关系时合并这两个.o 我单独编译了e1.c,得到了下面的错误 typedef struct members_t{ int a1; int b1; }members; int add(int a,int b){ return (a+b); } int sub(int a,int b){ return (a-b); } 如何在没有错误的情况下分别生成两个.o 我使用extern通知编

我需要单独生成.o,并且需要在创建可执行表以解决依赖关系时合并这两个.o

我单独编译了e1.c,得到了下面的错误

typedef struct members_t{
        int a1;
        int b1;
}members;
int add(int a,int b){
        return (a+b);
}
int sub(int a,int b){
        return (a-b);
}
如何在没有错误的情况下分别生成两个.o


我使用extern通知编译器这是在某处定义的,但仍然会出现上述错误。

编译器必须具有结构成员的完整声明以及编译e1.c时add和sub函数的声明原型,因此应该将其放在头文件中

第一步。 将声明移动到头文件

创建新的头文件,如下所示:

[root@angus]# gcc -c e1.c -o e1.o
e1.c:2: warning: useless storage class specifier in empty declaration
e1.c: In function ‘main’:
e1.c:4: error: ‘members’ undeclared (first use in this function)
e1.c:4: error: (Each undeclared identifier is reported only once
e1.c:4: error: for each function it appears in.)
叫这个e.h

第二步

到e1.c和e2.c

第三步

从e2.c中删除结构成员的声明

第四步

删除外部结构成员的正向声明;来自e1.c

第五步

修复代码。您需要实际创建members结构的实例,并初始化其 成员

然后,您就可以按照您的方式编译文件。

结构成员是一种类型。members不是对象,members.a1是语法错误。其他人关于将类型定义放在头文件中的说法是正确的,但除此之外,您还需要定义该类型的对象。

将结构声明放在两个文件都包含的头文件中。
[root@angus]# gcc -c e1.c -o e1.o
e1.c:2: warning: useless storage class specifier in empty declaration
e1.c: In function ‘main’:
e1.c:4: error: ‘members’ undeclared (first use in this function)
e1.c:4: error: (Each undeclared identifier is reported only once
e1.c:4: error: for each function it appears in.)
#ifndef E_HEADER_H
#define E_HEADER_H

 typedef struct members_t{
        int a1;
        int b1;
}members;
int add(int a,int b);
int sub(int a,int b);
#endif
#include "e.h" 
#include <stdio.h>
#include "e.h"
int main(int argc, char *argv[]){
        members m;
        m.a1 = 1;
        m.a2 = 2;
        printf("\n %d \n",m.a1);
        printf("%d",add(2,2));
        printf("%d",sub(2,2));

}