在静态库项目中使用预编译头
我需要一些关于如何设置我的项目的建议。我正在构建一个静态库,并想在走得太远之前知道我使用预编译头的方式是否正确。
到目前为止,我的 stdafx 文件仅包含(对于 DWORD 等类型)和(对于 std::string)。
我构建了一个名为 TestFuncs.cpp/h TestFuncs.cpp 的简单 cpp/标头组合
:
#include "stdafx.h"
using namespace std;
void MyFunc(DWORD a, string b) {
// irrelevant
}
TestFuncs.h
#pragma once
void MyFunc(DWORD a, std::string b);
此处的代码可以正确编译。我遇到的问题是,当我想在另一个项目中使用这个静态库时,我通常会执行#include“path_to_my_static_lib_project/TestFuncs.h”
但是,这个问题是基于TestFuncs.h,DWORD和字符串都会当时未知,因为它们是从 stdafx.h 文件定义的类型。
我想出的一个解决方案(我不知道这样做是否正确)只是在 #pragma 一次之后将 stdafx.h 包含在 TestFuncs.h 的顶部。现在项目工作文件是否使用预编译头。
这是应该如何完成的,或者是否有正确的方法?
谢谢。
I am in need of some advice on how to setup my project. I'm building a static library and want to know if the way I'm using precompiled headers is correct before I go too far.
So far, my stdafx file is just including (for types like DWORD, etc.) and (for std::string).
I built a simple cpp/header combo called TestFuncs.cpp/h
TestFuncs.cpp:
#include "stdafx.h"
using namespace std;
void MyFunc(DWORD a, string b) {
// irrelevant
}
TestFuncs.h
#pragma once
void MyFunc(DWORD a, std::string b);
This code here compiles properly. The issue that I have is when I want to use this static library in another project, I would normally do #include "path_to_my_static_lib_project/TestFuncs.h"
However, the issue with this, is based upon TestFuncs.h, both DWORD and string would be unknown at the time, as they are types defined from the stdafx.h file.
One solution that I came up with (that I don't know it's correct to be doing so) is just including stdafx.h at the top of TestFuncs.h after the #pragma once. Now the project works file using precompiled headers or not.
Is this how it should be done, or is there a proper way of doing this?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要在库的公共头文件中
#include "stdafx.h"
。我所说的公共头文件是指您的库的客户端#include
所使用的头文件。相反,仅定义绝对最小值,并且最好在此文件中使用 100% 可移植代码。如果您的库将由不同的编译器或平台使用,也请避免使用 STL。
因此,假设您有一个在
gizmo.cpp
中实现的公共头文件my_library.h
。您将拥有以下内容:gizmo.cpp
另外,题外话,但使用宏保护而不是
#pragma Once
,它不是 C++ 语言的一部分,因此并非所有编译器都支持。这是一个坏习惯。编辑:
对于当您的标头为
#include
-ed 时未定义DWORD
和string
的问题,我有 3 个建议:1)仅使用可移植数据类型。即标准定义的数据类型。
DWORD
是微软的发明(几十年前)。它不是语言的一部分,并且不可移植。相反,请使用unsigned long
或其他合适的东西。2) 如果您的库将由使用您以外的编译器编译的代码使用,请不要在库的公共接口中使用
string
。原因是因为string
完全在头文件中定义,因此每个编译器可能都有自己的实现。一个编译器的字符串
可能看起来与另一个不同。3) 假设#2 不适用,请随意在标头顶部
#include
标准库中的任何必要标头。如果您在公共界面中使用string
,请在标头中使用#include
。 (只是请不要使用命名空间std
)。您的标头应该是独立的。EDIT2:
这是我声明你的函数的方式:
Don't
#include "stdafx.h"
in your library's public header file. By public header file, I mean the header file that clients of your library would#include
.Instead, only define the absolute minimum, and preferably use 100% portable code in this file. Also avoid using the STL if your library is going to be used by different compilers or platforms.
So suppose you have a public header file
my_library.h
which is implemented ingizmo.cpp
. You would have the following:gizmo.cpp
Also, off-topic, but use macro guards and not
#pragma once
which is not part of the C++ language, and therefore isn't supported by all compilers. It is a bad habit to get in to.EDIT:
As for the question of
DWORD
andstring
not being defined when your header is#include
-ed, I have 3 suggestions:1) Only use portable datatypes. That is, datatypes defined by the Standard.
DWORD
is a microsoft invention (from decades ago). It is not part of the language, and it is not portable. Instead, useunsigned long
or something else suitable.2) Don't use
string
in your library's public interface if your library is going to be used by code compiled with a compiler other than yours. The reason is becausestring
is defined completely in header files, so each compiler potentially has it's own implementation. One compiler'sstring
might look different from another's.3) Assuming #2 doesn't apply, feel free to
#include
any necesarry headers from the Standard Library at the top of your header. If you usestring
in your public interface,#include <string>
in your header. (Just please do notusing namespace std
). Your header should be self-contained.EDIT2:
Here is how I would declare your function: