如何在.c文件之间共享用户定义的函数?

时间:2017-12-16 16:24:56

标签: c

我正在学习分享&#39;功能&#39;在C编程中的几个文件中。因此,为了共享/访问在一个文件(例如 compiler.js:466 Uncaught Error: Template parse errors: Can't bind to 'ng-href' since it isn't a known property of 'a'. (" </button> <a class="btn btn-primary" [ERROR ->]ng-href="{{loginURL}}" ng-hide="isAuth()">Log In</a> </div> "): ng:///AppModule/SteemBlogComponent.html@74:31 No provider for ControlContainer (" )中创建的用户定义函数,从另一个文件(one.c)进入,我们需要创建一个以其命名的单独头文件文件一,作为two.c,并应在one.h中声明指定的函数。完成。然后,我已将one.hone.h中的头文件(one.c)包含在two.c中。现在,当我调用(文件#include "one.h")的函数时,它将从one.c中找到圆的区域。我收到编译错误

  

C:\ Users \ Lenovo \ AppData \ Local \ Temp \ cceCxXJH.o:two.c :(。text + 0x36):未定义引用`area_circle&#39;

我的功能名称为two.c。请帮助我,我的方式是否正确分享文件中的功能。

档案area_circle

one.c

档案#include<stdio.h> #include "One.h" float pi = 3.14; float radius; void main() { printf("Enter the radius: "); scanf("%f", &radius); float c_area = area_circle(radius); float c_circum = circum_circle(radius); } float area_circle(float r) { float area = pi * r * r; printf("Area of the circle: %.2f", area); return area; } float circum_circle(float rad) { float circum = 2 * pi * rad; printf("\nCircumference of the circle: %.2f", circum); return circum; }

one.h

档案float area_circle(float r); float circum_circle(float rad); float pi;

two.c

我使用#include <stdio.h> #include "one.h" //Included the header file in which the functions are declared. void main() { extern float pi; //Global variable in One.c float rds; printf("Enter the radius: "); scanf("%f", &rds); float are = area_circle(rds); //Function built in One.c; Declared in One.h float cir = circum_circle(rds); //Function built in One.c; Declared in One.h } 编译并运行程序。请帮助我。

1 个答案:

答案 0 :(得分:1)

该函数需要存在于最终的可执行文件中,因此您应该将两个文件一起编译:

gcc one.c two.c -o two
    ~~~~~

或者更明确地说,链接它们在一起:

gcc -c one.c -o one.o
gcc -c two.c -o two.o
gcc one.o two.o -o two
#rm one.o two.o

作为旁注,您应该将变量声明放在头文件中,并将其定义放在一个 .c文件中,以便正确链接。