簡體   English   中英

如何將預編譯的 header 與動態庫和控制台應用程序一起使用?

[英]How to use precompiled header with dynamic library and console application?

我對預編譯的 header 有問題。 看起來有點像

ftpch.h


#pragma once
#include <vector>
#include <iostream>
#include <string>

#include <Windows.h>

ftpch.cpp

#include "ftpch.h"

然后我的 dll 庫中有一個 header 文件和 cpp。 測試.h

#pragma once

// DLL_EXPORT is a macro that changes for dll and console app like that:
// __declspec(dllexport) and __declspec(dllimport)

class DLL_EXPORT Test
{
    std::string foo() {return "ara ara"};
}

當我編譯我的動態庫項目時,這段代碼編譯得很好,但是當我在我的控制台應用程序項目中包含“test.h”並嘗試編譯它時編譯失敗。 我得到的錯誤是:

C2039: 'string' is not a member of 'std'

您的 header 文件應該始終是自給自足的。 將您的庫(在本例中為<string> )包括在您需要它們的地方、您需要它們的任何地方以及僅在您需要它們的地方。

如果您的 header 需要 function 的某個庫,請將其包含在該文件中; 不要回復已經包含該庫的其他 header,因為如果該不同的文件發生更改,那么您就不走運了。

您已經通過#pragma once獲得了#include保護,因此將#include <string.h>添加到需要它的 header 文件中不會導致沖突,並且還會使它們更易於維護和解釋。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM