سؤال

البيئة: أنا أستخدم MS-VC ++ 6.0،

  • أقوم بتضمين مجموعة من ملفات الرأس مع بعض البيانات.
  • تتغير ملفات الرأس في كثير من الأحيان، لذلك في كل تغيير أقوم بتغيير إعداد المسار وإعادة التحويل البرمجي
  • يتم إنشاء ملف سجل بناء على ملفات الرأس المضمنة
  • لتتبع ملف الرأس من ملف السجل، أود طباعة مسار ملف الرأس داخل السجل

  • السؤال رقم 1: هل الحصول على مسار ملف الرأس من داخل البرنامج ممكن؟

  • السؤال 2: أنا أستخدم VC ++، ولكن إذا كان ذلك ممكنا في دول مجلس التعاون الخليجي، فيمكنني بسهولة منفذ، لذلك، واسمحوا لي أن أعرف، إذا كان ذلك ممكنا في أي بيئة أخرى مثل دول مجلس التعاون الخليجي
هل كانت مفيدة؟

المحلول

في VC ++، تجميع مع الخيار /FC سوف يضع الملف المعالج حاليا المسار بأكمله في ال __FILE__ دقيق.

من شأنه أن يمثل تغييراتك في مسار التضمين.

التفاصيل هنا.

نصائح أخرى

بالتأكيد - داخل ملف الرأس وضع:

static const char *header_path = __FILE__;

.. ثم مجرد طباعة السلسلة المشار إليها header_path في السجل الخاص بك.

هل تعني أن وجود #include "path/to/header.h", ، تريد طباعة "مسار / رأس / رأس" من البرنامج نفسه؟

#define INCLUDE_FILE "path/to/header.h"
#include INCLUDE_FILE

printf("Included: %s\n", INCLUDE_FILE);

يمكنك الحصول على قائمة تضمين الملفات مثل هذا.

(project)
(export makefile)
(write dependencies when writing make files)

قم بتصدير ملفات إنشاء ملفات .MAK وملفات .dep. يتم سرد الملفات المتضمنة في ملفات .dep.

بالنسبة لفكرة الحصول على المسار الكامل إلى الملفات التضمين من داخل البرنامج قيد التشغيل. قد يتمكن المرء من استخدام استخدام نموذج كائن MSVC 6، لسحب قائمة مسارات الملفات الموجودة من IDE. بمجرد معرفة هذه القائمة. يمكن للمرء استخدام ملف البحث، للبحث في قائمة المسارات الخاصة بملفات تضمينها.

حسنا، إليك حل (الآن آمل أن أفهمك بشكل صحيح).

يستخدم قوالب متكررة، __FILE__ ماكرو، و __COUNTER__ دقيق. خاص headerlisting.h يحتوي ملف الرأس على منطق إكمال القالب، ويتضمن اثنين من وحدات ماكرو مفيدة (بالإضافة إلى بعض الماكرو المساعد)

  1. ADD_HEADER_FILE, فقط أضف هذا الخط إلى كل ملف رأس تريد تضمينه في قائمتك.
  2. LIST_HEADER(headers), ، والتي يمكنك وضعها في مصدرك لاسترداد وقت التشغيل قائمة بجميع ملفات الرأس المضمنة

أنا متأكد من أن هناك طريقة أسهل للقيام بذلك، وربما مع Boost Templating P0WERZ، يرجى التعليق.

أدناه هو الأول headerlisting.h, ثم برنامج مثال يحتوي على اثنين من الرؤوس مثالا وملف مصدر رئيسي (). يعمل هذا على Linux مع G ++، آمل أن يعمل في Visual Studio أيضا (لا يمكن اختبار الآن).

headerlogic.h.

#ifndef __HEADERLOGIC_H__
#define __HEADERLOGIC_H__

// By catchmeifyoutry, 2009-12-08
//  See http://stackoverflow.com/questions/1863995/getting-included-header-file-path-in-vc

#include <vector>
#include <string>

namespace HeaderListing
{

// Recursive templates to store header files, templatized by a header index I.
// Header files will be stored by template specialization, adding new specializations
// for every new header.
//
// The recursive headers depend on the assumption that the for the previous index I-1
// there is a HeaderFile<I-1> defined which contains a method
//   void HeaderFile<I-1>::list_headers(std::vector<std::string> &headers)
// to list all I-1 previous header files.
// The I-th HeaderFile then defines it's own list_header(...) to add one name
// to the list.

// -------------------------------------
// Recursive case
//    By default, list_headers() adds no name to the list, but if this default case
//    is specialized with c-string for name, it will add to the list
template <int I>
class HeaderFile
{
public:
    typedef HeaderFile<I-1> PrevHeader;

    // in the specalization, this will store the name of header file;
    // but if no header with index I is given, name will be NULL by default
    static const char * name;

    // in the recursive case
    static inline void list_headers(std::vector<std::string> &headers)
    {
        PrevHeader::list_headers(headers);
        if (name != NULL) {
            headers.push_back(name);
        }
    }
};
template <int I> const char * HeaderFile<I>::name = NULL;

// -------------------------------------
// Base case
//    Ensures recursion ends, implements dummy list_headers()
template <>
class HeaderFile<-1>
{
public:
    static inline void list_headers(std::vector<std::string> &headers)
    { /* end of recursion, do nothing! */ }
};

}; // namespace HeaderListing

// -------------------------------------
// Macros to add header files

// Add n-th header file name (as a string) to the list
#define ADD_HEADER_FILE_NAME_N(n, file) template <> const char * HeaderListing::HeaderFile<n>::name = __FILE__; \

// Add a given string (e.g. a header filename) to the to the list
//   Uses built-in __COUNTER__ macro to track the current header count.
//   NOTE: it doesn't matter if count was used in between since there can be gaps in between the header indices
#define ADD_HEADER_FILE_NAME(file) ADD_HEADER_FILE_NAME_N(__COUNTER__, file)

// Add the current (header) file to the list
//   Uses the built-in __FILE__ macro.
#define ADD_HEADER_FILE ADD_HEADER_FILE_NAME(__FILE__)

// List all defined header files
//   The "headers" argument should be a std::vector<std::string>
#define LIST_HEADERS(headers) HeaderListing::HeaderFile<__COUNTER__>::list_headers(headers);

#endif // __HEADERLOGIC_H__

الآن للحصول على برنامج المثال:

Head1.h.

#ifndef __HEAD1__
#define __HEAD1__

#include "headerlisting.h"
ADD_HEADER_FILE

#endif // __HEAD1__

head2.h.

#ifndef __HEAD2__
#define __HEAD2__

#include "headerlisting.h"
ADD_HEADER_FILE

#endif // __HEAD2__

headertest.cpp.

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

#include "headerlisting.h"
#include "head1.h" // <-- TRY COMMENTING THESE OUT!
#include "head2.h" // <-- TRY COMMENTING THESE OUT!

using namespace std;

int main()
{
    // list included header files
    vector<string> headers;
    LIST_HEADERS(headers);

    // display included header files
    size_t n = headers.size();
    cout << "Found " << n << " headers" << endl;
    for (size_t h = 0; h < n; ++h)
    {
        cout << "header " << h << " :\t" << headers[h] << endl;
    }

    return 0;
}

يجب أن يبدو الإخراج الذي تم إنشاؤه مثل هذا (إذا كنت لا تستبعد head1.h أو head2.h من headertest.cpp، أي):

Found 2 headers
header 0 :  head1.h
header 1 :  head2.h

من فضلك قل لي هذا يعمل.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top