Syntax error: “(” unexpected

2020-04-16 05:56发布

问题:

I am trying to compile code using gcc and run the executable, but it is throwing error:

gcc somefile.c -o somefile

compilation goes through successfully. But, when I try to execute it:

$sh somefile

It results in: Syntax error: "(" unexpected. Among the output files, I dont see somefile.o, but instead, I see somefile.c~

The contents of the file:

#include <stdio.h>
int main(int argc, char *argv[])
{
    printf("hi");
}

Context: I am new to programming in linux, and wanted to start out with simple programs. I am running ubuntu 64 bit on a virtual machine, with gcc, g++, etc installed. After that I created a sample file as mentioned above ("somefile.c"), and tried the steps mentioned above, but could not execute. My goal is to compile and execute a sample C or Cpp code on ubuntu using gcc or g++. Please help.

回答1:

your somefile is executable binary, it's not shell script. you should execute it by:

$./somefile


回答2:

To execute file you just have to do

$./somefile

sh is used when you've to execute a shell script



标签: c++ c linux gcc