0

I use Visual Studio on Windows, and I currently use ..\ to specify one folder/directory up when including files. But I was wondering if this is Windows specific because Windows uses the less common backslash as opposed to the forward slash. I can also use ../ , that is, forward slash, and my program compiles the same, but I'm not sure if this is because of the preprocessor standard interpreting this as "one folder up" on whichever platform it's running, or because of the fact that (I'm not sure when) Windows started accepting forward slashes as meaning the same thing as backslashes. I figured that since Windows accepts both, and the more common is the forward slash, I may as well use forward slashes everywhere, and when or if I try to compile on another platform it should work.

I'm specifically asking about C++, but I understand the C preprocessor is the same? Is this the correct way of going about it?

Zebrafish
  • 11,682
  • 3
  • 43
  • 119
  • not clear what you are asking, as you seem to know the answer already – 463035818_is_not_an_ai Sep 19 '17 at 11:20
  • 6
    Always use `/`, that works on most (if not all) platforms including Windows. And `../` is not interpreted by the preprocessor but by the operating system. And this also works on most (if not) all current operating systems. – Jabberwocky Sep 19 '17 at 11:22
  • @tobi Basically if use ..\ in Visual Studio and it works, will this work on another platform? Or will I have to change it to ../ ? When I looked up this question the solution I got was to use ..\ but I don't know if this is Windows specific. – Zebrafish Sep 19 '17 at 11:23
  • 2
    ../ works on windows too, so I use that – doctorlove Sep 19 '17 at 11:30
  • Thanks, that sounds easiest. – Zebrafish Sep 19 '17 at 11:33
  • Possible duplicate of https://stackoverflow.com/questions/5790161/is-the-backslash-acceptable-in-c-and-c-include-directives – roalz Sep 19 '17 at 11:34
  • @MichaelWalz: Actually, the Standard says that the header names using in `#include <>` and `#include ""` **are** interpreted by the preprocessor. It may of course decide to pass it to the OS, but there's nothing stopping a compiler from just going straight to http://github.com/ – MSalters Sep 19 '17 at 11:37
  • @MSalters, OK I didn't know that, but anyway `../` is always correct for specifying the parent directory of the directory containing the .c file beeing currently compiled. Please correct me if I'm wrong. – Jabberwocky Sep 19 '17 at 11:40
  • @MichaelWalz: My comment update raced with your comment, but the "files on the web" example still holds - a webserver might very well reject the `../` notion. – MSalters Sep 19 '17 at 11:41
  • Rather than put the build structure in your code (the wrong place), why not pass the correct directories to the compiler (the right place) from your makefile or build script? – stark Sep 19 '17 at 16:49
  • @stark I see. Unfortunately I know nothing about make files and that sort of thing. I have trouble building libraries that are all set up even. The scripting looks like an entirely different language. I guess I'll learn how to soon. – Zebrafish Sep 19 '17 at 17:12

1 Answers1

0

Windows DOS path processor allows both folder1/file and folder1\file. So the easiest way to get compatible paths is to use ../file.h

All unix systems use '/' as a folder separator, and it solves the issue for most cases.

Having said that I am not sure the C/C++ standard imparts any meaning on the text within the include statement, nor whether it means directories.

mksteve
  • 12,614
  • 3
  • 28
  • 50