簡體   English   中英

是否可以知道是否包含標題

[英]Is it possible to know if a header is included

在源代碼中是否可以知道是否包含頭?

這是我需要的一個例子:

#include<iostream>
using namespace std;

int main()
{
    char headname[4];
    cout<<"Enter a header name : ";
    cin>>headname;

    #ifdef headname
        cout<<headname<<" Defined"<<endl;
    #else
        cout<<headname<<" Not defined"<<endl;
    #endif

    return 0;
}

例如,如果我輸入“iostream”,則輸出應為“iostream Defined”。

是。 標題通常使用包括以下內容的警衛:

#ifndef MY_HEADER_INCLUDED
#define MY_HEADER_INCLUDED

// [...]

#endif

在我的Gentoo Linux / GCC系統上,查看iostream頭我看到:

#ifndef _GLIBCXX_IOSTREAM
#define _GLIBCXX_IOSTREAM 1

所以你可以檢查_GLIBCXX_IOSTREAM 如果您沒有使用GCC,請打開您的iostream頭文件,看看他們可能定義了哪些宏。

還應該指出cout屬於iostream頭,所以當_GLIBCXX_IOSTREAM (在我的情況下)沒有定義時,代碼也將無法編譯。 但是您可以使用printf()進行測試。

當然有可能:

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

int main(void) {
    std::vector<std::string> includes;
    includes.push_back("iostream");
    includes.push_back("vector");
    includes.push_back("string");
    includes.push_back("algorithm");

    std::string user_input;
    std::cout << "Enter a header name: ";
    std::cin >> user_input;

    if ( std::find(includes.begin(), includes.end(), user_input) !=
            includes.end() ) {
        std::cout << user_input << " is included." << std::endl;
    } else {
        std::cout << user_input << " is not included." << std::endl;
    }

    return 0;
}

輸出:

paul@local:~/src/cpp/scratch$ ./incl
Enter a header name: iostream
iostream is included.
paul@local:~/src/cpp/scratch$ ./incl
Enter a header name: map
map is not included.
paul@local:~/src/cpp/scratch$

首先,檢查標題,“在運行時”和“在編譯時”沒有什么不同,因為#include在編譯時與任何#ifdef一起執行。 #include本質上是在.cpp文件頂部的標題的復制粘貼。 Razvan Cojocaru指出,您可以使用#ifdef來檢查天氣_GLIBCXX_IOSTREAM是否已定義。 以下是可以使用它的一個小例子:

class Flagger
{
    typedef unsigned long ulong;
    public:
        Flagger (ulong flags = 0)    : f(flags) { ; }
        Flagger (const Flagger& cpy) : f(cpy.f) { ; }

        void clear  (ulong flags) { f &= ~flags; }
        void set    (ulong flags) { f |= flags;  }
        void toggle (ulong flags) { f ^= flags;  }

        bool get    (ulong flags) { return f & flags; }

#ifdef _GLIBCXX_OSTREAM
        friend std::ostream& operator << (std::ostream &out, const Flagger& f)
                { /* print it how you want it*/ }
#endif

    private:
        ulong f;
};

但是, 由於以下幾個原因, 這可能是一個壞主意

  1. 在包含iostream之后,用戶需要包含上面的標題,否則編譯器會刪除該函數。
  2. 如果iostream庫的制造商在任何時間點決定更改#define名稱,則該函數將被刪除。 同樣,如果某人正在使用具有不同#define標記的不同版本的iostream,則會刪除該功能。
  3. 在上面的例子中,只是自己包含庫並沒有太大的不同。 如果某個隨機用戶不使用該庫,則其程序的最終大小將沒有太大差異,並且功能根本不會改變。

所以基本上,是的,這是可能的 ,但不實用 特別適合長期維護。 好處並不比危險大。 只需自己包含有問題的圖書館。

正如其他人所說,如果我們知道你的最終結果是什么,將會有所幫助。 您計划使用此功能的地方最有可能提供更好的解決方案。

是的,快速破解這樣做是:

您需要做的就是轉到源代碼頭文件。 並訪問任何成員(任何在頭文件的源代碼中定義的常量) -

cout<<ACCESSED_MEMBER

現在編譯並運行程序,如果你在控制台上打印出來,那么祝賀,該文件已成功包含在您的代碼中。

注意:1查找標題的特定源文件。 你可以在終端輸入

locate <header_file_name>您將獲得文件的位置。 現在通過在vi或nano或gedit或您選擇的任何編輯器中打開它來檢查任何成員。

注意:2如果此頭文件不歸您所有,請勿對其進行任何更改。

暫無
暫無

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

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