This might be a dumb question but...
I am programming some stuff in C++, it compile well on g++, but when I start the binary, there is nothing printed, even if I redirect the output in a file.
Example:
print.cpp
#include <iostream>
using namespace std;
/*...*/
int main ()
{
//Table tab;
//tab.set_all('_');
//tab.setc(1, 1, 'c');
//tab.setc(10, 5, 'd');
cout << "print" << endl;
//tab.print();
cout << "end" << endl;
return 0;
}
In shell:
>g++ print.cpp -o print
>print
>print > t
>cat t
>
Is it a problem in my code, or do I start my program in the wrong way?
By typing print in your shell you are executing the print command, which is a built-in of your shell that prints nothing without any arguments.
To launch your binary, type ./print. This solves the confusion between the print command and the binary print in the current directory.
If you just call
> print
you are actually executing /usr/bin/print, that from the man page is
NAME
run-mailcap, view, see, edit, compose, print - execute programs via entries in the mailcap file
Tu run your code you should do one of these three things:
If from the same directory
> ./print
From an other directory
> /path/to/exe/print
Add the directory where the exe live (/path/to/exe/) in the PATH before /usr/bin
> export PATH=/path/to/exe:$PATH
> print
If you want to add it permanently, just add export PATH=/path/to/exe:$PATH to you ~/.profile file
print is the name of a program from mailcap package. Typing print into the shell and hitting the Return key will execute it (from /usr/bin/print). Start your program by typing ./print.
Related
I have a project in which I need to read a .txt file automatically through program arguments. I have tried typing "< input.txt" into the arguments tab but it does not seem to be reading it. I wrote a simple code to test this quicker and that still doesn't work. If anyone else knows how to solve this problem that would be great, thanks.
#include <iostream>
using namespace std;
int main() {
int r, c;
cin >> r >> c;
cout << r << " " << c << endl;
return 0;
}
photo of directory
[
It is enough complected task in Xcode 9. Suppose input.txt is the file that we need to pass to the standard input of our program like prog < input.txt.
The definitions of Xcode build environment variables are explained in How do I print a list of "Build Settings" in Xcode project?
Open your project Build Phases. Press + there to add new Run Script task. Enter the command bellow. It expects input.txt is placed in the same directory where the program executable is built, see the value of ${BUILT_PRODUCTS_DIR} in the build log.
echo "${BUILT_PRODUCTS_DIR}/${FULL_PRODUCT_NAME} < ${BUILT_PRODUCTS_DIR}/input.txt" > ${BUILT_PRODUCTS_DIR}/${FULL_PRODUCT_NAME}.sh
2. Add new scheme and edit it. For the Run task goto Info pane. Select Terminal.app in the Executable and uncheck Debug executable.
Goto Arguments pane. Press + and enter the command into the new argument
${BUILT_PRODUCTS_DIR}/${FULL_PRODUCT_NAME}.sh
Finally build and run your application. New Terminal app must start and you must see like below there (it is from your code snipped)
I am having trouble executing my C++ code. I have written a basic "Hello World" program, and compiled it using the g++ make command. Here is my code:
#include <iostream>
using namespace std;
int main() {
cout << "Hello World" << endl;
return 0;
}
I am on Windows 10, using Emacs for code editing, and CygWin for compilation. I saved this file as hello.cpp. I then navigated to the directory in CygWin. Then I did the command make hello. This created hello.exe. Then, I attempted to execute the file using ./hello.exe. I also tried ./hello which also didn't work. When I type one of these commands and hit Enter, it just on the next line, not doing anything. I can type in this blank line, but it won't do anything. Does anyone know a way to make my code execute properly. Thank you.
EDIT: I tried running this at cpp.sh, an online C++ compiler, and it worked fine.
Your program probably is working but the console window is closing before you can see anything.
Try adding an input at the end of the program so it will wait.
I.E.
int a;
cin >> a;
Your code is most likely executing, but not outputting anything. That's because it's failing. Try checking the return value after it has run with echo $?. If it's not 0 then it has crashed. Also run it in gdb and see if it fails. The reason why it's failing is most likely a windows/cygwin clash - it's not your code.
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
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;
}
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.
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.
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
The "Go" function is Scite is giving me the following error
"'.' is not recognized as an internal or external command,
operable program or batch file."
Is the '.' trying to relate to a path?
I have no trouble compiling and building. The exe file built works too. I just used a simple hello world code:
int main()
{
cout << "Hello " ;
return 0;
}
Thanks for any help.
This is because scite is using './' before the generated output file to execute the program and this is what we normally do to execute the programs in 'terminal' (linux). however, this is not required in windows and we just specify the output name and hit enter to execute '.exe' files.
you need to open cpp.properties in option menu and lookup the following:
# C++ styles
under the comment
# Braces are only matched in operator style
edit the line
command.go.*.c=./$(FileName)
to remove './'. Make it
command.go.*.c=$(FileName)
Again repeat the same thing below the following comment:
# To make the Go command both compile (if needed) and execute, use this setting:
#command.go.needs.*.c=gcc $(ccopts) -std=c99 $(FileNameExt) -o $(FileName)
Change
command.go.*.c=./$(FileName)
to
command.go.*.c=$(FileName)
'make' setting
If you are using mingW-gcc then lookup for the 'make' program in mingW-gcc installation folder. That should be 'mingw32-make'. Below the comment:
# Braces are only matched in operator style
Change
make.command=make
to
make.command=mingw32-make