I am not able to use stdio combined with virtual functions (Windows Vista, Cygwin, GCC 4.8.2)
#include <stdio.h>
class A
{
public:
// If I make g() as virtual, stdio doesn't print.
virtual void g() {}
};
int main()
{
A a; // Or I could remove this object to make stdio print.
printf("testing\n");
}
With hint by "Borgleader", I found that it works when used with "-O2" option. Linking seems to be different when used with/without "-O2".
/home/user> g++ test.cc
/home/user> ./a.exe
/home/user> ldd ./a.exe
ntdll.dll => /cygdrive/c/Windows/system32/ntdll.dll (0x77540000)
kernel32.dll => /cygdrive/c/Windows/system32/kernel32.dll (0x75790000)
cygwin1.dll => /usr/bin/cygwin1.dll (0x61000000)
cyggcc_s-1.dll => /usr/bin/cyggcc_s-1.dll (0x6bf40000)
cygstdc++-6.dll => /usr/bin/cygstdc++-6.dll (0x6c480000)
/home/user> g++ -O2 test.cc
/home/user> ./a.exe
testing
/home/user> ldd ./a.exe
ntdll.dll => /cygdrive/c/Windows/system32/ntdll.dll (0x77540000)
kernel32.dll => /cygdrive/c/Windows/system32/kernel32.dll (0x75790000)
cygwin1.dll => /usr/bin/cygwin1.dll (0x61000000)
Thanks for selbie's suggestion to try GDB. Now things are a bit more clear. I should forget about printf, the program doesn't even load! With option "-O2", it works ok (even in GDB).
/home/user> g++ -g test.cc
/home/user> gdb ./a.exe
<<Edited for brevity>>
(gdb) break main
Breakpoint 1 at 0x40119e: file test.cc, line 14.
(gdb) run
Starting program: /home/user/a.exe
[New Thread 4888.0x13e0]
gdb: unknown target exception 0xc0000139 at 0x77549cfc
During startup program exited with code 0xc0000139.
Another hint that the program was not loading is that, even if I put sleep(100) in it, it exists immediately.
Summary: The problem got solved after updating the entire cygwin installation
Details: Reinstall of just g++ compiler did not help. Updating just g++ compiler to new version did not help. Updating the entire Development category did not help. Updating Base category did not help. Updating the entire cygwin installation helped. It remains unknown as per why the previous install was still working with "-O2" option. I could have continued to use "-O2" option. But, I chose to do complete update of cygwin.
What I did was to update and reinstall (erase & download) many times Cygwin, but at the end it turned out that I had to downgrade my g++ compiler from 5.2 to 4.9. I suppose that this has something to do with a specific version of Cygwin C++ runtime.
Nothing in this code that should not work.
You can switch to VirtualBox and run gcc on Linux VM, this will be more robust than cygwin and other .exe stuff which is modelling Linux on Windows.
Seems that now you are testing cygwin implementation of compiler.
Related
I am new to C++ and want to print out a double value. It is not that I actually need to print that value, I just want to know what is going wrong here.
This is my code (HelloWorld.cpp):
#include <iostream>
int main() {
double i = 5.5;
std::cout << i << std::endl;
return 0;
}
Executing this with the debugger attached results in the following error:
Thread 1 hit Breakpoint 1, main () at src/HelloWorld.cpp:4
4 double i = 5.5;
Thread 1 received signal SIGSEGV, Segmentation fault.
0x00000000929ba260 in ?? ()
When I put a breakpoint in there, creating and assigning the variable is no problem. The error only occurs once the program is supposed to print that value. Executing the exe without the debugger results in no output at all. The same happens when I replace the double with a long double or float. Printing anything else works fine (Strings, char, int, short, etc.).
I am using Visual Studio Code and MinGW (x86_64-8.1.0-posix-seh-rt_v6-rev0). VS Code is using following files for compilation / debugging:
c_cpp_properties.json
launch.json
tasks.json
And here you can see the complete output, in case that helps.
Any Idea what I am doing wrong here? Thank you.
EDIT:
When compiling manually using g++ -g .\src\HelloWorld.cpp -std=c++11 -o HelloWorld.exe (or just g++ .\src\HelloWorld.cpp -o HelloWorld.exe) and running that from the console, the same happens (no output).
I installed MinGW from here using the following settings:
Version: 8.1.0
Architecture: x86_64
Threads: posix
Exception: seh
Build revision: 0
EDIT 2:
Found the problem. There was an old version of gcc lurking in my PATH (maybe from Visual Studio or MSSQL Server?). I just move the current gcc to the top of PATH and now it's working fine. Thank you all for your help!
As many pointed out, this should usually work. The problem was with my setup: I had an old version of gcc somewhere in my PATH variable (version 4.1). Moving the path of the newer version to the beginning of PATH resolves the issue. Thank you all for helping.
To check weather the same happens to you you can do the following: execute g++ --version in your project directory. Compare this with the output of g++.exe --version when you are in the directory where gcc is installed (for me, this was C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin).
I change post totally.
because I work in south korea army.
but south korea army internet computer is forbid upload file.
so I really upload my source code. but I can't .
so I try debug very very simple program with gdb.
but It is still not working.
my system is
Distributor ID: Ubuntu
Description: Ubuntu 14.04.3 LTS
Release: 14.04
Codename: trusty
in cloud IDE called "nitrous"
and g++ , gdb version is
g++ (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4
GNU gdb (GDB) 7.8
I write very simple code : simple.cpp
#include <iostream>
int main(){
std::cout << "Hello World!" << std::endl;
return 0;
}
compile with "-g" and gdb execute
nitrous#ubuntu-108903:~/code$ g++ -g simple.cpp -o simple
nitrous#ubuntu-108903:~/code$ gdb simple
and set break main and run
Reading symbols from simple...done.
(gdb) break main
Breakpoint 1 at 0x400861: file simple.cpp, line 4.
(gdb) run
Starting program: /home/nitrous/code/simple
Hello World!
During startup program exited normally.
Even very simple helloworld program not work breakpoint.
just print out During startup program exited norally.
I set a break point, but it's not hit. What is problem?
The most likely problem is that your program terminates before reaching main. (A typical dynamically linked program will execute several hundred 1000s of instructions before reaching main.)
Run your program under GDB until GDB stops with SIGSEGV. Execute GDB where command. Observe that main is not on the stack.
Once you've confirmed that main is not on the stack, ask a different question (assuming you still don't understand the reason for crash).
I guess that gdb for some reason failed to set breakpoint. Try to run gdb with sudo.
Btw, could you run strace on generated elf and grep for ptrace? It should be something like follow strace -f -o syscall.txt gdb ./simple.out.
I'm running on Mac OSX, version 10.8.5 (Mountain Lion). I have the following simple C++ code.
main.cpp:
#include <iostream>
int main ()
{
std::cout << "Hello world!"<<std::endl;
std::cout << "Goodbye world!"<<std::endl;
return 0;
}
I'm trying to get gprof to work on my computer. As the manual suggests, I enter the following two lines into my terminal:
g++ -g -pg main.cpp -o a.out
./a.out
However this does not generate a gmon.out file as it is supposed to. When I try typing gprof in the terminal, it says:
gprof: can't open: gmon.out (No such file or directory)
which is to be expected since gmon.out isn't there...
Any ideas on what I'm doing wrong?
EDIT: Some other things that may help:
My friend, who has a similar OS X version (I can ask him later to confirm), and the exact same versions of g++ and gprof, was able to
use gprof successfully as I have outlined.
I'm using an older version of g++ but I have read online that updating to a newer version didn't help.
a.out works perfectly, it prints out Hello world! and Goodbye world!. I also tried this with a more complex C++ program with
several classes and it still has the same problem. Everything
compiles and runs normally but no gmon.out file is produced.
You have to realize that OS X/MacOS does not provide GNU GCC on the system by default.
Note the output of this command:
ls -la /usr/bin/g++ /usr/bin/clang++
These executables look identical. (Actually! It looks like they are different, but somehow the filesize is identical!)
As far as I can tell, clang doesn't support the production of gprof output. As confusing as it may be, the gcc program will run clang.
I would recommend trying to use homebrew to install GCC on OS X/MacOS. You do want to be careful about how it gets installed, etc., so that you know which command corresponds to which compiler.
Before explaining my problem:
My OS is Ubuntu 12.04 LTS 32bits with 4GB RAM.
My IDE is Code::Blocks 12.11(I've had the same problem with 10.05)
I'm using the GNU GCC Compiler with the -g flag and the -std=c++0x flag.
When trying to debug this program:
using namespace std;
#include<iostream>
int main(){
int n=10;
for(int i=1;i<=n;i++){
int ax=i;
while(ax) ax--;
cout<<i;
}
return 0;
}
I can't get anything to happen,if I try the "Run to cursor" command or if I try to use breakpoints. They just get skipped and my program finishes running.
This is what I get in the debugger log:
Building to ensure sources are up-to-date
Selecting target:
Release
Adding source dir: /home/classius/CodeBlocks/Dr/
Adding source dir: /home/classius/CodeBlocks/Dr/
Adding file: /home/classius/CodeBlocks/Dr/bin/Release/Dr
Changing directory to: /home/classius/CodeBlocks/Dr/.
Set variable: LD_LIBRARY_PATH=.:
Starting debugger: /usr/bin/gdb -nx -fullname -quiet -args /home/classius/CodeBlocks/Dr/bin/Release/Dr
done
Registered new type: wxString
Registered new type: STL String
Registered new type: STL Vector
Setting breakpoints
Reading symbols from /home/classius/CodeBlocks/Dr/bin/Release/Dr...(no debugging symbols found)...done.
Debugger name and version: GNU gdb (Ubuntu/Linaro 7.4-2012.04-0ubuntu2.1) 7.4-2012.04
No symbol table is loaded. Use the "file" command.
Temporary breakpoint 2 ("/home/classius/CodeBlocks/Dr/main.cpp:10) pending.
[Inferior 1 (process 13381) exited normally]
Debugger finished with status 0
If anyone wants to suggest a solution that implies using the terminal, please do it in the most noob-orieted terms you can find!
PS: How to add breaklines on stack-overflow when asking a question?(Solved-Thanks!)
Reading symbols from /home/classius/CodeBlocks/Dr/bin/Release/Dr...(no
debugging symbols found)...done.
This means you didn't have the -g option during compilation.
UPDATE:
file /home/classius/CodeBlocks/Dr/bin/Release/Dr
/home/classius/CodeBlocks/Dr/bin/Release/Dr: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=0x7da8c5cff4af5082d82eecc3ede59a5920b253a0, stripped
So it's 'stripped', which means -g option was not really used. You may recheck your compiler config or try to build the Debug version?
in settings menu choose debugger then gdb-cdb tab and then reset default
I have the following smal C++ program
#include <stdio.h>
#include <stdlib.h>
int main(void) {
puts("!!!Hello World!!!");
return EXIT_SUCCESS;
}
I compile in Mac OS X Leopard last release using:
g++ -g hello.cpp -o hello.exe
being g++:
host:bin macbook$ g++ --ver
Using built-in specs.
Target: i686-apple-darwin9
Configured with: /var/tmp/gcc/gcc-5493~1/src/configure --disable-checking -enable-werror --prefix=/usr --mandir=/share/man --enable-languages=c,objc,c++,obj-c++ --program-transform-name=/^[cg][^.-]*$/s/$/-4.0/ --with-gxx-include-dir=/include/c++/4.0.0 --with-slibdir=/usr/lib --build=i686-apple-darwin9 --with-arch=apple --with-tune=generic --host=i686-apple-darwin9 --target=i686-apple-darwin9
Thread model: posix
gcc version 4.0.1 (Apple Inc. build 5493)
then I try to debug this program using fsf-gdb 7.1:
fsf-gdb hello.exe
put a breakpoint in main:
(gdb) b main
Breakpoint 1 at 0x1f8f: file hello.cpp, line 5.
run the program:
(gdb) r
Starting program: /Users/horacio/work/software/gdb/gdb-7.2-inst/bin/hello.exe
Breakpoint 1, main () at hello.cpp:5
5 puts("!!!Hello World!!!");
and try to step, and this happens:
(gdb) n
0x00003045 in ?? ()
This is the output if I do the same under Ubuntu Linux:
(gdb) n
!!!Hello World!!!
6 return EXIT_SUCCESS;
where gdb=7.1 and g++=4.3.4
What is the problem ???? I honestly do not understand why this does not work in mac os x.
Maybe the problem is the gdb version used in mac or the gcc version in mac. Which other alternatives exist for gdb in mac?
Thanks in advance
PS: Apple Leopard's gdb does not produce this error. But I want to use Eclipse CDT, and it can not work with Apple's gdb, that is why I am trying to use a non-Apple gdb version.
This works fine if you use the gdb bundled with Mac. Apple's gcc includes some small Apple-specific extensions, so would not be surprised that it is not 100% compatible with some other version of gdb. You may also have built your custom gdb incorrectly.
You mention that your g++ is 4.3.4, but the one you show above is 4.0.1.
Propably not releted to the actual issue but do remember that when you compile for debugging purposes, disable compiler optimizations with -O0 flag. If you don't pass that for gcc when compiling, you will get "funky" results when you are doing step execution with gdb.
My first thought was that your fsf-gdb doesn't understand Mach-0 binaries.
A quick look at Google came back with: http://reverse.put.as/2009/01/14/how-to-compile-gdb-and-other-apple-open-source-packages-in-mac-os-x/ which reveals that building gdb is not quite as trivial as one might think.