0

我的目标是创建一个 dll 和 lib 文件,所以我遵循本指南
1)我在 VS 中创建了一个新的 win32 控制台应用程序项目,选择 DLL 作为“应用程序类型”并清空项目
2)我正在尝试创建一个数据库在 C++ 中。所以我有 5 个只有函数声明的标题。
3) Database.h 是我的顶部标题,它看起来像这样:

#ifdef DBDLL_EXPORTS
#define DBDLL_API __declspec(dllexport) 
#else
#define DBDLL_API __declspec(dllimport) 
#endif
#ifndef __Database_H
#define __Database_H


#include "Table.h"

class DBDLL_API Database { ... };
#endif

4)现在只有标题,我尝试编译项目。它已编译,但我在项目文件夹中的任何地方都看不到任何 DLL 或 Lib。我如何创建它们?

4

1 回答 1

3

This is because headers are not compiled -- only CPP files are compiled (which pull in the headers -- a source file and all the headers it pulls in is called a "translation unit", which is the thing actually being compiled (independent of other TUs)).

So, in effect, the build system thinks you're building an empty project, and in this case generates nothing at all. Note that even if the headers are pulled in and compiled, unless an exported symbol is actually referenced somewhere, it may be optimized out. In such cases, you will get an (empty) DLL, but no .lib file (which can cause errors down the line if you have dependent projects looking for this .lib before there's anything in the DLL).

You'll need to create some CPP files that implement the functions declared in the headers. If you have everything in the headers and don't need any CPP files at all, then there's no point in having a DLL! (Since you could include the headers in any other project without needing any other dependency.)

Finally, your include guard (#ifndef ...) should encompass the entire header, including the DLL export macros, otherwise you'll eventually get macro redefinition warnings.

I would suggest using #pragma once instead of include guards since it's simpler and recognized by pretty much every compiler. I would also put the DLL macro goop into its own header (e.g. "dllmacros.h") and include that where it's needed (this will reduce duplication).

于 2013-02-10T23:58:18.860 回答