开发者

What is a 'shebang' line?

Currently I'm trying to start programming on my new Mac. I installed TextWrangler, and chose C++ as my language of choice; since I have some prior knowledge of it, from when I used Windows.

So, I wrote the ever so common "Hello World" program. Although, when I tried to run it, I got an error:

"This file doesn’t appear to contain a valid ‘shebang’ line (application error code: 13304)"

I tried searching the error code to find out how to fix this, but I couldn't find anything.. I ha开发者_如何学Cve no idea what a 'shebang' line is... Can someone help me out?


You need to compile it with a compiler first. I assume you tried to run the source file like ./source but C++ doesn't work this way.

With some compilers however, you can provide a shebang-line as the first line of the source file (the #! is known as shebang or crunchbang, hence the name), like so:

#!/path/to/compiler

So that the shell knows what application is used to run that sort of file, and when you attempt to run the source file by itself, the compiler will compile and run it for you. That's a compiler-dependent feature though, so I recommend just plain compiling with G++ or whatever Macs use to get an executable, then run that.


While I wouldn't recommend it for regular C++ development, I'm using a simple shell script wrapper for small C++ utilities. Here is a Hello World example:

#if 0  // -- build and run wrapper script for C++ ------------------------------
TMP=$(mktemp -d)
c++ -o ${TMP}/a.out ${0} && ${TMP}/a.out ${@:1} ; RV=${?}
rm -rf ${TMP}
exit ${RV}
#endif // ----------------------------------------------------------------------

#include <iostream>

int main(int argc, char *argv[])
{
  std::cout << "Hello world" << std::endl;
  return 0;
}


It does appear that you are trying to run the source file directly, however you will need to compile using a C++ compiler, such as that included in the gcc (GNU Compiler Collection) which contains the C++ compiler g++ for the Mac. It is not included with the Mac, you have to download it first:

from http://www.tech-recipes.com/rx/726/mac-os-x-install-gcc-compiler/ : "To install the gcc compiler, download the xcode package from http://connect.apple.com/. You’ll need to register for an Apple Developer Connection account. Once you’ve registered, login and click Download Software and then Developer Tools. Find the Download link next to Xcode Tools (version) – CD Image and click it!"

Once it's installed, if you are going for a quick Hello World, then, from a terminal window in the directory of your source file, you can execute the command g++ HelloWorld.cpp -o HelloWorld. Then you should be able to run it as ./HelloWorld.

Also, if you're coming from a Visual Studio world, you might want to give Mono and MonoDevelop a try. Mono is a free implementation of C# (and other languages), and MonoDevelop is an IDE which is very similar to Visual Studio. MonoDevelop supports C# and other .NET languages, including Visual Basic .NET, as well as C/C++ development. I have not used it extensively, but it does seem to be very similar to VS, so you won't have to learn new everything all in a day. I also have used KDevelop, which I liked a lot while I was using it, although that's been a while now. It has a lot of support for GNU-style development in C/C++, and was very powerful as I recall.

Good luck with your endeavors!

Links:

  • Mono: http://mono-project.com/Main_Page
  • MonoDevelop: http://monodevelop.com/
  • KDevelop: http://kdevelop.org/


shebang is http://en.wikipedia.org/wiki/Shebang_%28Unix%29.

not sure why your program is not running. you will need to compile and link to make an executable.


What I find confusing (/interesting) is C++ program giving "Shebang line" error. Shebang line is a way for the Unix like operating system to specify which program should be used to interpret the rest of the file. The shebang line usually points to the path of the interpreter. C++ is a compiled language and does not have interpreter for it.

To get the real technical details of how shebang lines work, do a man execve and get that man page online here - man execve.


If you're on a mac then doing something like this on the commandline:

g++ -o program program.cpp

Will compile and link your program into an executable called program. Then you can run it like:

./program

The reason you got the 'shebang' error is probably because you tried to run the cpp file like:

./program.cpp

And the shell tries to find an interpreter to run the code in the file. Because this is C++ there is no relevant interpreter but if your file contains Python or Bash then having a line like this

#!/usr/bin/python

at the 1st line in your source file will tell the shell to use the python interpreter


The lines that start with a pattern like this: #!/.../.../.. is called a shebang line. In other words, a shebang is the character sequence consisting of the characters number sign and exclamation mark (#!).In Unix-like operating systems, when a text file with a shebang is used as if it is an executable, the program loader mechanism parses the rest of the file's initial line as an interpreter directive. The loader executes the specified interpreter program, passing to it as an argument the path that was initially used when attempting to run the script, so that the program may use the file as input data.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜