i have a code like this
#include <iostream>
#include <string>
using namespace std;
void Test(){
string line;
}
int main(){
cout << "test " << endl;
return 0;
}
The code is compilable but when i try to run it, the program stop working. Then use gdb to discover what wrong with my program
(gdb) run
Starting program: E:\CPP\Program dinamis\a.exe
[New Thread 4892.0x1d4c]
test
Program received signal SIGILL, Illegal instruction.
0x6fcc43c3 in libstdc++-6!_ZSt4cout () from C:\MinGw\bin\libstdc++-6.dll
i dont understand what wrong with it. i use MinGW (G++) as my compiler by typing g++ -v :
E:\CPP\Program dinamis>g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/mingw32/4.6.2/lto-wrapper.exe
Target: mingw32
Configured with: ../gcc-4.6.2/configure --enable-
languages=c,c++,ada,fortran,objc,obj-c++ --disable-sjlj-exceptions --with-dwarf2
--enable-shared --enable-libgo
mp --disable-win32-registry --enable-libstdcxx-debug --enable-version-specific-
runtime-libs --build=mingw32 --prefix=/mingw
Thread model: win32
gcc version 4.6.2 (GCC)
but if i write the same code on my visual studio, the program runs well without error. what should i do with my G++
You are not actually using the test function for output, you could use it as follows but not sure why there is any error appearing for you as for me it runs fine under GNU GCC version 4.8.1
Here is a sample of code which uses Test()
#include <iostream>
#include <string>
using namespace std;
string Test(){
string line = "My text Line here!";
return line;
}
string Test2(){
return "My text Line2 here!";
}
int main(){
cout << Test() << endl;
cout << Test2() << endl;
return 0;
}
The second test is to illustrate a simpler usage for returning a string, notee as well the function needs to be defined as returning a string as well.
Well finally i found the solution. I am running on windows 8 Operating system and it seems the main problem is on linking process while compiling the source code.
So let say the filename is mycode.cpp and i just run the command with additional -static-libgcc -static-libstdc++ options like this one:
g++ "mycode.cpp" -o mycode -static-libgcc -static-libstdc++
Just a blind shot, try handling main function parameters:
int main(int, char**)
{
//...
Maybe it's something with the stack...?
Related
I created and executed a simple thread on my system. when I execute this program, I have the error message : Enable multithreading to use std::thread: Operation not permitted
some details about my system :
linux ubuntu 13.10
g++ 4.8.1
I compile the source code including the library pthread
The source code:
#include <iostream>
#include <thread>
using namespace std;
void func(void) {
cout << "test thread" << endl;
}
int main() {
cout << "start" << endl;
thread t1 (func);
t1.join();
cout << "end" << endl;
return 0;
}
It seems that you are trying to use C++11 threads. If it is true, then
correct #include <thread> and #include <iostream>, i.e. do not use " in these lines and add # in front of them.
compile with g++ -std=c++11 q.cpp -lpthread (dependency order matters for newer g++)
Hint: when you are using threads in a static linked library and use this library in an executable, then you have to add the flag -pthread to the link command for the executable. Example:
g++ Obj1.o Obj2.o MyStaticLib.a -o MyExecutable -pthread
I have a C++ program which I am compiling on a x64 machine running Windows 10 with g++ x86_64-win32-seh-rev2 v7.1.0, using the command g++ -g main.cpp. When I run my program, I get the error 0xc000007b. This is my code
#include <iostream>
using namespace std;
int main() {
cout << "Hello World";
return 0;
}
When I compile with this code
#include <stdio.h>
using namespace std;
int main() {
printf("Hello World");
return 0;
}
It works fine. When I run it in gdb, it runs fine.
I have seen other posts where there are dlls being used that do not support the architecture, but I don't think I am using any dlls in this application, unless they are being added by g++
This is my code in the C++ file:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World" << endl;
return 0;
}
My compiler is Clang. Yes I do put "clang++" in the terminal. The problem is, is that it runs it and doesn't output anything. It just starts a new line.
I am running Clang version 3.8.0.
As you said clang++ is the compiler, that is, it turns your code into a binary file that can be executed. It doesn't execute the program itself.
By default the compiled file name is a.out, so after compiling, to execute the program you should run:
./a.out
You could specify the output file name like so:
clang++ infile.cpp -o outfile
and then run the binary:
./outfile
I created and executed a simple thread on my system. when I execute this program, I have the error message : Enable multithreading to use std::thread: Operation not permitted
some details about my system :
linux ubuntu 13.10
g++ 4.8.1
I compile the source code including the library pthread
The source code:
#include <iostream>
#include <thread>
using namespace std;
void func(void) {
cout << "test thread" << endl;
}
int main() {
cout << "start" << endl;
thread t1 (func);
t1.join();
cout << "end" << endl;
return 0;
}
It seems that you are trying to use C++11 threads. If it is true, then
correct #include <thread> and #include <iostream>, i.e. do not use " in these lines and add # in front of them.
compile with g++ -std=c++11 q.cpp -lpthread (dependency order matters for newer g++)
Hint: when you are using threads in a static linked library and use this library in an executable, then you have to add the flag -pthread to the link command for the executable. Example:
g++ Obj1.o Obj2.o MyStaticLib.a -o MyExecutable -pthread
I'm trying to use boost::exception and have condensed my prototype code down to the example in the Boost exception tutorial however when running the code with the BOOST_THROW_EXCEPTION macro I get an abort from the program.
#include <iostream>
#include <boost/exception/all.hpp>
typedef boost::error_info<struct tag_my_info,int> my_info;
struct my_error: virtual boost::exception, virtual std::exception { };
void f() {
BOOST_THROW_EXCEPTION(my_error() << my_info(42));
// Uncomment the below (and comment the above) for the program to work
//throw my_error() << my_info(42);
}
int main(int argc, char** argv) {
try {
f();
}
catch(my_error& x) {
if(int const* mi = boost::get_error_info<my_info>(x)) {
std::cout << "My info: " << *mi << std::endl;
}
}
return 0;
}
Running the code with the BOOST_THROW_EXCEPTION macro:
$ ./a.out
Abort trap
If as the comment says, I swap the code, all is well
$ ./a.out
My info: 42
Below is the output from the g++ preprocessor for f()
void f() {
::boost::exception_detail::throw_exception_(my_error() << my_info(42),__PRETTY_FUNCTION__,"main.cpp",14);
}
Software versions are:
$ g++ -v
Using built-in specs.
Target: x86_64-apple-darwin10
Thread model: posix
gcc version 4.4.6 (GCC)
$ port list boost
boost #1.47.0 devel/boost
I'm on OSX SL using the tools provided by MacPorts. I've double checked the g++ search paths and there's only one copy of the boost hpp files and that's the ones that belong to the aforementioned boost package.
I have no idea why the abort trap is being called. I admit I'm newish to C++ .
The problem was caused by using the MacPorts version of g++. There are plenty of tickets related to exceptions and Abort Traps in the MP system (and plenty of examples on Google).
Using the version of g++ that comes with XCode enabled this problem to go away.