简体   繁体   English

外部标头包含在C ++中

[英]External headers include in C++

I have two projects, and they have some headers in common, so they both include those headers. 我有两个项目,并且它们有一些共同的标题,因此它们都包含这些标题。 Now my question is: Can I implement these headers in outside cpp files. 现在我的问题是:我可以在外部cpp文件中实现这些头文件吗? For example when the header is inside the project you just declare the functions and implement them in .cpp file, and because the .cpp file is in the project the compiler will know where to get these functions implementation. 例如,当标头位于项目内部时,您只需声明函数并在.cpp文件中实现它们,并且由于.cpp文件位于项目中,因此编译器将知道从何处获得这些函数的实现。

So how do I do the same for outside headers?? 那么我该如何对外部头做同样的事情呢? include both .h and .cpp?? 同时包括.h和.cpp? implement all the functions inside the .h?? 实现.h ??中的所有功能

Thanks. 谢谢。

typically, you will create a dynamic or static library for the definitions found in the cpp files - these are the exported definitions of the declarations in the headers you share. 通常,您将为cpp文件中的定义创建一个动态库或静态库-这些是共享标头中声明的导出定义。

then you would link your app/executable to that library. 那么您可以将您的应用程序/可执行文件链接到该库。

during linking, the linker will locate the symbols referenced by the app in the static/dynamic library you made. 在链接期间,链接器将在您创建的静态/动态库中找到该应用程序引用的符号。 if you have implemented and configured everything correctly, there will be no linker errors. 如果正确实施和配置了所有内容,则不会出现链接器错误。

It depends on what you are trying to accomplish. 这取决于您要完成的工作。 I valid solution is to create a "header only" file where the implementation is in the declaration. 我有效的解决方案是在声明中创建一个“仅标头”文件。 A large portion of the boost library does this. Boost库的很大一部分都可以做到这一点。

Another option would be to put your common code in another project, and link to it. 另一个选择是将您的通用代码放在另一个项目中,并链接到它。

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

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