开发者

Running an executable file on Ubuntu

I am trying to run a C/C++ program on Ubuntu.

So at first g++ didn't work at all in the shell, so I installed it using the command

sudo aptitude update && sudo aptitude install g++

so it installed it, and when I wrote the most simplest program it compiled

g++ -Wall a.cpp -o d

But when i tried to execute it just wrote "d" on the command line and states that the comman开发者_运维百科d is not found, but if I type ls in the shell it shows me that there is an executable file named d

Glad if someone can clear things out for me


You have to use ./d (if you are in the directory) or the complete path of your executable.

When you type "d", your OS looks automatically into the directories of $PATH. You can figure out what is the complete path of any executable (like ls or rm) with the command "which".

If it don't work check that the file is executable ("x" flag) and if needed, just add it with

chmod +x file


Probably because the current directory is not in your path. Try

$ ./d


When executing a file that isn't in one of the normal bin directories (just think of it as something that you didn't install from a package or that didn't come from the OS) you need to supply the path to the file.

In this case, what you want is ./d


Could also be rights problem, you may need to set the file to be executable. See chmod command.


You need to give the command line the full path to your file so that it knows exactly where it is. You can do this by using the . prefix, which is shorthand for "the path to the current directory". Therefore, if you are in the same directory as the d file, you should be able to type:

./d

And this should run your program.


I don't like the solution I will propose now, but I do give it to some friends that are used to AIX or Windows command prompt (where you don't have to use ./) and changed to some Linux flavor:
- Add "./" to your $PATH environment variable (I'd put it in the end, but it's up to you to choose its priority). You'll then be able to just use "d" anytime.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜