Syntax error: "(" unexpected - c++

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.

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

To execute file you just have to do
$./somefile
sh is used when you've to execute a shell script

Related

Can not run make command in Dev-cpp application

I have a very simple example named HelloWorld and located in folder "C:/HelloWorld" in Dev-Cpp as follows:
#include <iostream>
using namespace std;
int main(){
cout << "Hello World";
return 0;
}
Of course, if I press F11 (Compile and run) then the program executes normally.
But I want to execute this program in an another way: use make command!!!
I have known that the syntax to run make command : make -f [make file name]. In Dev-Cpp, this command is make -f "Makefile.win". So I jump into HelloWorld folder and type as follows:
The console displays many errors relating to included headers.
How can I run make command without any errors? Thanks in advance.
not so easy with make :)
at first if you use msvc or DevCpp try to find "Export makefile"
at second you need to configure your compilator(s).
at third you need to show what Makefile.win contains.
man:
http://www.gnu.org/software/make/manual/make.html
gcc:
https://gcc.gnu.org/install/binaries.html
All right. There are something wrong with my make command.
The problem is I had installed two versions of gcc in my computer. The HelloWorld example is compiled and built with the latest GCC version.
However, the make command I had type is belonged to the older GCC version. As a result, it leads to many errors.
In order to run make command correctly, I must to use the right make command that is defined in Dev-Cpp (below picture): mingw32-make.exe

Error when running OpenNI 2 class ( gcc 4.7.2 / ubuntu 12.10 )

I'm trying to compile an run a very basic program given below (test.cpp) which calls the OpenNI class. You can see the files and dirs they're in here. Sorry that some characters screws up a little bit in the browser's encoding. I'm using the linux command: tree, if you know a better command tell me and I will update it.
File Structure
I'm following the guide here, see "GCC / GNU Make".
#include < stdio.h >
#include < OpenNI.h >
using namespace openni;
int
main ( void )
{
Status rc = OpenNI::initialize();
if (rc != STATUS_OK)
{
printf("\nInitialize failed\n%s\n", OpenNI::getExtendedError());
return 1;
}
printf("Hello, world!\n");
return 0;
}
Here is what I'm running in the command line to compile it (gcc 4.7.2):
gcc test.cpp -I../OpenNI-2.0.0/Include -L/home/evan/Code/OpenNi/Init -l OpenNI2 -o test
This works fine but when I run ./test I get the following error:
Initialize failed
DeviceDriver: library handle is invalid for file libOniFile.so
Couldn't understand file 'libOniFile.so' as a device driver
DeviceDriver: library handle is invalid for file libPS1080.so
Couldn't understand file 'libPS1080.so' as a device driver
Found no valid drivers in './OpenNI2/Drivers'
Thanks, any help would be much appreciated.
Instructions from your guide says, that
It is highly suggested to also add the "-Wl,-rpath ./" to your linkage command. Otherwise, the runtime linker will not find the libOpenNI.so file when you run your application. (default Linux behavior is to look for shared objects only in /lib and /usr/lib).
It seems you have exactly this problem -- it can not find some libraries. Try to add proper rpath (seems to be /home/evan/Code/OpenNi/Init/OpenNI2/Drivers in your case) to your compilation string.
I had the same issue after compiling this little "Hello World" with Eclipse and trying to run it in the command line.
The "Wl,-rpath=./" thing did not work for me.
As also discussed here it worked for me after setting some env. variables before execution:
export LD_LIBRARY_PATH="/path/to/OpenNI2:$LD_LIBRARY_PATH"
export OPENNI2_DRIVERS_PATH="/path/to/OpenNI2/Drivers"
export LD_LIBRARY_PATH="/path/to/OpenNI2/Drivers:$LD_LIBRARY_PATH"
Somewhere I got the info that the first two lines should be enough but it was the third line which is important. I does also work just with the third line.

using mingw and g++ compiler

I’m trying to run my c++ files off the prompt, but nothing is showing, e.g.
C:\C++\mingw>g++ hello.cpp -o hello.exe
It seems to be bug free but nothing displays, in this case a simple hello to the terminal.
My code is a straightforward
#include <iostream>
using namespace std;
int main()
{
cout << "Hello \n" << endl;
return 0;
}
Of course, the simplest answer "Just run hello.exe" is correct. Here's some additional logic behind:
If you're used to interpreted languages, such as Python or Lua, you might have noticed that you execute them by supplying source file to the executable, as such:
python my_source.py
However, this works because each time you run python command, it reads the source file given, and then interprets it and executes appropriate machine instructions based on the file contents - it interprets the file.
C++, on the other hand, is a compiled language. The execution of g++, which is a compiler, generates said machine code for your platform, and stops there. Next time you don't need the compiler to run your program; every instruction is encoded as the machine code in the .exe file. That's why you can share your .exe file with your friend if he doesn't have a compiler, but he won't be able to execute python script without python environment installed.
g++ hello.cpp -o hello.exe // This command only produce the exe file
The executive file doesn't run automatically. You should run it by yourself.
hello.exe

C++ compiles but gives error when executed

I am new to Linux Ubuntu 11.10 and have basic C++exposure.
I installed the g++ by
sudo apt-get install build-essential
and created a directory cpp in my home directory. I then wrote a program hello.cpp in my cpp directory
#include <iostream>
using namespace std;
int main() {
cout << "Hello !" ; return 0;
}
and compiled using
g++ -W hello.cpp -o hello
The program compiles without any errors/warnings. When I try to execute the file
./hello.cpp
I get error messages:
line 3: using: command not found
line 6: syntax error near unexpected token `('
line 6: `int main() {'
I tried looking at a lot of posts but could not resolve this. I have MS VisualStudio on Windows, but I would rather learn C++ on Ubuntu. Thanks in advance.
I think that the problem is that you're trying to execute the .cpp source file rather than the generated executable. Try running ./hello instead of ./hello.cpp, since hello is the actual executable. The errors you're currently getting are caused by the shell interpreter choking on C++ syntax, since it's trying to run it as a shell script.
Hope this helps!

Problem with running c program on mac?

I'm a total beginner in C programming so please bear with me. I have just started today and wanted to write a short program - well at least a small script that would just print out a line of text. Now here's what I did in order to achieve this:
I downloaded vim text editor and wrote this few lines of code:
#include <stdio.h>
int main(void)
{
printf("This is some text written in C \n");
return 0;
}
I saved it as inform.c and compiled it using "cc inform.c" command.
In the end I got a.out file but when I'm trying to run it says:
-bash: a.out: command not found
Can someone tell what I'm doing wrong here and point me in the right direction? Thanks.
Bash can't find your command because the current directory is not usually in the path.
Try:
$ ./a.out
It's a basic one.
on Mac, you need to specify were your executable is.
when you type a.out, the system look for the command in /usr/bin and other synstem binaries folders.
to be more precise type ./a.out
which basically says : "in this directory, command a.out"
you should also add directly the classical signature of main which is :
int main(int argc, char ** argv);