将代码从 Linux 移植到 MinGW
我正在编写一个小类,可以在 PC 上创建/删除/重命名/搜索文件和目录。
我成功编写了该类并在 Linux 上运行。
当我尝试在 MinGW 中运行相同的类代码时,出现错误。 我可以缩小范围: Linux中的mkdir函数,Cygwin有2个参数(目录名,模式权限) 但在 MinGW 中只有一个参数(目录名称)。
我的问题是:a)使代码在两个操作系统上运行的最佳方法是什么。 b) 虽然我从未使用过,但我听说预处理器指令可以像 #ifdefine .....#endif .. 或类似的东西 c) 使用预处理器指令是一种很好的编程实践。据我所知,应尽量少用预处理器指令。
有人可以帮助我吗:
这是我在 Linux 和 Cygwin 上运行的代码:
#include "BioDatabase.h"
#include <dirent.h>
#include <sys/stat.h>
#include <stdio.h>
#include <unistd.h>
BioDatabase::BioDatabase() {
string s = getcwd(NULL,0);
changeDirectory(s,"*");
}
BioDatabase::BioDatabase(string directoryName, string extension)
{
changeDirectory(directoryName, extension);
}
bool BioDatabase::createDirectory(string st)
{
if( mkdir(st.c_str(),0755) == -1)
{
cerr <<endl<<"BOSERR-BioDatabase, createDirectory: Path or file function not found or Permission denied\n\n";
return false;
}
flag =1;
return true;
}
I am writing a small class which can create/remove/rename/search for files and directories on the PC.
I successfully wrote the class and run on Linux.
When I was trying to run the same Class Code in MinGW, it was giving an error.
I could narrow down to:
mkdir function in Linux, Cygwin has 2 Arguments (directory name , mode permissions)
but in MinGW has only one argument(directory name).
My query is : a) What is the best way to make the code work on both OSs. b) Though I never used, I heard Preprocessor directives can be put like #ifdefined .....#endif ..or some thing of that sort c) Is using Preprocessor directives a good programming practice. As I learnt, preprocessor directives should be used minimally.
Could some one help me in this:
Here is my Code which works on Linux and Cygwin:
#include "BioDatabase.h"
#include <dirent.h>
#include <sys/stat.h>
#include <stdio.h>
#include <unistd.h>
BioDatabase::BioDatabase() {
string s = getcwd(NULL,0);
changeDirectory(s,"*");
}
BioDatabase::BioDatabase(string directoryName, string extension)
{
changeDirectory(directoryName, extension);
}
bool BioDatabase::createDirectory(string st)
{
if( mkdir(st.c_str(),0755) == -1)
{
cerr <<endl<<"BOSERR-BioDatabase, createDirectory: Path or file function not found or Permission denied\n\n";
return false;
}
flag =1;
return true;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以编写类似的代码
另请参阅 feature_test_macros(7) 手册页。
You could code something like
See also feature_test_macros(7) man page.
1)您可以使用预处理器在一个平台上做一件事,而在另一个平台上做不同的事情。 EG:
2)是的,你完全正确:尽可能限制使用它们。 但是您很快就会发现您无法完全避免它们。
3)最好的办法是使用一个脚本来检查
功能而不是在每个操作系统的基础上进行。
通常这涉及编写配置脚本(或
类似),这是一个完全不同的学习曲线。尽管如此,它还是让
您通过检查功能而不是检查功能来移植到新平台
将平台添加到一长串列表中。
1) you can use pre-processors to do one thing on one platform, and something different on another. EG:
2) Yes, you're absolutely right: limit using them as much as you can. but you'll quickly find out you can't avoid them entirely.
3) The best thing to do is to use a script that checks for
functionality rather than do it on a per-operating system basis.
Typically this involves writing a configure script (or
similar), which is a whole other learning curve. Still, it lets
you port to new platforms by checking for functionality rather than
adding the platform to a long list.