包括从 Windows 到 Linux 的标头路径更改
我正在将用 C++ 编写的应用程序从 Windows 移植到 Linux。 我的头文件路径有问题。 Windows 使用 \
,Linux 使用 /
。 我发现在每个源文件和头文件中更改此设置很麻烦。 有什么解决办法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您使用什么版本的 Windows? 据我所知,从 Windows XP 开始,正斜杠实际上可以用作路径分隔符。
What version of Windows are you using? As far as I know, starting with Windows XP, forward-slashes do actually work as path delimiters.
您可以根据之前的 perl 命令尝试此命令,该命令递归地完成工作
You can try this command based on previous perl command which do the work recusrively
Windows API始终支持正斜杠作为目录分隔符。 这是因为至少早在 DOS 3.1 就已经支持它了。 问题一直是COMMAND.COM和CMD.EXE。 他们使用正斜杠作为选项指示符(而不是 Unix 中的破折号)。 在字符串中使用反斜杠转义特殊字符的语言中,切勿使用反斜杠作为目录分隔符。
如果您缺少 Unix shell,您是否没有一个可以跨多个文件搜索/替换的编辑器? 哎呀,如果有必要的话,写一个小程序来做到这一点。 解析 C++ 源代码中的 #include 语句并不困难。
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.
始终在 #include 指令中使用正斜杠。 某些操作系统/编译器需要它,并且 Windows/Visual Studio 足够智能,可以正确处理它。
自从您开始使用 Windows 代码以来,我假设您拥有 Visual Studio。 使用查找和替换对话框并创建一个正则表达式来为您进行替换。 对所有文件运行查找和替换。
例如,
对于搜索表达式和
替换表达式,使用类似以下内容(警告:未经测试)。 对所有文件运行此操作,直到没有进行任何替换。 然后将尖括号切换为引号并重复。
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:
for your search expression and
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.
始终在 #include 路径中使用正斜杠。 编译器的工作是将路径映射到底层操作系统支持的任何斜杠/目录方案。
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.
你们这些人! 是的,您可以而且应该始终使用正斜杠。 我认为问题是如何从这里到达那里!
如果您安装了 Perl,则以下行将转换 C++ 源文件以使用正斜杠,并将原始版本保存在扩展名为
.bak
的文件中:(以上命令行适用于 Windows;如果如果您使用的是 Linux 或其他类 Unix 的 shell,请在第三个参数周围使用单引号而不是双引号。)
如果您有一堆需要转换的文件,请假设所有以
.cpp 结尾的文件
:Bourne shell 环境(典型的 Linux shell)的相应命令:
如果您没有安装 Perl,您应该能够找到一个允许跨文件搜索和替换的文本编辑器。
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
:(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
:The corresponding command for a Bourne shell environment (typical Linux shell):
If you don't have Perl installed, you should be able to find a text editor that allows search and replace across files.