得到包括头文件中的路径VC++
-
16-09-2019 - |
题
环境:我使用MS-VC++6.0,
- I包括一个集团的头文件 一些数据。
- 头文件经常变化,因此在 每一次改变我变化的路径 设置并重新编译器
- 一个登录文件的基础上生成的 包括标题的文件
用于跟踪的文件,从头 日志文件,我希望打印 头文件路径内的日志
问题1: 到了头 文件路径从内部程序 可能吗?
- 问题2: 我使用VC++,但如果 它能够在海湾合作委员会,然后我就可以 很容易的港口,所以,请让我知道 如果能够在任何其他 环境如海湾合作委员会
解决方案
其他提示
肯定内部文件的头说:
static const char *header_path = __FILE__;
...然后打印的字符串指出,由 header_path
进入你的记录。
你的意思是,具有 #include "path/to/header.h"
, 你想打印"path/to/头。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)
出口的作文件,其创建的。麦文件。dep文件。这包括文件中列出。dep文件。
作为该想法得到了完整的路径包括文件从内运行的程序。一个可能可以使用使用msvc6象模型,拉的列表中包括文件路径的环境。一旦这一名单是已知的。一个可以利用发现的文件,搜索那个列表中的路径包括文件的兴趣。
好的,这里是一个解决方案(现在我希望我理解你正确地).
它采用递归的模板, __FILE__
宏观, __COUNTER__
宏。一个特殊的 headerlisting.h
头文件中包含的逻辑模板递归,包括两个有用的宏(加一些辅助的宏)
ADD_HEADER_FILE
, ,只是添加这条线的每一标题的文件要包括在您的名单。LIST_HEADER(headers)
, 你可以把你的来源的检索运行时的一个列表中的所有列入标题的文件
我肯定有一个更简单的方法来做到这一点,也许有提高的模板p0werz,请评论。
下面是第一个 headerlisting.h
, ,然后一个例子计划包含两个实例的标题和一个主要()来源的文件。这可以在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__
现在,例的程序:
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
请告诉我这个工作。
不隶属于 StackOverflow