I have a C++ application which is build statically against libcurl. The application compiles perfectly, however when I execute the application it returns nothing on the console. Neither it shows anything when I redirect the output to e.g. > output.txt.
The code is just:
int main(void) {
cout << "Show me" << endl; //Or not... :(
return 0;
}
When executed it just starts the console and keeps black, nothing is happening.
Related
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.
I've never had trouble before, but my current C++/Xcode (7.0.1 on El Capitan) project refuses to print anything to cout (or cerr)--even a simple "Hello" (with endl) as the first line in the main program.
The debugger indicates that the program is running, it's just not outputting anything. It compiles and builds with no errors.
I tried creating a new "Hello, world" project and it works, and all my old projects work, so what could explain this one's failure?
Try the activate Console command: Cmd+Shift+C or in the menu: View, Debug Area, Activate Console.
I am using Netbeans 6.8 and trying to run a simple application, here is my code:
#include <iostream>
int main()
{
std::cout<< "Game Over!" << std::endl; // Displays "Game Over" Output
return 0;
}
Netbeans says there are no errors and it cleans and builds just fine but when I click Build/Run it seems to get stuck. I literally went to the supermarket and returned to find that my program still hadn't run.
Any ideas on what the problem could be and how to fix it would be a big help, thanks.
I doubt it's the program cause this. Try finding the executable and running in on the command line and seeing if it hangs.
I currently have 2 virtual machines running, one is a server and one is a client. They are both running Ubuntu. I created a C++ program to connect to a MYSQL server. When i open up a terminal in the server VM, the c++ program compiles and runs PERFECTLY!
BUT, when I try to run it on the client, it wont work at all. The code compiles perfectly fine but when i type "./main" to start the program, NOTHING comes up. It's just a blank screen. I totally do not get why this is happening. The code is exactly the same on both the client and the server, but for some reason, it wont display ANY of my code when I run it on the client side. Below are pictures to show what i mean
Here is a link to view a screenshot of what I am talking about (http://imgur.com/a/QqgPV). In the first picture, I compile the program, which compiles fine. I then run the program but NOTHING outputs to the screen.
The second picture shows the program being ran on the server and shows what is suppose to be output on the screen after the initial ./main command
What should I do to find out what's going wrong?
Compile with "-g -ggdb" and run it in a debugger:
$ cgdb --args ./main your_args
When it blocks pres Ctrl-C and type "bt". That will tell you where the program stopped.
There might be a gazillion things blocking your code (you use external stuff, like the db).
Give us some code to analyze. We can't help without specifics.
BTW: The fact that it compiles doesn't mean it will run flawlessly.
Either add logging to your program or run it under strace to figure out where it's getting stuck or what it's waiting for.
At the most basic level, you add logging by adding log statements. Say your code looks like this:
a();
b();
c();
output_stuff();
Temporarily change it to:
cout << "about to do a" << endl;
a();
cout << "about to do b" << endl;
b();
cout << "about to do c" << endl;
c();
cout << "done with c" << endl;
output_stuff();
Then see what messages you get. If you don't get "about to do a" then it's a constructor for a global object or process initialization that's hanging. If you get "about to do a" but not "about to do b", then it's a() that's hanging. And so on.
Build your program with debug symbols enabled (GCC -g option)
$ cat infinite.cpp
void infinite()
{
for(;;)
{
}
}
int main()
{
infinite();
}
$ g++ -g infinite.cpp
Run your program:
$ ./a.out
Open new terminal session
Find out what is PID of your program
$ ps x | grep a.out
7817 pts/3 R+ 0:08 ./a.out
Run gdb and attach it to your program (sudo if you get permission error):
$ gdb
...
(gdb) attach 7817
Attaching to process 7817
...
Loaded symbols for /lib64/ld-linux-x86-64.so.2
infinite () at infinite.cpp:3
3 for(;;)
(gdb) backtrace
#0 infinite () at infinite.cpp:3
#1 0x0000000000400513 in main () at infinite.cpp:10
Use the debugger to inspect at what point of execution is your program, set breakpoints, inspect state of your program (perhaps one of loop counters is unsigned integer and overflows, etc.)
I want to write a C++ program in Xcode on my Mac. I wrote this basic one to test it. When I build, I get a "Build Successful" message.
However, I cannot run it! If I hit Command+R nothing happens. When I go to Project->Run the Run option is disabled.
#include <iostream>
using namespace std;
int main()
{
cout << "helo" << endl;
}
Launch XCode
In the "Choose template" box, pick Mac OS X, then Command Line Tool. Press Next
Give your project a name, select C++ as the type
You should see a new project with main.cpp
press the Run button
At the bottom of the screen, under All Output you should see:
Hello, World!
Program ended with exit code: 0