Unable to compile crypto for SHA-256 hashing on Devc - dev-c++

#include <..\cryptopp\dll.h>
#include <..\cryptopp\sha.h>
#include <..\cryptopp\hex.h>
#include<..\cryptopp\files.h>
#include <iostream>
#include<string>
using namespace std;
using namespace CryptoPP;
const int MAX_PHRASE_LENGTH=250;
int main(int argc, char *argv[]) {
CryptoPP::SHA256 hash;
byte digest[ CryptoPP::SHA256::DIGESTSIZE ];
std::string message = "Hello World!";
hash.CalculateDigest( digest, (const byte*)message.c_str(), message.length());
CryptoPP::HexEncoder encoder;
std::string output;
encoder.Attach( new CryptoPP::StringSink( output ) );
encoder.Put( digest, sizeof(digest) );
encoder.MessageEnd();
std::cout << "Input string: " << message << std::endl;
std::cout << "SHA256: " << output << std::endl;
return 0;
}
errors
Compiler: Default compiler
Executing g++.exe...
g++.exe "C:\Users\Pr\Desktop\Work\encrypt\sha256\sampeSHA256.cpp" -o "C:\Users\Pr\Desktop\Work\encrypt\sha256\sampeSHA256.exe" -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"C:\Dev-Cpp\include\c++\3.4.2\backward" -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32" -I"C:\Dev-Cpp\include\c++\3.4.2" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib"
In file included from C:/Dev-Cpp/include/c++/3.4.2/backward/..\cryptopp/seckey.h:8,
from C:/Dev-Cpp/include/c++/3.4.2/backward/..\cryptopp/rijndael.h:7,
from C:/Dev-Cpp/include/c++/3.4.2/backward/..\cryptopp/aes.h:4,
from C:/Dev-Cpp/include/c++/3.4.2/backward/..\cryptopp\dll.h:11,
from C:\Users\Pr\Desktop\Work\encrypt\sha256\sampeSHA256.cpp:1:
C:/Dev-Cpp/include/c++/3.4.2/backward/..\cryptopp/cryptlib.h:277: error: function std::string CryptoPP::NameValuePairs::GetValueNames() const' definition is marked dllimport.
C:/Dev-Cpp/include/c++/3.4.2/backward/..\cryptopp\/cryptlib.h:283: error: functionbool CryptoPP::NameValuePairs::GetIntValue(const char*, int&) const' definition is marked dllimport.
C:/Dev-Cpp/include/c++/3.4.2/backward/..\cryptopp/cryptlib.h:287: error: function int CryptoPP::NameValuePairs::GetIntValueWithDefault(const char*, int) const' definition is marked dllimport.
C:/Dev-Cpp/include/c++/3.4.2/backward/..\cryptopp\/cryptlib.h:291: error: functionstatic void CryptoPP::NameValuePairs::ThrowIfTypeMismatch(const char*, const std::type_info&, const std::type_info&)' definition is marked dllimport.
C:/Dev-Cpp/include/c++/3.4.2/backward/..\cryptopp/cryptlib.h:301: error: function `void CryptoPP::NameValuePairs::GetRequiredIntParameter(const char*, const char*, int&) const' definition is marked dllimport.
In file included from C:/Dev-Cpp/include/c++/3.4.2/backward/..\cryptopp/aes.h:4,
from C:/Dev-Cpp/include/c++/3.4.2/backward/..\cryptopp\dll.h:11,
from C:\Users\Pr\Desktop\Work\encrypt\sha256\sampeSHA256.cpp:1:
C:/Dev-Cpp/include/c++/3.4.2/backward/..\cryptopp/rijndael.h:15: error: function `static const char* CryptoPP::Rijndael_Info::StaticAlgorithmName()' definition is marked dllimport.
In file included from C:/Dev-Cpp/include/c++/3.4.2/backward/..\cryptopp\dll.h:16,
from C:\Users\Pr\Desktop\Work\encrypt\sha256\sampeSHA256.cpp:1:
C:/Dev-Cpp/include/c++/3.4.2/backward/..\cryptopp/des.h:58: error: function `static const char* CryptoPP::DES_EDE2_Info::StaticAlgorithmName()' definition is marked dllimport.
C:/Dev-Cpp/include/c++/3.4.2/backward/..\cryptopp/des.h:82: error: function `static const char* CryptoPP::DES_EDE3_Info::StaticAlgorithmName()' definition is marked dllimport.
In file included from C:/Dev-Cpp/include/c++/3.4.2/backward/..\cryptopp\dll.h:37,
from C:\Users\Pr\Desktop\Work\encrypt\sha256\sampeSHA256.cpp:1:
C:/Dev-Cpp/include/c++/3.4.2/backward/..\cryptopp/skipjack.h:15: error: function `static const char* CryptoPP::SKIPJACK_Info::StaticAlgorithmName()' definition is marked dllimport.
Execution terminated

Remove #include <..\cryptopp\dll.h>
As Fraser said, use -L<path to Crypto++>.
As Fraser said, use either -lcryptopp or -lcrypto++. The one you use depends on what you have. By default, its -lcryptopp.

Related

std::function of a value templated method compiles with clang and g++ but not with msvc

The following code compiles with clang v5.0.0 and g++ v8.1.0 but fails with visual studio (2013 and 2017):
#include <string>
#include <functional>
#include <iostream>
template <const char* name>
std::string fct() {
return name;
}
const char toto[] = "toto";
std::function<std::string()> fctptr = fct<toto>;
int main(){
std::cout << fctptr() << std::endl;
}
The error is the following:
main.cpp(11): error C2440: 'initializing' : cannot convert from 'std::string (__cdecl *)(void)' to 'std::function<std::string (void)>'
1> No constructor could take the source type, or constructor overload resolution was ambiguous
I tried to replace the std::function with a typedef to a function pointer, as such:
typedef std::string(*Fctptr)();
Fctptr fctptr = fct<toto>;
However, I got the same error.
Is it a bug with msvc compiler, or is the above code not standard compliant.
FWIW, the following failed to compile using g++ 6.4.0 (g++ -std=c++11).
#include <string>
#include <functional>
#include <iostream>
template <const char* name>
std::string fct() {
return name;
}
const char toto[] = "toto";
std::function<std::string()> fctptr = fct<toto>;
int main(){
std::cout << fctptr() << std::endl;
}
Here's the error message:
socc.cc:11:43: error: the value of ‘toto’ is not usable in a constant expression
std::function<std::string()> fctptr = fct<toto>;
^~~~
socc.cc:10:12: note: ‘toto’ was not declared ‘constexpr’
const char toto[] = "toto";
Changing the definition of toto to
constexpr char toto[] = "toto";
resolved the problem.

Invalid use of non-static data member for accesing array in a struct

I am trying to work with JUCE Demo, and extract portions of BinaryData.cpp and OpenGLDemo.cpp into my own class. Unfortunately, I run into a problem I can't really parse, which I've managed to reduce in this minimal example of three files: main.cpp, mystuff.cpp and mystuff.h:
main.cpp:
// g++ -std=c++11 main.cpp mystuff.cpp -o main
#include "mystuff.h"
int main(int argc, char* argv[])
{
MyStuff tmpstuff;
std::cout << "hello world" << tmpstuff.temp_binary_data_7[0] << std::endl ;
}
mystuff.h
#include <iostream>
class MyStuff
{
public:
MyStuff();
~MyStuff();
// from BinaryData.cpp:
//~ static const unsigned char temp_binary_data_7[] =
//~ { 35,32,77,97,120,50,79,98,106,32,86,101,114,115,105,111,110,32,52,46,48,32,77,97,114,32,49,48,116,104,44,32,50,48,48,49,10,35,10,35,32,111,98,106,101,99,116,32,84,101,97,112,111,116,48,49,32,116,111,32,99,111,109,101,32,46,46,46,10,35,10,118,32,32,53,
//~ 46,57,50,57,54,56,56,32,52,46,49,50,53,48,48,48,32,48,46,48,48,48,48,48,48,10,118,32,32,53,46,56,51,50,48,51,49,32,52,46,52,57,52,49,52,49,32,48,46,48,48,48,48,48,48,10,118,32,32,53,46,57,52,53,51,49,51,32,52,46,54,49,55,49,56,56,32,48,46,48,48,48,48,
//~ 48,48,10,118,32,32,54,46,49,55,53,55,56,49,32,52,46,52,57,52,49,52,49,32,48,46,48,48,48,48,48,48,10,118,32,32,54,46,52,50,57,54,56,56,32,52,46,49,50,53,48,48,48,32,48,46,48,48,48,48,48,48,10,118,32,32,53,46,51,56,55,49,56,56,32,52,46,49,50,53,48,48,48
//~ };
// move definition to .cpp because of 'error: in-class initialization of static data member ‘const unsigned char MyStuff::temp_binary_data_7 []’ of incomplete type'
static const unsigned char temp_binary_data_7[];
const char* teapot_obj = (const char*) temp_binary_data_7;
// from OpenGLDemo.cpp:
struct Shape
{
Shape()
{
std::cout << "initializing " << static_cast<void*>(teapot_obj) << std::endl ;
}
};
};
mystuff.cpp:
#include "mystuff.h"
const unsigned char MyStuff::temp_binary_data_7[] =
{ 35,32,77,97,120,50,79,98,106,32,86,101,114,115,105,111,110,32,52,46,48,32,77,97,114,32,49,48,116,104,44,32,50,48,48,49,10,35,10,35,32,111,98,106,101,99,116,32,84,101,97,112,111,116,48,49,32,116,111,32,99,111,109,101,32,46,46,46,10,35,10,118,32,32,53,
46,57,50,57,54,56,56,32,52,46,49,50,53,48,48,48,32,48,46,48,48,48,48,48,48,10,118,32,32,53,46,56,51,50,48,51,49,32,52,46,52,57,52,49,52,49,32,48,46,48,48,48,48,48,48,10,118,32,32,53,46,57,52,53,51,49,51,32,52,46,54,49,55,49,56,56,32,48,46,48,48,48,48,
48,48,10,118,32,32,54,46,49,55,53,55,56,49,32,52,46,52,57,52,49,52,49,32,48,46,48,48,48,48,48,48,10,118,32,32,54,46,52,50,57,54,56,56,32,52,46,49,50,53,48,48,48,32,48,46,48,48,48,48,48,48,10,118,32,32,53,46,51,56,55,49,56,56,32,52,46,49,50,53,48,48,48
};
MyStuff::MyStuff() {
}
MyStuff::~MyStuff() {
}
When I compile with g++, I get this:
$ g++ -std=c++11 main.cpp mystuff.cpp -o main
In file included from main.cpp:3:0:
mystuff.h: In constructor ‘MyStuff::Shape::Shape()’:
mystuff.h:18:42: error: invalid use of non-static data member ‘MyStuff::teapot_obj’
const char* teapot_obj = (const char*) temp_binary_data_7;
^
mystuff.h:25:58: error: from this location
std::cout << "initializing " << static_cast<void*>(teapot_obj) << std::endl ;
^
In file included from mystuff.cpp:1:0:
mystuff.h: In constructor ‘MyStuff::Shape::Shape()’:
mystuff.h:18:42: error: invalid use of non-static data member ‘MyStuff::teapot_obj’
const char* teapot_obj = (const char*) temp_binary_data_7;
^
mystuff.h:25:58: error: from this location
std::cout << "initializing " << static_cast<void*>(teapot_obj) << std::endl ;
^
This happens only when the struct Shape code exists in mystuff.h - if you delete it, then the code compiles and runs fine.
So what are my options? How can I define the struct Shape (or the other variables) so that it can refer to teapot_obj without compilation errors?
Ok, managed to fix it by just throwing expressions here and there, but it would still be great to read an answer that explains what is actually going on... here are my changes - only in mystuff.h:
mystuff.h:
#include <iostream>
class MyStuff
{
public:
MyStuff();
~MyStuff();
static const unsigned char temp_binary_data_7[];
static constexpr const char* teapot_obj = (const char*) temp_binary_data_7;
// from OpenGLDemo.cpp:
struct Shape
{
Shape()
{
std::cout << "initializing " << static_cast<const void*>(teapot_obj) << std::endl ;
}
};
Shape tmptest;
};
So, basically:
const char* teapot_obj = (const char*) temp_binary_data_7;
had to change into:
static constexpr const char* teapot_obj = (const char*) temp_binary_data_7;
... which then means I have to make a static_cast<const void*> (instead of just static_cast<void*>) to print out the object address; and finally, have to add a Shape tmptest; so that the constructor of Shape runs at least once, so we can have something printed.
And now, the program runs without problems:
$ g++ -std=c++11 main.cpp mystuff.cpp -o main
$ ./main
initializing 0x400c60
hello world#

Redefinition of custom delete

I am getting an error saying that I have redefined a function custom_delete.
header.h
#include <iostream>
#include <string>
static int unfreed_count = 0;
#define DELETE(O) custom_delete(O,__PRETTY_FUNCTION__, __LINE__)
void custom_delete(void* ptr, const std::string& function_name, unsigned int line_number) {
unfreed_count--;
std::cout << "delete called in " + function_name + ":" << line_number << std::endl;
std::cout << "unfreed_count: = " << unfreed_count << std::endl << std::endl;
free(ptr);
}
main.cpp
#include "header.h"
int main(int argc, char* argv[]) {
int* ptr = new int;
DELETE(ptr);
}
This code results in the following error message upon attempted compilation:
main.cpp: In function 'void custom_delete(void*, const string&, unsigned int)':
main.cpp:5:6: error: redefinition of 'void custom_delete(void*, const string&, unsigned int)'
void custom_delete(void* ptr, const std::string& function_name, unsigned int line_number) {
^
In file included from main.cpp:21:0:
header.h:7:6: note: 'void custom_delete(void*, const string&, unsigned int)' previously defined here
void custom_delete(void* ptr, const std::string& function_name, unsigned int line_number) {
^
I was trying to make a custom version of delete that printed out when it was used. This was in a larger project, so I made a separate project with just these two files and got the same errors.
I tried commenting out all the code inside the custom_delete function. I also tried writing the prototype before the macro. I got the same error each time.
---EDIT---
I found there was another file being compiled:
header.cpp
#include "header.h"
// Some commented out functions
If I remove the #include "header.h" everything works. However, I eventually will need to add the functions to header.cpp. These functions need stuff that will be added to header.h. What should I do?
It looks like you don't have include guard in your headers.
Add #pragma once as the first line of your header.h
or for more portable solution, add
#ifndef HEADER_H
#define HEADER_H
as first two lines, and
#endif
as last line of the header.h.
Also, to avoid link errors, add inline keyword to declaration of your function, like inline void custom_delete(...
or move function implementation to .cpp.

Undefined reference when using extern on a c++ object, but not integral type

I'm getting undefined reference errors when trying to use extern on a c++ object. It doesn't appear to happen with integral types. What am I missing?! This code below replicates the problem:
file1.cpp:
#include <string>
const std::string s("test");
int i = 99;
int main()
{
extern void Test();
Test();
}
file2.cpp:
#include <iostream>
#include <string>
extern const std::string s;
extern int i;
void Test()
{
std::cout << s << std::endl;
std::cout << i << std::endl;
}
if i comment out the usage of the 's' std::string variable, the linking errors go away.
There are other questions on SO similar to this, but they all seem to be related to people not defining the variable, which I am!
It's the const on std::string, it gives s internal linkage. Quote from [3.5 Program and linkage]:3:
A name having namespace scope (3.3.6) has internal linkage if it is
the name of
— a variable that is explicitly declared const or constexpr and
neither explicitly declared extern nor previously declared to have
external linkage; or
If you remove const, it works in vc++, and if you define them both extern const, it also works.
// file1.cpp
extern const std::string s("test");
extern const int i = 99;
// file2.cpp
extern const std::string s;
extern const int i;
If you remove any 'extern' from file1.cpp, it can't compile. If the variables are defined const, you can remove 'extern' from file1.cpp.

error: function returning a function

Although there is at least one similar question, I still ask mine since that one hasn't got solved and seems more complicated. I'm trying to simplify mine.
I have a .cpp file that uses .h as below, and compiling these sheds error as follows. Any idea is appreciated. Note that codes are simplified in order to minimally show the problematic parts only.
FC_boost_prove.h:
#ifndef FC_H
#define FC_H
#include <vector>
#include "iostream"
#include "boost/signal.hpp"
#include "boost/bind.hpp"
#include <boost/random.hpp>
typedef boost::signal0<void()> PreUpdateSignal;
typedef PreUpdateSignal::slot_function_type PreUpdateSlot;
typedef boost::signal0<void()> PostUpdateSignal;
typedef PostUpdateSignal::slot_function_type PostUpdateSlot;
class FC {
public:
FC(uint width, uint height) {
std::cout << "In constructor." << std::endl;
}
~FC() {
//Do ...
}
void connectPreUpdate(PreUpdateSlot s) {
preUpdateSignal_.connect(s);
}
void connectPostUpdate(PostUpdateSlot s) {
postUpdateSignal_.connect(s);
}
protected:
PreUpdateSignal preUpdateSignal_;
PostUpdateSignal postUpdateSignal_;
};
#endif
FC_boost_prove.cpp:
#include <iostream>
#include <string>
#include "FC_boost_prove.h"
int main() {
std::cout << "test." << std::endl;
}
Compile error:
$ g++ FC_boost_prove.cpp
In file included from /usr/include/boost/signals/signal_template.hpp:22,
from /usr/include/boost/signals/signal0.hpp:24,
from /usr/include/boost/signal.hpp:19,
from FC_boost_prove.h:7,
from FC_boost_prove.cpp:3:
/usr/include/boost/last_value.hpp: In instantiation of ‘boost::last_value<void()>’:
/usr/include/boost/signals/signal_template.hpp:178: instantiated from ‘boost::signal0<void(), boost::last_value<void()>, int, std::less<int>, boost::function0<void()> >’
FC_boost_prove.h:12: instantiated from here
/usr/include/boost/last_value.hpp:22: error: function returning a function
In file included from /usr/include/boost/signals/signal0.hpp:24,
from /usr/include/boost/signal.hpp:19,
from FC_boost_prove.h:7,
from FC_boost_prove.cpp:3:
/usr/include/boost/signals/signal_template.hpp: In instantiation of ‘boost::signal0<void(), boost::last_value<void()>, int, std::less<int>, boost::function0<void()> >’:
FC_boost_prove.h:12: instantiated from here
/usr/include/boost/signals/signal_template.hpp:330: error: function returning a function
/usr/include/boost/signals/signal_template.hpp:370: error: function returning a function
In file included from /usr/include/boost/function/detail/maybe_include.hpp:13,
from /usr/include/boost/function/function0.hpp:11,
from /usr/include/boost/signals/signal_template.hpp:38,
from /usr/include/boost/signals/signal0.hpp:24,
from /usr/include/boost/signal.hpp:19,
from FC_boost_prove.h:7,
from FC_boost_prove.cpp:3:
/usr/include/boost/function/function_template.hpp: In instantiation of ‘boost::function0<void()>’:
FC_boost_prove.h:24: instantiated from here
/usr/include/boost/function/function_template.hpp:1006: error: function returning a function
/usr/include/boost/function/function_template.hpp: In instantiation of ‘boost::detail::function::basic_vtable0<void()>’:
/usr/include/boost/function/function_template.hpp:856: instantiated from ‘void boost::function0<R>::clear() [with R = void()]’
/usr/include/boost/function/function_template.hpp:752: instantiated from ‘boost::function0<R>::~function0() [with R = void()]’
/usr/include/boost/signals/slot.hpp:105: instantiated from here
/usr/include/boost/function/function_template.hpp:486: error: function returning a function
/usr/include/boost/function/function_template.hpp:643: error: function returning a function
Environment: Ubuntu 10.10, g++ (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5
Why are you specifying boost::signal0<>? The signalN templates are for deficient compilers that can't properly parse function signatures.
Either use signal and specify the function signature, as recommended for modern compilers:
typedef boost::signal<void()> PreUpdateSignal;
typedef boost::signal<void()> PostUpdateSignal;
or use signalN and specify the return type (and every argument type) explicitly, as needed for deficient compilers:
typedef boost::signal0<void> PreUpdateSignal;
typedef boost::signal0<void> PostUpdateSignal;