问题
I recently discovered that when using the __FILE__
predefined macro in MSVC (specifically 2013) that by default it will print relative paths for source files and absolute paths for header files.
As an example I have a VS project containing the following:
Solution
Project
Headers
foo.h
Sources
main.cpp
Both main.cpp and foo.h are in the same directory on disk.
main.cpp:
#include <iostream>
#include <string>
#include "foo.h"
int main(int, char*[])
{
std::cout << __FILE__ << std::endl;
foo::bar();
std::cout << "Press enter to exit";
std::string str;
std::getline(std::cin, str);
return 0;
}
foo.h:
#ifndef FOO_H
#define FOO_H
#include <iosfwd>
class foo
{
public:
static void bar()
{
std::cout << __FILE__ << std::endl;
}
};
#endif
When I run the application (in release mode, with the default settings - compiling with /Zi
and /FC
is not defined) then the output is:
main.cpp
c:\users\<user>\documents\dev\solution\project\foo.h
Press enter to exit
I know I could probably pass in a base path and strip it out at runtime but I was wondering whether there was any way to change this behavior at compile time? Obviously defining /FC
produces the opposite result and I cannot see anything else in the MSVC manual to control the display of header paths. I am thinking this might be a hardcoded behavior so that if the compiler is able to pick up two files called foo.h from different include paths that you can still distinguish between them or because it would be possible to have an include path unrelated to the base of the sources and displaying as relative would be messy.
来源:https://stackoverflow.com/questions/41567178/how-do-i-configure-msvc-to-show-relative-path-for-header-files-using-file