在不同的地方重用功能 [英] Reusing functions in different places

查看:52
本文介绍了在不同的地方重用功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在不同的项目中写了几个函数(没有类)。



所以你可以想象我想把这些函数声明/实现放在一个文件

并且可以直接在其他项目中使用它们。



我该怎么做?我应该把所有这些东西都嵌入到一个头文件中吗?



任何一个例子如何理解它。

解决方案

< blockquote>如果这些只是属于同一个问题区域的一小部分函数,​​你可以将所有函数声明放在一个头文件中,并将相应的实现放在一个cpp文件中。

使用你的在项目中你必须将这两个源文件包含在项目中。



另一种技术是从你的函数生成一个对象库。在Windows环境中,这些称为.olb。然后,链接器将根据需要从这样的库中选择函数并将它们包含在程序中。 (优点是只有已使用的函数包含在程序中!)在这种情况下,您需要在使用项目中包含头文件和.olb文件。请注意,在这种情况下,您不必向库的用户公开函数实现的源代码。



第三种技术是生成动态链接库(Windows下的.dll)。应用程序加载时,将在运行时加载此类库。链接器到目前为止还涉及到它在.exe文件中添加了一些线索,要加载哪些DLL以及哪些函数来自哪个DLL。在这种情况下,您必须将.h文件,.lib文件和.dll文件分发给库的用户。后两个是在构建DLL时生成的。这种方法是最耗费人力的方法,它提供了在不触及.exe文件的情况下交换DLL的机会 - 如果您想为库提供更新或错误修复,则非常有用。


< blockquote>为什么不创建一个DLL项目并将其包含在那里?然后你可以释放二进制DLL文件并使用它们编译并准备好从那里开始。


将函数声明放在一些头文件中,比如说 myfuncs.hpp ,然后将函数定义放在 cpp 文件中,比如说 myfuncs.cpp



Make确定 myfuncs.cpp 包含 myfuncs.hpp ,然后对于其他每个 cpp 文件,只需包含 myfuncs.hpp



您可以将这两个复制到您正在处理的任何项目中,或者您可以将 myfuncs.cpp 构建到lib中并且只包括 myfuncs.hpp 在你的其他代码中,并确保链接到该库。



希望这有帮助,

Fredrik


I wrote several functions (no classes) which I may need in different projects.

So you can imagine I want to put these function declaration/implementations in one file
and be able to use them from other projects directly.

How can I do this? Shall I embed all these things in one header file?

Any example how to do it appreciated.

解决方案

If these are just a small bunch of functions that all belong to the same problem area, you may put all function declarations in one header file and the corresponding implementations in one cpp file.
To use your functions in a project you will have to include these two source files into the project.

Another technique is to generate an object library from your functions. In the Windows environment, these are called .olb. The linker will then pick functions on demand from such a library and include them in the program. (Advantage that only the used functions are included in the program!) In that case you need to include the header file and .olb file in the using project. Note that in this case you do not have to disclose the source code of your function implementation to the users of the library.

A third technique is to generate a dynamic link library (.dll under Windows). Such a library will be loaded at run-time when you application loads. The linker is just in so far involved that it puts some clues in your .exe file, which DLLs to load and which functions are to come from which DLL. In that case you must distribute the .h file, the .lib file and the .dll file to the user of your library. The latter two are produced when you build the DLL. This approach is the most labor intensive one, put it offers the chance of exchanging the DLL without touching the .exe file -- useful if you want to deliver updates or bug-fixes for your library.


Why not create a DLL project and include them in there? You can then "release" just the binary DLL file and use them compiled and ready to go from that.


Put the function declaration in some header file, say myfuncs.hpp, then put the function definitions in a cpp file, say myfuncs.cpp.

Make sure myfuncs.cpp includes myfuncs.hpp and then for every other cpp file just include myfuncs.hpp.

You can either copy these two into whatever project you're working on, or you can build myfuncs.cpp into a lib and just include myfuncs.hpp in your other code and make sure to link against that lib.

Hope this helps,
Fredrik


这篇关于在不同的地方重用功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆