I just installed MinGW because I wanted to write code using a lightweight code editor instead of Visual Studio or Codeblocks, both of which I never had a problem with. So the code compiles and works, but for some reason it crashes every time I try to use a string or char pointer. For example this code:
#include <iostream>
int main()
{
std::string a;
a = "Hello";
std::cout << a;
std::cin.get();
}
throws this error when executed:
but this code:
#include <iostream>
int main()
{
std::cout << "hello";
std::cin.get();
}
Works perfectly:
(I have tried both with std::string and const char* with no difference)
I have Visual studio 2017 installed, I don't know if that could be the issue.
MinGW version 6.0.3.
PD: I only added C:/MinGW/bin directory to Path environment, don't know if I should add any other directory
Fixed it! For some reason g++ by default links the std library dynamically. As my program didn't find the dll it was not able to link std::string and std::cout. The solution was to pass the -static-libstdc++ argument to g++.
TL;DR:
Instead of compiling directly like this:
g++ somesourcefile.cpp
try this argument:
g++ somesourcefile.cpp -static-libstdc++
Related
Can someone explain what is happening here?
#include <iostream>
#include <string>
using namespace std;
int main() {
string kek = "kek";
cout << "test" << endl;
return 0;
}
For some reason, every time I compile with g++, and I declare a string variable, I can't see any output, no compile or runtime errors, simply no output.
Now, if I change the code to be this instead:
#include <iostream>
#include <string>
using namespace std;
int main() {
// string kek = "kek";
cout << "test" << endl;
return 0;
}
then everything works.
Does anyone know what the problem is here?
I am currently on Windows 10, using gcc 8.1.0.
Edit:
same thing but using bash, linux compile
i think the version of mingw i had is bugged so trying to reinstall it
Okay so still i have no idea what that bug was about,
but when i had it, the version of MinGW i had was 32bit,
changing the MinGW installation to 64bit, fixed that issue.
i have changed nothing, except removing MinGW 32bit from my comp,
and setting the MinGW 64bit.
Swaping from MinGW 32bit to 64bit, fixed the issue.
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++
I constantly have CLion editor showing me parameter type mismatch errors while during build everything is fine. For example, consider the following MWE:
#include <iostream>
#include <boost/container/flat_set.hpp>
using namespace std;
namespace bc = boost::container;
int main() {
bc::flat_set<bc::flat_set<int>> manySets;
bc::flat_set<int> oneSet({1, 2, 3});
manySets.insert(oneSet);
cout << "Hello, World!" << endl;
return 0;
}
Here flat_set is a template from boost library (description could be seen here). Editor shows me an error:
But when I build it (even from CLion), everything is compiled fine.
My system is:
Ubuntu 15.10 64bit
CLion 1.2.4
This looks like a known problem - https://youtrack.jetbrains.com/issue/CPP-6027. We hope to fix it soon.
I am using CodeBlocks with gcc 4.7.2 and gmp 5.0.5 on a Win 7 64 machine.
After starting to use gmpxx I see a strange segfault which doesn't occur with the +,-,etc operators, but with << when trying to cout an mp*_class variable.
I have to say that gmpxx works perfectly so far except for this.
For example:
#include <iostream>
#include <gmpxx.h>
using namespace std;
int main()
{
mpz_class c = 21;
cout << c << endl;
}
gives a segfault on the line with the cout, whereas the below code works fine:
#include <iostream>
#include <gmpxx.h>
using namespace std;
int main()
{
mpz_class a = 3, b = 8, c;
c = a + b;
cout << c.get_str() << endl;
}
What is even stranger is that this code:
#include <iostream>
#include <gmpxx.h>
using namespace std;
int main()
{
mpz_class a = 3, b = 8, c, d = 21;
c = a + b;
cout << c.get_str() << endl;
cout << d << endl;
}
doesn't segfault when run, but shows only the first result (11) and then exits normally.
On the other hand in debug it segfaults on: cout << d << endl.
I've googled for the last couple of days and found nothing similar to only some of the overloaded operators not working.
I would be thankful for an explanation.
I linked the two gmp libraries in codeblocks like this:
Settings->Compiler and Debugger->Global Compiler Settings->Linker Settings
and there I've added: C:\mingw\lib\libgmpxx.dll.a and C:\mingw\lib\libgmp.dll.a (in that order).
Nothing else was needed in order to compile c++ code with gmpxx.
Finally my CodeBlocks build log looks like this:
g++.exe -pg -g -pg -g -c "C:\Temp\test.cpp" -o .objs\test.o
g++.exe -o test.exe .objs\test.o -pg -lgmon -pg -lgmon C:\mingw\lib\libgmpxx.dll.a C:\mingw\lib\libgmp.dll.a
I honestly don't know why there are two switches of each.
If you need any more info, I'd be glad to provide. Thank you.
Alright, time to mark this as answered. The fact is that I installed gmp via mingw-get whereas almost everywhere in googleland it states to build it yourself for your own system. A silly mistake and thanks to the comment by #Lol4t0 it works fine now.
So for all the new guys like me:
1) Install MinGW with MSYS
2) Download gmp source and extract to some folder in mingw\msys\1.0\home\
3) open mingw shell and navigate to gmp folder
4) ./configure --enable-cxx --prefix=/home/newgmpinstall
5) make
6) make install
7) make check
If it checks ok then in newgmpinstall you'll find the headers gmp.h and gmpxx.h and libraries libgmp.a and libgmpxx.a which work for your system.
You can move them to a new folder if you wish. Then in your IDE Project properties add the *.a files to your link libraries and the folder with the *.h files to the compiler search directories.
Write code
Note: At first ./configure exited with an error about M4 missing because I was missing the M4 package. Just download the source for M4 and do the above steps first for M4 and then install gmp.
I recently installed MinGW and MSYS on my Windows 32 machine and it seems to be running fine.
On the C++ compiler, I am including a vector container and getting no errors to that. But I`m getting compile-time errors when I try to use it.
So, the code
#include <vector> // include vector.h
#include <stdio.h> // include stdio.h
using namespace std;
main() {
// vector<int> A;
printf("\nHeya ..");
}
is running just fine. However, the moment I un-comment line 8-- the vector declaration line, I get the following error (shortened) in compile time:
undefined reference to 'operator delete(void*)'
undefined reference to '__gxx_personality_v0'
You're probably compiling with gcc instead of g++. The actual compiler is the same, but g++ tells the linker to use the default C++ libraries, were gcc only tells it to look at the C libraries. As soon as you use and C++-specific parts of the standard library, gcc will fail.
As an aside, C++ doesn't support the default int rule from old C, so you should really specify the return type from main.
I don't see how you are compiling your code. Your main method is invalid, incorrect signature and you aren't returning anything.
Should be like this:
#include <vector> // include vector.h
#include <stdio.h> // include stdio.h
using namespace std;
int main(int, char**) {
// vector<int> A;
printf("\nHeya ..");
return 0;
}
Also you need to compile this with g++ and not gcc.