C++ 何时使用 .hpp 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20023610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When to use .hpp files
提问by Phorce
I've created a library using C++, I want to create a Python Wrapper for this library and I am using boost.python
- The problem is that I have created .h
and .cpp
files separately and for some reason, the .so file cannot link these .cpp files.
我已经使用 C++ 创建了一个库,我想为这个库创建一个 Python 包装器,我正在使用boost.python
- 问题是我已经分别创建了.h
和.cpp
文件,出于某种原因,.so 文件无法链接这些 .cpp 文件。
I have therefore decided to just use the .hpp extension and include the implementation as a header file. Is this good or bad practice in terms of C++? I'm hoping to upload my project to Github so want to maximize the most optimal solution.
因此,我决定只使用 .hpp 扩展名并将实现作为头文件包含在内。就 C++ 而言,这是好还是坏的做法?我希望将我的项目上传到 Github,因此希望最大化最佳解决方案。
P.S. I think this question would more belong on programmers.stackexchange.com so if it is, could someone please migrate it.
PS我认为这个问题更应该属于programmers.stackexchange.com,所以如果是的话,有人可以迁移它。
回答by James McDonnell
This is a good idea if you have mixed c++ and c in your project. As mentioned in the comments .hpp and .h are essentially the same (for compiling c++, not c). If you are having problems linking your project it's not because of your file extensions.
如果您在项目中混合使用了 c++ 和 c,这是一个好主意。正如评论中提到的 .hpp 和 .h 本质上是相同的(用于编译 c++,而不是 c)。如果您在链接项目时遇到问题,那不是因为您的文件扩展名。
In the header files you typically 'prototype' the class definition so that all of your class members can be used, not just ones defined before the current code.
在头文件中,您通常对类定义进行“原型化”,以便可以使用所有类成员,而不仅仅是在当前代码之前定义的那些。
Please review: *.h or *.hpp for your class definitions
请查看: *.h 或 *.hpp 以了解您的类定义