终端中的“./”和“open”之间有什么区别,以及如何从 XCode 运行 Unix 可执行文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4694062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between "./" and "open" in Terminal and how could run a Unix executable from XCode?
提问by user575988
I'm new in all these and I would like to excuse me for my ignorance..
我是所有这些的新手,我想原谅我的无知..
I have a unix executable file. I run through terminal with command: open programname --args argument and it responds that can't find the resource file(which is the argument that I'v passed.)
我有一个 Unix 可执行文件。我用命令运行终端:打开程序名 --args 参数,它响应找不到资源文件(这是我传递的参数。)
I tried by writing: ./programname argument and it works.
我尝试编写: ./programname 参数并且它有效。
Firstly, I would like to know the difference between these two methods and why the first one doesn't work, while the second works.
首先,我想知道这两种方法之间的区别,以及为什么第一种方法不起作用,而第二种方法有效。
The problem that I have now is that I can't do it in XCode. When a executable is running, it says that it can't find the resourse file. I passed an argument in executable by cmd+i to executable and adding the resource file as an argument.
我现在遇到的问题是我无法在 XCode 中做到这一点。当一个可执行文件正在运行时,它说它找不到资源文件。我通过 cmd+i 将可执行文件中的参数传递给可执行文件并将资源文件添加为参数。
Thank you for your potential answer..
谢谢你的潜在答案..
采纳答案by Tommy
open
is the exact equivalent of double clicking on a thing in the Finder. So if you use it on a UNIX executable then the executable will launch but the current working directory won't be where the executable is located and won't necessarily be anything useful. However, you could also use it to open a .doc file or any other file type, to open a directory in the Finder, to open a URL in the finder, etc.
open
完全等同于在 Finder 中双击某个事物。因此,如果您在 UNIX 可执行文件上使用它,那么该可执行文件将启动,但当前工作目录不会是该可执行文件所在的位置,也不一定是有用的。但是,您也可以使用它来打开 .doc 文件或任何其他文件类型、在 Finder 中打开目录、在 finder 中打开 URL 等。
Launching an executable with ./name is a normal UNIX way to run the thing. The current working directory will be wherever you are when you type it. So that'll be the same directory as the executable is located in (unless you launch from elsewhere on the disk, e.g. by going down a directory and using ../name to launch the executable).
使用 ./name 启动可执行文件是运行该事物的正常 UNIX 方式。当您键入时,当前工作目录将位于您所在的任何位置。所以这将与可执行文件所在的目录相同(除非您从磁盘上的其他地方启动,例如通过进入目录并使用 ../name 启动可执行文件)。
What you probably want to do is write code to get the path to the executable, and seek the specified file relative to that? You don't say what language you're working in, and annoyingly that's not something that's in the POSIX spec so it varies from OS to OS.
您可能想要做的是编写代码来获取可执行文件的路径,并寻找与该文件相关的指定文件?您没有说明您使用的是哪种语言,而且令人讨厌的是,这不是 POSIX 规范中的内容,因此它因操作系统而异。
If you're in C or anything that can call C (so, C++, Objective-C, etc) then NSGetExecutablePath
(ignore Google's desire to put an underscore before it, see e.g. this site) is quite probably what you want. If you're in Objective-C then NSString
has some methods for automatically appending paths.
如果您使用 C 或任何可以调用 C 的东西(例如,C++、Objective-C 等),那么NSGetExecutablePath
(忽略 Google 在其前面放置下划线的愿望,请参阅例如此站点)很可能是您想要的。如果您使用 Objective-C,则NSString
有一些方法可以自动附加路径。
回答by Dan K.
"open" is a command that works exactly as if you had navigated to a specific application via Finder and then double-clicked it.
“打开”是一个命令,就像您通过 Finder 导航到特定应用程序然后双击它一样。
./ is a way of running a particular file that's marked executable via chmod.
./ 是一种运行特定文件的方式,该文件通过 chmod 标记为可执行文件。
For example, you can run a Mac OS X application from the command line by entering "open /Applications/MyApp.app/". But it won't work if you do ./MyApp.app/ (assuming you're in the /Applications directory).
例如,您可以通过输入“open /Applications/MyApp.app/”从命令行运行 Mac OS X 应用程序。但是,如果您执行 ./MyApp.app/(假设您在 /Applications 目录中),它将无法工作。
If you type "man open" into your terminal, you can read more details there.
如果您在终端中输入“man open”,您可以在那里阅读更多详细信息。