Why I get "mpir_ui" was not declared in this scope? - c++

after installing mpir-3.0 on fedora 31. Now I try to build project:
#include <stdio.h>
#include <gmp.h>
#include <mpir.h>
#include <mpfr.h>
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
mpf_t a; //mpir float variable
mpf_init(a); //initialise a
mpir_ui two = 2; //mpir unsigned integer variable
FILE* stream; //file type pointer to output on standard output (console)
mpf_init_set_ui (a, 2); //set value of a to 2
mpf_out_str (stream, 10, 2, a); //output value of a
cout << "\nMPIR working" << "\n" ;
}
But when I compile it I get this error:
‘mpir_ui’ was not declared in this scope; did you mean ‘mpfr_ai’?|
I've used the flags:
-lmpir -lmpfr -lgmp

Related

c++ std::vector<int> Undefined symbols for architecture x86_64:

I'm having problems using std::vector<int> in C++ on Mac OSX Catalina. I have a function static int insertMoneyData(std::vector<int> money) that writes data to an SQLite database. The function is declared in SQLFunctions.h and defined in SQLFunctions.cc.
When also running the function from SQLFunctions.cc, everything works fine in the compilation (using c++ in make). But when I try to run the same function from another file (city.cc), I get the following error:
Undefined symbols for architecture x86_64:
"insertMoneyData(std::__1::vector<int, std::__1::allocator<int> >)", referenced from:
City::save_money_data() in city.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1
(use -v to see invocation)
make: *** [main] Error 1
Calling insertMoneyData(money_data) from SQLFunctions.cc, where it is defined, works fine.
//SQLFunctions.cc
#include <iostream>
#include <sqlite3.h>
#include <stdio.h>
#include <vector>
#include <cstring>
#include "SQLfunctions.h"
using namespace std;
using Record = std::vector<std::string>;
using Records = std::vector<Record>;
int initiateDB() {
std::vector<int> money_data;
money_data.push_back(1);
money_data.push_back(2);
money_data.push_back(3);
money_data.push_back(4);
money_data.push_back(5);
money_data.push_back(6);
money_data.push_back(7);
money_data.push_back(8);
money_data.push_back(9);
money_data.push_back(10);
money_data.push_back(11);
money_data.push_back(12);
money_data.push_back(13);
insertMoneyData(money_data);
}
static int insertMoneyData(std::vector<int> money) {
const char* dir = "/Users/bennyjohansson/Projects/ekosim/myDB/ekosimDB.db";
sqlite3* DB;
char* messageerror;
int exit = sqlite3_open(dir, &DB);
string sql = "INSERT INTO MONEY_DATA (TIME, BANK_CAPITAL, BANK_LOANS, BANK_DEPOSITS, BANK_LIQUIDITY, CONSUMER_CAPITAL, CONSUMER_DEPOSITS, CONSUMER_DEBTS, COMPANY_DEBTS, COMPANY_CAPITAL, MARKET_CAPITAL, CITY_CAPITAL, TOTAL_CAPITAL) VALUES(";
sql.append(std::to_string(money[0]) + ", ");
sql.append(std::to_string(money[1]) + ", ");
sql.append(std::to_string(money[2]) + ", ");
sql.append(std::to_string(money[3]) + ", ");
sql.append(std::to_string(money[4]) + ", ");
sql.append(std::to_string(money[5]) + ", ");
sql.append(std::to_string(money[6]) + ", ");
sql.append(std::to_string(money[7]) + ", ");
sql.append(std::to_string(money[8]) + ", ");
sql.append(std::to_string(money[9]) + ", ");
sql.append(std::to_string(money[10]) + ", ");
sql.append(std::to_string(money[11]) + ", ");
sql.append(std::to_string(money[12]) + ");");
exit = sqlite3_exec(DB, sql.c_str(), NULL, 0, &messageerror);
}
However, when I call the function from another file, it doesn't work:
//City.cc
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <cstring>
#include <vector>
#include <list>
#include <fstream>
#include <cmath>
#include <random>
#include "SQLfunctions.h"
using namespace std;
void City::save_money_data() {
std::vector<int> money_data;
money_data.push_back(1);
money_data.push_back(2);
money_data.push_back(3);
money_data.push_back(4);
money_data.push_back(5);
money_data.push_back(6);
money_data.push_back(7);
money_data.push_back(8);
money_data.push_back(9);
money_data.push_back(10);
money_data.push_back(11);
money_data.push_back(12);
money_data.push_back(13);
insertMoneyData(money_data);
}
Declaring:
//SQLFunctions.h
#ifndef SQL_FUNCTIONS_H
#define SQL_FUNCTIONS_H
#include <iostream>
#include <sqlite3.h>
#include <stdio.h>
using namespace std;
using Record = std::vector<std::string>;
using Records = std::vector<Record>;
int initiateDB();
static int createDB(const char* s);
static int createParameterTable(const char* s);
static int createDataTable(const char* s);
static int createMoneyTable(const char* s);
static int insertParameterData(const char* s);
static int insertMoneyData(std::vector<int> money); //
static int updateData(const char* s);
static int updateParameter(const char* s, string, double);
static int deleteTheData(const char* s);
static int selectData(const char* s);
static int callback(void* NotUsed, int argc, char** argv, char** azColName);
int select_callback(void *p_data, int num_fields, char **p_fields, char **p_col_names);
Records select_stmt(const char* stmt, const char* s);
#endif
Problem is obsolete keyword static before each function.
static keyword has multiple meanings. In this context it means: this function definition should be visible only in this translation unit (translation unit means sources compiled during single compilation - so source file with all its includes).
So you defined a function which should be accessed by other translation units (other sources), but you have limited its visibility to single file where it is defined. That is why linker complains that can't find this functions.

How to convert a command line argument to an int?

Im trying to convert the command line argument(*argv[]) to an integer using the atoi function
int main(int argc, char *argv[]) {
This is my attempt
#include <iostream>
#include <sstream>
#include <string>
#include <cstdlib>
#include <conio.h>
using namespace std;
int main(int argc, char *argv[]) {
int x = 0;
for ( x=0; x < argc; x++ )
{
int x = atoi(argv[1]);
cout << x;
}
return 0;
}
However this returns 0 and im unsure why. Thankyou
It's hard to say having the arguments you pass to your program, but there are few problems here.
Your loop goes from 0 to argc, but your inside your loop you always use argv[1], if you didn't pass any arguments you're going out of bounds, because argv[0] is always the path to your executable.
atoi is a function from C, and when it fails to parse it's argument as an int, it returns 0, replace it with std::stoi, and you will get and execption if the conversion failed. You can catch this exception with try/catch, and then check the string that you tried to convert to int.
Well, this
#include <iostream>
#include <sstream>
#include <string>
#include <cstdlib>
#include <conio.h>
using namespace std;
int main(int argc, char* argv[]) {
int x = 0;
for (x = 0; x < argc; x++)
{
cout << argv[x];
}
return 0;
}
just prints the path to the .exe, the path is a string, it has no numbers. And as I understood from my "research" about command line arguments, you need to use your program through a command line, a terminal, to initialise the argv argument.
Link : https://www.tutorialspoint.com/cprogramming/c_command_line_arguments.htm
Also, as I understood at least, the argv[0] is always the path of the .exe
I hope I will be of some help, if I am mistaken at something, pls tell me where and I will correct my self by editing the answer

Unable to pass the arguments to R function from cpp code

My cpp source code:
#include <iostream>
using namespace std;
int main(int argc, char **argv) {
cout<<"Before"<<endl;
system("Rscript /Desktop/R_TENS/rtest.R");
return 0;
}
rtest.R
rtest = function(input ,output){
a <- input
b<- output
outpath<-a*b
print(a*b)
return(outpath)
}
Compile: g++ name.cpp
This creates a executable file. But how the parameters (eg:2,3) is could be passed?
./a.out 2 3 is not working. Expected result for this input is 6.

GMP Library - File I/O

Okay. So I'm using GMP library to calculate big numbers. I've got code like this:
#include <stdio.h>
#include <iostream>
#include <cstdlib>
#include <string>
#include <gmp.h>
using std::cout;
using std::endl;
int main(int argc, char** argv)
{
FILE *file;
file = fopen("data.txt", "wt");
int number=atoi(argv[1]), i=1;
mpz_t a; mpz_init(a);
mpz_t b; mpz_init(b);
mpz_set_ui(b, 1);
cout<<a<<endl;
for (; number>0; number--, i++)
{
cout<<i<<". "<<b<<endl;
mpz_add(b,b,a);
mpz_sub(a,b,a);
}
mpz_clear(a);
mpz_clear(b);
fclose(file);
}
And I wanted to print numbers (a,b) to a .txt file. How can I do it? Tried fprintf(), but it doesn't seem to work
You should use gmp_fprintf().
The format specifier will be %Zd for mpz_t, so the code will be like
gmp_fprintf(file, "%Zd\n%Zd\n", a, b);
Other format specifiers and samples are in GNU MP 6.1.0: Formatted Output Strings

C++ SHA256 is different from command line SHA256

I'm trying to compute a SHA256 hash of the string iEk21fuwZApXlz93750dmW22pw389dPwOkm198sOkJEn37DjqZ32lpRu76xmw288xSQ9
When I run my C++ code, I get a string that's not even a valid SHA256 hash. However, when I run echo -n iEk21fuwZApXlz93750dmW22pw389dPwOkm198sOkJEn37DjqZ32lpRu76xmw288xSQ9 | openssl sha256, I get the correct hash. Here's my C++ code:
#include <iostream>
#include <time.h>
#include <sstream>
#include <string>
#include <iomanip>
#include <typeinfo>
#include <openssl/sha.h>
#include <cstdio>
#include <cstring>
std::string hash256(std::string string) {
unsigned char digest[SHA256_DIGEST_LENGTH];
SHA256_CTX ctx;
SHA256_Init(&ctx);
SHA256_Update(&ctx, string.c_str(), std::strlen(string.c_str()));
SHA256_Final(digest, &ctx);
char mdString[SHA256_DIGEST_LENGTH*2+1];
for (int i = 0; i < SHA256_DIGEST_LENGTH; i++)
std::sprintf(&mdString[i*2], "%02x", (unsigned int)digest[i]);
return std::string(mdString);
}
int main(int argc, char *argv[])
{
const char *hash = hash256("iEk21fuwZApXlz93750dmW22pw389dPwOkm198sOkJEn37DjqZ32lpRu76xmw288xSQ9").c_str();
std::cout << hash << std::endl;
return 0;
}
Another thing to note: When I run my code in an online compiler, such as Coliru, I get the correct hash. I am compiling with G++ on Cygwin with OpenSSL version OpenSSL 1.0.1g 7 Apr 2014
As pointed out by #Alan Stokes, you have Undefined Behavior due to a dangling reference to the internal structure of the string. Change your declaration of hash in main:
std::string hash = hash256("...");