I'm porting an application written in C++ from Windows to Linux. I have a problem with the header files path. Windows uses \
and Linux uses /
. I am finding it cumbersome to change this in each and every source and header file. Is there some work around?

- 13,850
- 9
- 56
- 64
-
14When you finally fix that, remember: NEVER EVER use backslashes. Forward slashes work both in Windows and Linux. – R. Martinho Fernandes Feb 21 '09 at 17:46
6 Answers
Always use forward slashes in #include paths. It is the compiler's job to map the path to whatever slash/directory scheme the underlying OS supports.

- 50,331
- 10
- 105
- 169

- 31,821
- 7
- 55
- 59
-
16-1. IMHO you're answering the wrong question -- the guy already has a pile of files with backslashed #include paths. He needs an ambulance, not someone to tell him he shouldn't have driven so fast. – j_random_hacker Jun 16 '09 at 09:09
-
Good question! I guess he meant that he wants to avoid continually having to swap between / and \ as he moves between the projects. (OP could you clarify please?) Dropped my -1 since obviously the OP's opinion of what the OP wants outranks mine :) – j_random_hacker Jun 17 '09 at 12:15
-
1@j_random_hacker They're porting (at least, he was when the question was written) so they're still driving and are yet to crash. Flipping the direction of slashes in their files is a single perl statement. – Sep 08 '17 at 10:44
You people! Yes, you can, and should, always use forward slashes. I think the issue is how to get there from here!
If you have Perl installed, the following one liner will convert a C++ source file to use forward slashes, saving the original version in a file with extension .bak
:
perl -i.bak -pe "tr!\\!/! if /^\s*#\s*include\b/" myfile.cpp
(The above command line is for Windows; if you're using Linux or other Unix-like shell, use single quotes around the 3rd parameter instead of double quotes.)
If you have a bunch of files you need to convert, say all files ending in .cpp
:
for %f in (*.cpp) do perl -i.bak -pe "tr!\\!/! if /^\s*#\s*include\b/" %f
The corresponding command for a Bourne shell environment (typical Linux shell):
for f in *.cpp; do perl -i.bak -pe 'tr!\\!/! if /^\s*#\s*include\b/' $f; done
If you don't have Perl installed, you should be able to find a text editor that allows search and replace across files.

- 50,331
- 10
- 105
- 169
-
1You might want to use a different separator than ! if you are using tcsh. – Mr.Ree Feb 21 '09 at 19:37
-
@mrree: Good point. Also you will need to change the syntax of the for loop somewhat (I forget the exact syntax unfortunately). – j_random_hacker Feb 21 '09 at 19:50
The Windows APIs has always supported forward slash as directory separator. And that is because as far back as at least DOS 3.1 it was supported as well. The problem has always been COMMAND.COM and CMD.EXE. They use forward slash as the option indicator (instead of dash as found in Unix). Never ever use backslashes for directory separators in languages where a backslash in a string is used to escape special characters.
If you lack a Unix shell, don't you have an editor that does search/replace across multiple files? Heck, write a small program to do it if you have to. Parsing C++ source code for its #include statements can't be hard.

- 18,754
- 7
- 41
- 61
-
2It has not Always supported forward slash that was a big problem for ages. But it does now. – Martin York Feb 21 '09 at 21:18
-
2
-
1joe: definitely not true. The compilers may have supported it for includes, and the Explorer UI certainly did. But WinAPI in general didn't. – Konrad Rudolph Feb 22 '09 at 13:54
Always use forward slashes in #include directives. Some operating systems / compilers require it, and Windows / Visual Studio is smart enough to handle it correctly.
Since your starting with Windows code, I'm assuming that you have Visual Studio. Use the find and replace dialog and create a regular expression that will do the substitution for you. Run the find and replace on all files.
For example, use something like this:
#include:b+<{[^\\\>]}\\
for your search expression and
#include <\1
for the replace expression (warning: untested). Run this on all files until no replacements are made. Then switch the angled brackets to quotes and repeat.

- 109,094
- 6
- 73
- 101
What version of Windows are you using? As far as I know, starting with Windows XP, forward-slashes do actually work as path delimiters.

- 530,221
- 131
- 937
- 1,214
-
2
-
actually the application is coded for different platforms. And I am using the files coded by someone else. I am interested to know whether there is an easier way of changing the backslashes to forward slashes or get some similar effect. – Feb 21 '09 at 17:45
You can try this command based on previous perl command which do the work recusrively
find . -type f -name \* | xargs grep '#include' | grep '\\' |awk -F: '{print $1}'|sort| uniq | xargs -n1 perl -i.bak -pe 'tr!\\!/! if /^\s*#\s*include\b/'

- 91
- 1
- 4