10

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

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

I have tried these different usage and it all seems to work.

  1. #include "D:\temp\temp_lib\temp.h"
  2. #include "D:\\temp\\temp_lib\\temp.h"
  3. #include "D:/temp/temp_lib/temp.h"

I just want to know which one should I use? I am using MSVC 2005. I'm wondering if all three will still work in Linux or other environment.

I was expecting #1 to be an error during compilation, but I did not get any. Anyone has any idea why that is?

Sumit Singh
  • 15,743
  • 6
  • 59
  • 89
chris yo
  • 1,187
  • 4
  • 13
  • 30
  • 2
    I say you use whichever you feel most comfortable with. And no, _none_ will work in a Linux (or OSX) environment, as no other system than Windows (and DOS) have drive letters. The closest is the one with forward slashes (`/`). – Some programmer dude Sep 24 '12 at 10:08
  • @JoachimPileborg good point. :) `D:/` – Luchian Grigore Sep 24 '12 at 10:13
  • 2
    It is better to determine - why do you need absolute paths. I think if you'll explain us that - we will suggest a solution which will help you to avoid that. And yes - all of them will not work in Linux. – denys Sep 24 '12 at 10:19
  • @denys A perfectly good reason is having a debug header with some debug tool function outside source control, which I only include for debug sessions and do not actually commit. Yet I reuse them and want to be able to put them in&out as fast as possible. Absolute path allows that. – Tomáš Zato Jun 18 '19 at 12:39

1 Answers1

14

Every implementation I'm aware of, and certainly MSVC 2005 and linux, allows you to specify the directory paths in which to find header files. You should include D:\temp\temp_lib on the list of directory paths, and then use

#include <temp.h>

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

The reason that #1 isn't a syntax error is that, although it looks like a string literal, it isn't. The specification is

#include "q-char-sequence"

Where q-char is

any member of the source character set except the new-line character and "

In particular, \ has no special meaning. The interpretation of the q-char-sequence is implementation-defined.

Community
  • 1
  • 1
Jim Balter
  • 16,163
  • 3
  • 43
  • 66
  • 1
    This is good explanation. But I tried this line: #include "D:\temp\new\new_file.h" and still MSVC was able to find new_file.h. I am expecting an error on this since this contains a new-line character (\n). Anyone has any idea why MSVC was able to find the file? I did not set -I path to D:\temp\new in the MSVC also. So, I am confused why MSVC was still able to find the file. – chris yo Sep 25 '12 at 02:30
  • @chrisyo \n is not a newline character ... a newline character is (in ASCII) ctrl-j. \n is an escape sequence that the C compiler *interprets* as a newline character .... but not here; again, "\ has no special meaning". 'why MSVC was still able to find the file' -- because you gave it an absolute path, so it didn't need to look it up on the path list. The reason I recommended using the path list is because you said you want it work in Linux, but your Windows path won't work there. – Jim Balter Sep 25 '12 at 02:44