C++ compiles but gives error when executed - c++

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!

Related

Notepad++ NppExec console warning, need explanation "C++"

I've tried using Notepad++ to code c++ and followed a few tutorials on youtube, here's what I did:
-Installed gcc/g++ compiler using mingw64
-Installed NppExec plugin on N++
-Typed in the following compilier script and saved as C++:
NPP_SAVE cd $(CURRENT_DIRECTORY) g++ $(FILE_NAME) cmd /c $(CURRENT_DIRECTORY)\program.exe
Anyways whenever compiling a program, for example a simple program
#include <iostream>
using namespace std;
int main(){
cout << "Online\n";
system("pause"); //So that cmd doesn't disappear immeadiately on running.
return 0;
}
The console displays the following warning:
"C:\Users\pc\Desktop\Courses\Projects\C\program.exe' is not recognized as an internal or external command, operable program or batch file."
My question is, When I run the program on cmd, it runs perfectly but the error displayed during linking says that the folder does not exist in %PATH%
Any explanation?
Thank you!
Ok so, what I basically did was change the script,
cmd /c $(CURRENT_DIRECTORY)\program.exe
To be later
cmd /c $(CURRENT_DIRECTORY)\a.exe
the console worked fine and even received input
Here is a link to a similar problem:
How to compile/execute C++ code from within Notepad++

Compiling C++ file at command line on Windows using MinGW

I wrote the following on a notepad and save it as hello_world.cpp in the following folder: C:\cplusplus
#include <iostream>
main(int argc, char* argv[])
{
std::cout << "Hello World\n";
return 0;
}
I brought up cmd.exe and executed the following code to compile the cpp file.
C:\Program Files (x86)\CodeBlocks\MinGW\bin>g++.exe C:/cplusplus/hello_world.cpp
-o hello_world.o
However, I got the following error message:
g++.exe: error: CreateProcess: No such file or directory
I wonder if someone could point out what I did wrong.
Thank you!
I think the problem is "/" which is used in Linux. In Windows is "\".
Try:
C:\Program Files (x86)\CodeBlocks\MinGW\bin>g++ c:\cplusplus\hello_world.cpp
-o hello_world.o
To run your C++ code in Visual Studio Code using the MingW g++ compiler, you can simply use the following steps:
Make sure you are in the same directory you save your C++ file
Next, use this command to create the executable file of your code:
g++ -o hello_world hello_world.cpp
Here, the "hello_world" will be the name of your executable file which you wish to provide and "hello_world.cpp" is the name of your actual C++ source file.
This will create your executable file, then finally use the command:
./hello_world
This will run your executable file.
The instructions do say to re-login after setting MingGW up. I'd reboot, just for good measure.

how to compile c++ program in mac terminal

i wrote a program in my mac using sublime text as the plateform..
#include<iostream>
using namespace std;
int main()
{
cout<<"HELLOW WORLD";
return 0;
}
this was my program..
i saved it in desktop as hellow.cpp
while compiling on mac terminal as g++ hellow.cpp, i found an error
adarshs-MacBook-Air:Desktop adarshak$ g++ hellow.cpp
xcrun: error: invalid active developer path
(/Library/Developer/CommandLineTools), missing xcrun at:
/Library/Developer/CommandLineTools/usr/bin/xcrun
anyone plese help me to find out the error
It sounds like you don't have the command line developer tools installed. Run this command from Terminal once:
xcode-select --install
This will bring up the download & installation UI. Follow this through to the end. (It may take a while depending on the speed of your internet connection.)
From then on compiling should work.

C++ Mingw32 CreateProcess() failed with error code 2: The system cannot find the file specified

I am just trying to run a basic program in notepad++ and mingw32. I have attempted multiple different thing but I continue to get.
Current directory: \\THEBOX\Users\jacks_000\Documents
C:\MinGW\mingw32\bin\g++.exe -g "testpgrm"
CreateProcess() failed with error code 2:
The system cannot find the file specified.
================ READY ================
When I run the nppexec I use the following
NPP_SAVE
CD $(CURRENT_DIRECTORY)
C:\MinGW\mingw32\bin\g++.exe -g "$(FILE_NAME)"
I have also tried:
NPP_SAVE
CD $(CURRENT_DIRECTORY)
C:\MinGW\bin\g++.exe -g "$(FILE_NAME)"
I am just using a basic test program:
#include <iostream>
using namespace std;
int main()
{
cout<<"Hi";
return 0;
}
I don't know if I will have issue running it in the command prompt if I save it this way or if I have done something wrong. I am running Windows 10 if that is a issue.
Current directory: \THEBOX\Users\jacks_000\Documents
I think it's because g++ can't access to a SMB share.
Try to compile the file locally.
The problem is with the location of "C:\MinGW\mingw32\bin\g++.exe". Where it is on your PC, and what is the actual filename, will depend on your installation.
For example, on my machine I have and old version in "C:\Program Files (x86)\CodeBlocks13_02\MinGW\bin\mingw32-g++.exe", but a newer installation in another folder.
So you need to find the executable name and location of the compiler. You won't need to use the top two lines, just "C:\Program Files (x86)\CodeBlocks13_02\MinGW\bin\mingw32-g++.exe -g test.c", for example.
I did this and it gave a result of :
C:\Program Files (x86)\CodeBlocks13_02\MinGW\bin\mingw32-g++.exe -g test.c
Process started >>>
<<< Process finished. (Exit code 0)
================ READY ================

Syntax error: "(" unexpected

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