문제

환경 : MS-VC ++ 6.0을 사용하고 있습니다.

  • 데이터가 포함 된 헤더 파일 그룹이 포함되어 있습니다.
  • 헤더 파일은 자주 변경되므로 모든 변경마다 경로 설정과 리 컴파일러를 변경합니다.
  • 포함 된 헤더 파일에 따라 로그 파일이 생성됩니다.
  • 로그 파일에서 헤더 파일을 추적하려면 로그 내부의 헤더 파일 경로를 인쇄하고 싶습니다.

  • 질문 1: 프로그램 내부에서 헤더 파일 경로를 얻을 수 있습니까?

  • 질문 -2 : VC ++를 사용하고 있지만 GCC에서 가능하다면 쉽게 포트 할 수 있으므로 GCC와 같은 다른 환경에서 가능하다면 알려주십시오.
도움이 되었습니까?

해결책

VC ++에서 옵션으로 컴파일합니다 /FC 현재 처리 된 파일을 넣습니다 전체 경로 에서 __FILE__ 매크로.

포함 된 경로에 대한 변경 사항을 설명합니다.

자세한 내용은 여기에 있습니다.

다른 팁

확실히 - 헤더 파일 내부 내부 :

static const char *header_path = __FILE__;

.. 그 다음으로 지적한 문자열을 인쇄합니다 header_path 로그에.

당신은 그것을 가지고 있다는 것을 의미합니까? #include "path/to/header.h", 당신은 프로그램 자체에서 "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 파일을 생성하는 MAKE 파일을 내보내십시오. 포함 파일은 .dep 파일에 나열되어 있습니다.

실행중인 프로그램 내에서 포함 된 파일에 대한 전체 경로를 얻는 아이디어에 관해서. 하나는 MSVC 6 객체 모델을 사용하여 IDE에서 포함 파일 경로 목록을 가져올 수 있습니다. 일단 그 목록이 알려지면. 찾기 파일을 사용하여 관심있는 파일 포함 경로 목록을 검색 할 수 있습니다.

좋아, 여기에 해결책이있다 (이제 내가 당신을 올바르게 이해했으면 좋겠다).

재귀 템플릿을 사용합니다 __FILE__ 매크로, 그리고 __COUNTER__ 매크로. 특별한 headerlisting.h 헤더 파일에는 템플릿 재귀의 논리가 포함되어 있으며 유용한 매크로 두 가지 (일부 도우미 매크로)에 포함됩니다.

  1. ADD_HEADER_FILE, 목록에 포함하려는 모든 헤더 파일 에이 줄을 추가하십시오.
  2. LIST_HEADER(headers), 런타임에 검색하여 포함 된 모든 헤더 파일의 목록을 검색 할 수있는 소스를 넣을 수 있습니다.

Boost의 템플릿 P0werz를 사용하면 더 쉬운 방법이 있다고 확신합니다.

아래는 첫 번째입니다 headerlisting.h, 그런 다음 두 개의 예제 헤더와 main () 소스 파일이 포함 된 예제 프로그램입니다. 이것은 G ++와 함께 Linux에서 작동합니다. 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__

이제 예제 프로그램의 경우 :

머리 1.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;
}

생성 된 출력은 다음과 같아야합니다 (Headertest.cpp에서 head1.h 또는 head2.h를 제외하지 않으면) :

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

이 효과를 알려주세요.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top