Cannot run g++ compiled program Ubuntu - c++

I created a simple program
#include <cstdio>
int main(){
printf("Hello world!");
return 0;
}
and compiled it with CodeBlocks 13.12 on Ubuntu 16.04, successfully
I am trying to run it from bash nothing happens
What am i doing wrong ?

test is an intrinsic bash shell command, so instead of calling your executable program you're calling this one.
To call you own executable test refer to the local binary in the directory it was created. Either ./test, or ~/Programs/test/bin/Release/test.

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++

Hello World C++

Every time I try and run the Simple Hello World program in C++, I build it and it says nothing to build for FirstProject. The code looks correct, I'm using MinGW as my compiler, etc. Every time I try to run the program, instead of printing the output, it just terminates. Anyone have a clue?
#include <iostream>
using namespace std;
int main() {
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
return 0;
}
try these commands:
g++ -o hello_world.exe hello_world.cpp
./hello_world.exe
MinGW works same as gcc on linux so all commands which work on linux should work on MinGW
In cygwin, the following steps should work.
Save the contents to file HelloWorld.cc.
Go to the directory where you saved the file.
Execute make HelloWorld
Execute ./HelloWorld.exe
If that doesn't work, something is really not right.
Are you running it from the command line or an IDE? Sometimes an IDE will open a terminal and close it too fast for you to see, or you'll just see a flash. Try running it from the command line so you'll be able to see if it printed anything before exiting the program.
Tried and tested using MinGW in windows 10 command prompt.
#include <iostream>
using namespace std;
int main()
{
cout<<"Hello, World!"<<endl;
system("pause");
return 0;
}
Image of command prompt output
Save the contents to file HelloWorld.cpp
Go to the directory where you saved the file.
Execute make HelloWorld
Execute ./HelloWorld.exe

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

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++ using system command problem

hi every body i have a strange problem i write a code in c++ complied it successfully and run it successfully. i compiled with following command
g++ 1.c -o abc
to run program i use ./abc
now my problem is that i write a another code in c++ like
#include <fstream>
#include<iostream>
using namespace std;
int main()
{
ofstream SaveFile("/home/hadoop/project/hadoop-0.20.0/conf/core-site2.xml");
SaveFile <<"<configuration>";
SaveFile<<endl;
SaveFile<<"<property>";
SaveFile<<endl;
savefile.close();
return 0;
}
now i want to run abc in this code how to do this ?
how to use or run abc in this file?
how to use ./abc in this program ?
Actually, your question title ("... using system ...") says it all. Use:
system ("./abc");
to run the ./abc program.
There are other ways to run programs from within a program (which usually depend on platform-specific features) but this is the most portable.
A full sample program, testprog.cpp, to show what I mean:
#include <cstdlib>
int main (void) {
std::system ("ls -ald m*");
return 0;
}
Compiling this with:
g++ -Wall -Wextra -pedantic -o testprog testprog.cpp
and running the resultant executable testprog, this outputs (on my Ubuntu 10.04 box):
drwxr-xr-x 2 pax pax 4096 2010-12-14 09:33 myfolder
In other words, it runs the ls -ald m* command from within the program itself.