c++ hello world throws Segmentation fault - MinGW, Netbeans - c++

Set up MinGW and Netbeans. Only working withe othere IDE's and compilers in before. My machine is x64 win7x64. I'm not shure if I only installed the 32bit version of MinGW.
Compiling a simpel Helle World in x64 leeds to:
g++ -m64 -c -g -MMD -MP -MF "build/Debug/MinGW-Windows/main.o.d"
-o build/Debug/MinGW-Windows/main.o main.cpp
//Message: sorry, unimplemented: 64-bit mode not compiled in
The win32 Version compiles fine.
// ---
g++ -m32 -c -g -MMD -MP -MF "build/Debug/MinGW-Windows/main.o.d"
-o build/Debug/MinGW-Windows/main.o main.cpp
// Compiles without any error...
Starting the 32-Version it prints only one message and terminats withe a segmentation fault before returning from first std:cout. Using ftream, to make a file output same error occures.
The demo-Programm of netbeans "Welcome" throws the same exceptions.
#include <cstdlib>
#include <iostream> // EDIT! (worked at this post long time sry.)
using namespace std;
int main(int argc, char** argv) {
int i = 0; // for making shure, program is working before calling fs.open
std::cout << "hello world blubb" << endl; // this one shows up its message at consol.
//programm aborded.
std::cout << "Now again" << endl; // this on is never reached.
return 0;
}
Errormessages:
Signal received: SIGSEGV ( Segmentation fault )
For programm cppapplication_1 pid 7972
You may discard...
Errormessage windows:
cppapplication_1.exe does not work.
Where can I start my serach? Compiler? some dlls? Dependency Walker?
Thx for any comment =).
Cutton

Related

Compiling & building CPP using LLVM (Clang) on Windows10

I installed LLVM on a clear Windows 10 machine, without anything else. I installed the ARMCompiler6.14.1. I have a simple cpp for testing:
#include <stdio.h>
#include <iostream>
#include <vector>
int main(void) {
int counter = 0;
counter++;
printf("counter: %d", counter);
std::cout << std::endl;
printf("c++14 output");
std::cout << std::endl;
std::vector<int> vect{1, 2, 3, 4, 5};
for (auto & el : vect)
std::cout << "-" << el << std::endl;
return counter;
}
When I compile this with following line I get errors:
"C:\Program Files\LLVM\bin\clang++.exe" -MD -x c++ "C:\Test\test.cpp" -target armv7a-none-eabi "-IC:\Program Files\ARMCompiler.6.14.1\include"
The error is:
test.cpp 2:10:fatal error: 'iostream' file not found
When I compile this with following line I get an windows executable which runs:
"C:\Program Files\LLVM\bin\clang++.exe" -MD -x c++ "C:\Test\test.cpp"
Since I checked that in the LLVM\bin is anything from the developer.arm.com package (ARMcompiler 6.14.1), I think that I should get an a.out for ARM, but I do not. When I checked the generated object file with:
"C:\Program Files\LLVM\bin\llvm-objdump.exe" -f test.o
I get following:
test.o file format elf32-littlearm
arhitecture: arm
start adress: 0x00000000
which means the object file is correctly generated for ARM, but I did not get an executable a.out for ARM.
What am I missing? How should I compile a simple test program to get it cross-compiled for ARM? Should I get the binutil from GCC for the Clang to create ARM executables?

using c++ Boost in a makefile

I have a simple C++ program which I compile with clang using the Boost library and with C++14 support.
I use the following command to compile my sample.cpp file and it works fine:
clang++ -g -std=c++1y -I$BOOST_ROOT sample.cpp -o sample
where $BOOST_ROOT is the path to where I downloaded and extracted the boost zip file.
$BOOST_ROOT=/usr/local/boost_1_66_0/
When I try to compile the same sample.cpp file with a makefile, it doesn't work.
This is what my makefile looks like:
sample: sample.cpp
clang++ -g -std=c++1y -I$BOOST_ROOT sample.cpp -o sample
Running make command, I get the error:
Sample.cpp:9:10: fatal error: 'boost/format.hpp' file not found
#include <boost/format.hpp>
^~~~~~~~~~~~~~~~~~
1 error generated.
And here is the sample.cpp
//
// sample.cpp
//
//
#include <iostream>
#include <boost/format.hpp>
using namespace std;
int main()
{
std::cout << "Enter your first name: " << std::endl;
std::string firstName;
std::cin >> firstName;
std::cout << "Enter your surname: " << std::endl;
std::string surname;
std::cin >> surname;
auto formattedName = str( boost::format("%1% %2%"s) % firstName % surname );
std::cout << "You said your name is: " << formattedName << std::endl;
return 0;
}
CONSOLE OUTPUT
MacBook-Air:Listing_1_7 userd43f$ make
c++ sample.cpp -o sample
sample.cpp:9:10: fatal error: 'boost/format.hpp' file not found
#include <boost/format.hpp>
^~~~~~~~~~~~~~~~~~
1 error generated.
make: *** [sample] Error 1
MacBook-Air:Listing_1_7 userd43f$ ls $BOOST_ROOT
INSTALL boost boost.png bootstrap.sh index.html rst.css
Jamroot boost-build.jam boostcpp.jam doc libs status
LICENSE_1_0.txt boost.css bootstrap.bat index.htm more tools
The problem was that there were spaces instead of a tab in the command-line of my makefile.
BOOST_ROOT := /usr/local/boost_1_66_0
BOOST_INC := ${BOOST_ROOT}/include
sample: sample.cpp
clang++ -g -std=c++1y -I$(BOOST_ROOT) sample.cpp -o sample
In the last line just before clang++ command I was using spaces instead of a TAB. I replaced all the spaces with a single TAB before clang++, and also I needed to put the BOOST_ROOT inside the parentheses as (mentioned by #MaximEgorushkin)
Then it started picking up the right command, as shown in the output below:
MacBook-Air:Listing_1_7 userd43f$ make
clang++ -g -std=c++1y -I/usr/local/boost_1_66_0 sample.cpp -o sample
Boost root directory normally has include and lib directories in it.
It should probably be:
BOOST_ROOT := /usr/local/boost_1_66_0
BOOST_INC := ${BOOST_ROOT}/include
sample: sample.cpp
clang++ -g -std=c++1y -I${BOOST_INC} sample.cpp -o sample

.gcda files don't merge on multiple runs

I have two main functions that use a common C++ class.
File1: main.cpp
#include <iostream>
#include "HelloAnother.h"
int main() {
HelloAnother::sayHello1();
return 0;
}
File2: main2.cpp
#include <iostream>
#include "HelloAnother.h"
int main() {
HelloAnother::sayHello2();
return 0;
}
File3: HelloAnother.h
#pragma once
class HelloAnother {
public:
static void sayHello1();
static void sayHello2();
};
File4: HelloAnother.cpp
#include <iostream>
#include "HelloAnother.h"
void HelloAnother::sayHello1() {
std::cout << "Hello 1!!!" << std::endl;
}
void HelloAnother::sayHello2() {
std::cout << "Hello 2 !!!" << std::endl;
}
Now I compile two executables:
clang-3.8 -o main -fprofile-arcs -ftest-coverage --coverage -g -fPIC -lstdc++ main.cpp HelloAnother.cpp
clang-3.8 -o main2 -fprofile-arcs -ftest-coverage --coverage -g -fPIC -lstdc++ main2.cpp HelloAnother.cpp
Now, I run ./main
Hello 1!!!
When I rerun ./main
Hello 1!!!
profiling: /media/sf_ubuntu-shared/test-profiling/main.gcda: cannot map: Invalid argument
profiling: /media/sf_ubuntu-shared/test-profiling/HelloAnother.gcda: cannot map: Invalid argument
One second run, I get this error (above) in trying to create/merge .gcda files.
Now, If I try to run ./main2
Hello 2 !!!
profiling: /media/sf_ubuntu-shared/test-profiling/HelloAnother.gcda: cannot map: Invalid argument
When I generate the code coverage report, the call to second function doesn't show up as if the call wasn't made.
Can anyone help me debug this issue pls? The issue seems to be related to merging of .gcda files on multiple runs, but not sure how to solve it.
I also tried clang-3.5 but with same results.
After a lot of searching and trial/error this is what works for me:
Compile first executable, run it. This generates HelloAnother.gcda and main.gcda files.
Execute lcov --gcov-tool=gcov-4.4 --directory . --capture --output-file coverage.main.info
rm -rf *.gcda; rm -rf *.gcno
Compile second executable (main2.cpp), run it. This generates another HelloAnother.gcda and a main2.gcda file.
Execute lcov --gcov-tool=gcov-4.4 --directory . --capture --output-file coverage.main2.info
Now to generate nice looking html report do: genhtml -o coverage coverage.main.info coverage.main2.info
Your problem is that you compile the shared file (HelloAnother.cc) twice and gcov fails to understand that two copies of HelloAnother.o inside main1 and main2 need to be shared.
Instead, compile the shared code once and link it into each executable:
$ g++ --coverage -c HelloAnother.cc
$ g++ --coverage main1.cc HelloAnother.o -o main1
$ g++ --coverage main2.cc HelloAnother.o -o main2
$ ./main1
Hello 1!!!
$ ./main2
Hello 2 !!!
$ gcov --stdout HelloAnother.gcno
...
1: 4:void HelloAnother::sayHello1() {
1: 5: std::cout << "Hello 1!!!" << std::endl;
1: 6:}
-: 7:
1: 8:void HelloAnother::sayHello2() {
1: 9: std::cout << "Hello 2 !!!" << std::endl;
1: 10:}
-: 11:

llvm-g++ compile "hello world" error: internal compiler error: Segmentation fault

I am quite new to c++,and this is my development configuration:
llvm 3.5
gcc+ 4.8
ubuntu 14.04 64bit
eclipse 4.3
This is a "hello world" content generated by eclipse
#include <iostream>
using namespace std;
int main() {
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
return 0;
}
In the beginning,eclipse refuses to compile and complain:
Program "llvm-g++" not found in PATH
Program "llvm-gcc" not found in PATH
after I made two symbolic links:
sudo ln -s /usr/bin/llvm-g++-4.8 /usr/bin/llvm-g++
sudo ln -s /usr/bin/llvm-gcc-4.8 /usr/bin/llvm-gcc
Eclipse starts to compile,but give me these errors:
Info: Internal Builder is used for build
llvm-g++ -O0 -emit-llvm -g3 -Wall -c -fmessage-length=0 -o src/HelloWorld.bc ../src /HelloWorld.cpp
*** WARNING *** there are active plugins, do not report this as a bug unless you can reproduce it without enabling any plugins.
Event | Plugins
PLUGIN_FINISH_UNIT | dragonegg
PLUGIN_FINISH | dragonegg
PLUGIN_START_UNIT | dragonegg
PLUGIN_ALL_IPA_PASSES_END | dragonegg
../src/HelloWorld.cpp: In function ‘main’:
../src/HelloWorld.cpp:13:10: internal compiler error: Segmentation fault
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
^
Please tell me what I did wrong here?

boost::program_options::positional_options_description termination

The following program aborts with pointer being freed was not allocated:
#include <boost/program_options.hpp>
int main(int argc, char* argv[])
{
boost::program_options::positional_options_description positional;
return 0;
}
I compiled and linked the program with Boost 1.46.1, which I built myself into /usr/local, on OS X 10.6.7. I can't find any installed libboost_program_options other than the one I'm (supposedly) linking against.
Any idea what causes this crash?
Edit: As for stacktrace, the program
#include <boost/program_options.hpp>
#include <execinfo.h>
int main(int argc, char* argv[])
{
boost::program_options::positional_options_description positional;
void* callstack[128];
int i, frames = backtrace(callstack, 128);
char** strs = backtrace_symbols(callstack, frames);
for (i = 0; i < frames; ++i) {
printf("%s\n", strs[i]);
}
free(strs);
return 0;
}
built as
g++ -Wp,-MMD,.make-debug/main.dd -Wall -g3 -I/usr/local/include -c main.cc -o .make-debug/main.o
g++ -o sandbox .make-debug/main.o -lboost_program_options -L/usr/local/lib
and run as ./sandbox produces the output
0 sandbox 0x00000001000017bf main + 57
1 sandbox 0x0000000100001764 start + 52
2 ??? 0x0000000000000001 0x0 + 1
sandbox(50587) malloc: *** error for object 0x7fff70506500: pointer being freed was not al
located
*** set a breakpoint in malloc_error_break to debug
Command terminated
As for building Boost:
$ cd boost_1_46_1
$ ./bootstrap.sh --prefix=/usr/local
$ ./bjam toolset=darwin-4.2
And here's my ~/user-config.jam:
using darwin : 4.0 : g++-4.0 ;
using darwin : 4.2 : g++-4.2 ;
using darwin : 4.5.1 : /Users/matan/usr/bin/g++ ;
I am unable to reproduce
macmini:stackoverflow samm$ cat po.cc
#include <boost/program_options.hpp>
#include <boost/version.hpp>
#include <iostream>
int
main(int argc, char* argv[])
{
std::cout << BOOST_LIB_VERSION << std::endl;
boost::program_options::positional_options_description positional;
return 0;
}
macmini:stackoverflow samm$ g++ -I /opt/local/include -L/opt/local/lib -lboost_program_options po.cc
macmini:stackoverflow samm$ ./a.out
1_46_1
macmini:stackoverflow samm$
you should update your question with the steps you used to build boost, particularly the arguments to bjam.
I think I resolved the issue, but I'm not happy with my solution. I neglected to mention that I previously installed gcc 4.6.0 with --program-suffix=-4.6 in /usr/local. Uninstalling it and rebuilding Boost solved the issue. I then had no compilers installed other than gcc-4.0 and gcc-4.2 which came with XCode. Presumably gcc-4.6 files interfered with gcc-4.0 files or the darwin toolset.