What is a 'shebang' line?

17,233

Solution 1

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:

Solution 2

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.

Solution 3

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;
}
Share:
17,233
James Litewski
Author by

James Litewski

Updated on August 19, 2022

Comments

  • James Litewski
    James Litewski almost 2 years

    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 have no idea what a 'shebang' line is... Can someone help me out?

  • James Litewski
    James Litewski almost 13 years
    I haven't used any other compiler than the one that comes with Visual Studio.. So I have no clue how; also this is my first Mac ever, and I've only had it for about a month. So I'm very unsure of how to do all this..
  • Seth Carnegie
    Seth Carnegie almost 13 years
    @James I've never used a Mac, but maybe this guide will help you to get g++ running on your Mac. Good luck and good choice with C++.
  • James Litewski
    James Litewski almost 13 years
    Thanks, I'll try to see if I can figure this out.. I've used mono before on Ubuntu, I didn't even think to check to see if it was on Mac.
  • sashang
    sashang almost 13 years
    Try installing gcc. You could also install XCode, Apple's IDE, since that will have gcc packed in it.
  • shelleybutterfly
    shelleybutterfly almost 13 years
    you're very welcome! glad to be able to help. :) if you do have any issues you know where to find us! :D ♡
  • molbdnilo
    molbdnilo almost 13 years
    @James: XCode is on one of the DVDs that came with your Mac. If you've misplaced it, register for a free account at developer.apple.com and download an older XCode from there.