#include the absolute syntax path in c / C ++

For some reason, I need to use the absolute path in #include for my system.

Is #include "D:\temp\temp_lib\temp.h" ?

I tried these different uses and it all works.

  • #include "D:\temp\temp_lib\temp.h"
  • #include "D:\\temp\\temp_lib\\temp.h"
  • #include "D:/temp/temp_lib/temp.h"

I just want to know which one should I use? I am using MSVC 2005. I am wondering if all three will work on Linux or other environments.

I expected # 1 to be a compile time error, but I didn’t get it. Does anyone know why this is?

+4
source share
1 answer

Each implementation that I know of, and, of course, MSVC 2005 and linux, allows you to specify the paths to directories in which to find header files. You should include D: \ temp \ temp_lib in the list of directory paths and then use

 #include <temp.h> 

For gcc, use the -I path. For MSVC see Where does Visual Studio look for C ++ header files?

The reason # 1 is not a syntax error is that although it looks like a string literal, it is not. Specification

 #include "q-char-sequence" 

Where q is char

any member of the original character set except a newline and "

In particular, \ does not really matter. The interpretation of the q-char sequence is determined by the implementation.

+11
source

Source: https://habr.com/ru/post/1435883/


All Articles