简体   繁体   English

如何在C中定义通用函数

[英]How to define common function in c

I've got two c programs: program1.c and program2.c 我有两个C程序: program1.cprogram2.c

Both of the two programs, have three or more same functions. 这两个程序都具有三个或更多相同的功能。 How can I define them one time and use it inside both? 如何定义它们一次并在两者中使用它?

An example: I've got two programs: simple-calculator.c and scientific-calculator.c . 一个例子:我有两个程序: simple-calculator.cscientific-calculator.c Both have the basic operation (Addition, Subtraction, Multiplication and Division). 两者都有基本的运算(加法,减法,乘法和除法)。 I want to define a thrid file "basic-operation.c" and create a function per operation, and then include these function inside both scripts. 我想定义一个普通文件“ basic-operation.c”并为每个操作创建一个函数,然后在两个脚本中都包含这些函数。

I guess that you are on a Posix system (Linux, MacOSX...) 我猜您在Posix系统上(Linux,MacOSX ...)

I assume that your two source files program1.c and program2.c corresponds to two different executables program1 and program2 我假设您的两个源文件program1.cprogram2.c对应于两个不同的可执行文件program1program2

Make a header file myheader.h containing types, function & variable declarations . 将头文件myheader.h包含类型,函数和变量声明

Make an implementation file common.c containing the common functions (and common variables) definitions 使实现文件common.c包含公共功能(和公共变量)定义

Add #include "myheader.h" inside all of program1.c , program2.c , common.c program1.cprogram2.ccommon.c所有内部添加#include "myheader.h"

compile common.c to an object file common.o : common.c编译为目标文件common.o

gcc -Wall -g -c common.c -o common.o

compile each of program1.c and program2.c and link it to that object file common.o 编译program1.cprogram2.c每一个并将其链接到该目标文件common.o

gcc -Wall -g program1.c common.o -o program1
gcc -Wall -g program2.c common.o -o program2

Later, learn how to make a program library -eg a static library libcommon.a or a shared library libcommon.so - (read this howto ) and use a builder like make (see this documentation , and also this example ). 稍后,学习如何制作程序库(例如,静态库libcommon.a或共享库libcommon.so )(请阅读此howto )并使用诸如make的构建器(请参见本文档以及本示例 )。 Your library could contain several members common_a.o and common_b.o .... 您的库可能包含几个成员common_a.ocommon_b.o ...。

Whenever I use a function more than once I consider making it part of one of my libraries, which I include as necessary in my later projects. 每当我多次使用一个函数时,我都会考虑使其成为我的一个库的一部分,并在以后的项目中将其包括在内。 Some functions don't make the grade, and most need to be rewritten to make them more universal and reusable. 某些功能未达到要求,大多数功能都需要重写,以使其更具通用性和可重用性。 However it's worth the little bit of extra work. 但是,值得进行一些额外的工作。

Caveat: having grown up in a procedural world, I'm still using procedural library code that some day I'm going to refactor properly into objects and the like, yeah right. 警告:我在过程世界中长大,我仍在使用过程库代码,有朝一日,我将正确地将其重构为对象等,是的。 Translation: lugging too many libraries around for too long tends to tie you into using legacy code into current projects, unless you're marginally smarter and better disciplined than I am. 翻译:长时间使用过多的库往往会使您陷入将遗留代码用于当前项目的麻烦,除非您比我稍微聪明一点,并且受过更好的训练。

However, it's not a bad place to start. 但是,这不是一个不好的起点。 Writing reusable code and building up a repository of "written once, used often" code is a practice I heartily recommend. 我衷心建议您编写可重用的代码并建立“编写一次,经常使用”代码的存储库。

Here 'sa good description. 是一个很好的描述。 Basically, you write a third file that contains common code, and compile it into a library; 基本上,您将编写一个包含通用代码的第三个文件,并将其编译为一个库。 you extract the signatures of those functions into a header file; 您将这些函数的签名提取到头文件中; you #include the header in each of your two program files; 您将#include包含在两个程序文件的每一个中; and during linking you link your new program against the compiled library. 在链接期间,您将新程序链接到已编译的库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM