pearbear
pearbear

Reputation: 1055

Why would I get a syntax error near unexpected token? Command line arguments?

Here is the code I have- not sure why I am getting this error message:

$ ./main.cpp "hello" "is"
./main.cpp: line 4: syntax error near unexpected token `('
./main.cpp: line 4: `int main(int argc, char *argv[]){'

It compiles fine in g++, but when I run it, I get the above error. Any idea why? Here is my complete code..

#include <iostream>
#include <fstream>

int main(int argc, char *argv[]){

    for(int i = 0; i < argc; i++){
        std::cout << argc << " : " << argv[i] << '\n';
    }

    if (argc != 2){
        std::cout << "\nUSAGE: 2 command line arguments please." << std::endl;
        std::cout << "\n   (1) Input file with raw event scores.\n   (2) Output file to write into.";
    }

  // open the font file for reading
    std::string in_file = argv[1];
    std::ifstream istr(in_file.c_str());
    if (!istr) { 
        std::cerr << "ERROR: Cannot open input file " << in_file << std::endl;
  }

    return 0; 
}

Upvotes: 4

Views: 16793

Answers (3)

Keith Thompson
Keith Thompson

Reputation: 263507

As both the other answers say, you're running it as a shell script, implicitly using /bin/sh.

The first two lines starting with # are treated by the shell as comments. The third line is blank, and does nothing. The fourth line is interpreted as a command int, but parentheses are special to the shell, and are not being use correctly here. There probably isn't an int command in your $PATH, but the shell doesn't get a chance to report that because it chokes on the syntax error.

None of these details are particularly important; the problem is that you're executing the program incorrectly. But it might be interesting to see why those specific error messages are printed.

And it appears that you've done something like chmod +x main.cpp; otherwise the shell would have refused to try to execute it in the first place. Making a C++ source file executable isn't going to cause any real harm (as long as it's readable and writable), but it's not at all useful, and as you've seen it delayed the detection of your error. If you do chmod -x main.cpp, and then try ./main.cpp again, you'll get a "Permission denied" error instead.

As Carl's answer says, you need to execute the executable file generated by the compiler, not the C++ source file. That's why there's a compiler. The compiler (well, actually the linker) will automatically do the equivalent of chmod +x on the executable file it generates.

The file command will tell you what kind of file something is, which affects what you can do with it. For example, using your code on my system, after running g++ main.cpp -o main:

$ file main.cpp
main.cpp: ASCII C program text
$ file main
main: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=0xacee0dfd9ded7aacefd679947e106b500c2cf75b, not stripped
$ 

(The file command should have recognized main.cpp as C++ rather than as C, but sometimes it guesses wrong.)

"ELF" is the executable format used by my system; the file contains executable machine code plus some other information. The pre-installed commands on the system use the same format.

The details may differ on your system -- and will differ substantially on non-Unix-like systems such as MS Windows. For example, on Windows executable files are normally named with a .exe extension.

Upvotes: 3

Eric
Eric

Reputation: 843

The compiler, by default, creates an executable called "a.out", so you want to do:

$ a.out "hello" "is"

Typing "./main.cpp" is trying to execute the C++ source file, probably as a shell script

Upvotes: 0

Carl Norum
Carl Norum

Reputation: 225072

You have to run the compiled program, not the source code:

$ g++ -o main main.cpp
$ ./main "hello" "is"
3 : ./main
3 : hello
3 : is

USAGE: 2 command line arguments please.

   (1) Input file with raw event scores.
   (2) Output file to write into.ERROR: Cannot open input file hello

Your example is trying to execute C++ code as a shell script, which isn't going to work. As you can see from the output of my test run of your program here, you still have some bugs to work out.

Upvotes: 6

Related Questions