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.
Related
So I installed OpenSSL directly following the instructions in the install.txt file you get when cloning the git repository. It installed it seemingly witout issues and is located in C:/Program Files/OpenSSL and C:/Program Files(x86)/OpenSSL. Inside each of those there are 4 folders: bin, html, include, lib.
In VSCode I'm using the code runner extension with the following command:
g++ -I "C:/Users/Batres/vcpkg/installed/x86-windows/include" "C:/Program Files/OpenSSL/include" simple_functions.cpp -o simple_functions
the "C:/Users/Batres/vcpkg/installed/x86-windows/include" is a different library called XTensor, which works completely fine as I have tested the code without OpenSSL and it outputs the correct results with no problem. This is the code I'm using, although I doubt it's relevant since it seems to be a complier issue.
#include <cmath>
#include <iomanip>
#include <cstddef>
#include "xtensor.hpp"
#include <functional>
#include <string>
#include <typeinfo>
#include <vector>
#include <regex>
#include <array>
#include <algorithm>
#include <openssl/sha.h>
#include <openssl/evp.h>
/*
def hash_string(string):
# Truncating at 16 bytes for cleanliness
hasher = hashlib.sha256(string.encode())
return hasher.hexdigest()[:16]
*/
std::string hash_string(std::string string) {
// Truncating at 16 bytes for cleanliness
std::array<unsigned char, SHA256_DIGEST_LENGTH> hash;
std::array<unsigned char, SHA256_DIGEST_LENGTH>::iterator it;
EVP_MD_CTX* ctx = EVP_MD_CTX_new();
EVP_DigestInit_ex(ctx, EVP_sha256(), NULL);
EVP_DigestUpdate(ctx, string.c_str(), string.size());
EVP_DigestFinal_ex(ctx, hash.data(), NULL);
// Convert the hash to a hexadecimal string
std::stringstream ss;
for (it = hash.begin(); it != hash.end(); it++) {
ss << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(*it);
}
return ss.str().substr(0, 16);
}
int main(){
double a = gen_choose(2,5);
std::string input = "Hello, world!";
std::string output = hash_string(input);
std::cout << std::setprecision(16) << output << std::endl;
return 0;
}
Ignore all the other #includes, they are for different functions that work with no issue. As you can probably see I have some python code commented above my C++ code. Thats simply because it's a function I tried transalting using ChatGPT, but that's not relevant to the error.
This is the line for C++ settings in code-runner.executorMap:
"cpp": "cd $dir && g++ -I \"C:/Users/Batres/vcpkg/installed/x86-windows/include\" \"C:/Program Files/OpenSSL/lib\" $fileName -o $fileNameWithoutExt -lssl && $dir$fileNameWithoutExt"
My compiler is mingw and I'm on Windows 11. I also don't have any antivirus aside from Windows Defender and I've added the library to the exceptions list as well as ld.exe.
I have also tried these commands, all yeilding the same error. I have also tried running all the same commands through a terminal with admin priviliges and it gives the same error. I even copied the folder to a different directory inside my /Documents/ folder and it still didn't help.g++ -I "C:/Users/Batres/vcpkg/installed/x86-windows/include" "C:/Program Files (x86)/OpenSSL/include" simple_functions.cpp -o simple_functions
and
g++ -I "C:/Users/Batres/vcpkg/installed/x86-windows/include" -L "C:/Program Files/OpenSSL/lib" simple_functions.cpp -o simple_functions -lssl
This last one returns a different error. Insted of being acces denied it seems like none of the functions or variables that I call inside my code are recognised, so this is the type of error: undefined reference to 'EVP_MD_CTX_new', or any other functions or variables I call.
The weird thing is that even though it says access denied when compliling, Intellisense can acess the files just fine, I can right click on the functions and "Go to Definition" and it takes me to my C:/Program Files/OpenSSL/include/openssl/ folder.
If anyone knows how to fix the error I would be very thankful.
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 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++
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'm trying to get started with premake but I can't get my test project to link properly with it. If I link it manual it works fine though.
I'm using premake 4.3 (also tested it with premake 4.4) on OS X 10.9 with clang 3.4.
After I create a makefile via "premake4 gmake" and try to compile it I get an error like this:
Linking subproject
ld: internal error: atom not found in symbolIndex(__ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc) for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[1]: *** [libsubproject.dylib] Error 1
make: *** [subproject] Error 2
My pretty simple project setup:
project/
src/
test.cpp
subproject/
include/
Library.hpp
source/
Library.cpp
premake4.lua
premake4.lua
solution "testa"
configurations {"debug"}
language "C++"
includedirs {"subproject/include"}
project "subproject"
kind "SharedLib"
files {"subproject/source/*.cpp"}
project "main"
kind "ConsoleApp"
files {"src/*.cpp"}
links {"subproject"}
src/test.cpp
#include <iostream>
#include <Library.hpp>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
Library lib(13, 3);
lib.do_stuff(7);
return 0;
}
subproject/include/Library.hpp
#ifndef __LIBRARY_HPP__
#define __LIBRARY_HPP__
#include <iostream>
using namespace std;
class Library {
public:
Library(int, int);
void do_stuff(int) const;
private:
int x;
int y;
};
#endif
subproject/source/Library.cpp
#include <Library.hpp>
Library::Library(int x, int y) {
this->x = x;
this->y = y;
}
void Library::do_stuff(int z) const {
cout << "X: " << x << "Y: " << y << "Z: " << z << endl;
}
Thank you for your time.
This is a known premake bug. It was reported and fixed, but a fixed version of the program has not been released yet. See the discussion here.
This bug is caused by -Wl,-x linker flags that premake will add by default to the project.make makefile. As of now, there are two possible solutions, download the updated premake source with the fix, compile it and install the new version, or, manually change the value of LDFLAGS in the generated project.make after each run of premake.
I have also tried the suggestion they give in the link above of setting premake.tools.gcc.ldflags.flags._Symbols to nil, but it had no effect on my system.