error: ‘GlobalRNG’ was not declared in this scope - c++

I'm using Crypto++ to encrypt files in C++. And I'm using the code below.
It doesn't contain the headers files so I added my own :
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <fstream>
#include <cryptopp/cryptlib.h>
#include <cryptopp/sha.h>
#include <cryptopp/secblock.h>
#include <cryptopp/files.h>
#include <cryptopp/queue.h>
#include <cryptopp/hex.h>
#include <cryptopp/base64.h>
#include <cryptopp/filters.h>
#include <cryptopp/osrng.h>
#include <cryptopp/integer.h>
#include <cryptopp/dh.h>
#include <cryptopp/sha.h>
#include <cryptopp/modes.h>
#include <cryptopp/eax.h>
#include <cryptopp/tea.h>
#include <cryptopp/blowfish.h>
#include <cryptopp/pssr.h>
#include <cryptopp/rsa.h>
#include <cryptopp/nbtheory.h>
#include <cryptopp/eccrypto.h>
#include <cryptopp/oids.h>
#include <cryptopp/modes.h>
#include <cryptopp/gzip.h>
#include <cryptopp/blowfish.h>
#include <cryptopp/rsa.h>
#include <cryptopp/rng.h>
#include <cryptopp/cryptlib.h>
#include <cryptopp/filters.h>
#include <cryptopp/rdrand.h>
using namespace std;
using namespace CryptoPP;
But unfortunately the code doesn't work
Saying that the GlobalRNG is not declared !
error: ‘GlobalRNG’ was not declared in this scope
I googled and kept looking for a solution for 2 days i found that it's a bug and fixed but i'm having the latest version : 5.6.3 !
So i really don't know why this error is showing !

In the version 5.6.3 GlobalRNG is defined in the file validate.h, as:
// Functions that need a RNG; uses AES inf CFB mode with Seed.
CryptoPP::RandomNumberGenerator & GlobalRNG();
Just add this inclusion:
#include <cryptopp/validate.h>
to solve definition problem.

GloablaRNG is part of testing and bench-marking. It should not be part of the library proper (i.e., libcryptopp.a or libcryptopp.so). If your programs are complaining about a missing GloablaRNG, then the library was cross-contaminated with some of the testing and bench-marking gear.
These are the files used for testing and bench-marking. They should not be included in your build of the library or your project:
validate.h
bench.h
test.cpp
bench1.cpp, bench2.cpp
validat0.cpp, validat1.cpp, validat2.cpp, validat3.cpp
datatest.cpp, regtest.cpp, fipsalgt.cpp, dlltest.cpp
You are free to use a function called GlobalRNG(). Here's how its used in the library's test and bench-marking gear. But you might consider using an AutoSeededRandomPool instead. The AutoSeededRandomPool is a PGP-style generator, and its seeded from /dev/urandom, /dev/srandom, /dev/random or the Windows entropy pool.
Declaration in validate.h
NAMESPACE_BEGIN(CryptoPP)
NAMESPACE_BEGIN(Test)
CryptoPP::RandomNumberGenerator & GlobalRNG();
NAMESPACE_END // Test
NAMESPACE_END // CryptoPP
Definition in test.cpp
NAMESPACE_BEGIN(CryptoPP)
NAMESPACE_BEGIN(Test)
ANONYMOUS_NAMESPACE_BEGIN
OFB_Mode<AES>::Encryption s_globalRNG;
NAMESPACE_END
RandomNumberGenerator & GlobalRNG()
{
return dynamic_cast<RandomNumberGenerator&>(s_globalRNG);
}
NAMESPACE_END // Test
NAMESPACE_END // CryptoPP
Seeding in test.cpp
// Don't do this in production because it creates a deterministic generator
OFB_Mode<AES>::Encryption& aesg = dynamic_cast<OFB_Mode<AES>::Encryption&>(Test::GlobalRNG());
aesg.SetKeyWithIV((byte *)seed.data(), 16, (byte *)seed.data());
A lot of folks have had this problem over the years. At Crypto++ 6.0, we moved GlobalRNG() into the Test namespace. Test is a new namespace, and we hope Test::GlobalRNG() will provide the signals that something is amiss in your library build or project configuration.
Also see Issue 379, Add Test namespace within CryptoPP namespace and Commit 73836e58a5f5c11c.

Related

std::queue included in precompiled header is not part of namespace std

I have a problem with the inclusion of header "queue" via a precompiled header file in c++ (VS 2019).
My Visual Studio solution consists of two projects, one engine project (static library) and one sandbox project (links the engine project) to test the engine's functionality. The engine project uses a precompiled header file, in which i just included "queue" since i am implementing a message system. Both projects are using C++17.
Visual Studio compiles the engine project without a problem, the sandbox project then throws the following error while compiling:
Error C2039 'queue': is not a member of 'std' (path\to\engine\message_handler.h)
As soon as I include "queue" directly in message_handler.h (see code excerpt), the error vanishes and the project starts up just fine.
I have been using the precompiled header for almost a year now and never had any problems.
Can anyone help me with this?
Following are the relevant excerpts from my code.
Precompiled header:
#pragma once
//memory
#include <memory>
//timing
#include <chrono>
#include <ctime>
//container
#include <vector>
#include <queue>
#include <unordered_map>
#include <array>
//string and streams
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
//misc
#include <algorithm>
#include <cstdint>
#include <cmath>
#include <random>
message.h
struct Message
{
Message(){}
uint32_t id_;
};
message_handler.h
#pragma once
//#include <queue> //fixes the issue
#include "message.h"
using UnqPtr = std::unique_ptr<Message>;
class MessageHandler
{
public:
MessageHandler();
private:
static constexpr uint32_t maxMessages_ = 10000;
std::queue<UnqPtr<Message>, std::vector<UnqPtr<Message>>> msqQueue_;
};
message_handler.cpp
#include "trpch.h" //precompiled header
#include "message_handler.h"
MessageHandler::MessageHandler()
{
//reserve space in the underlying vector
std::vector<UnqPtr<Message>> queue;
queue.reserve(maxMessages_);
msqQueue_ = std::queue{queue};
}

Trouble with including external directories in C++ with ViconDataStreamSDK

#include <iostream>
#include <cassert>
#include <vector>
#include <algorithm>
#include <functional>
#include <limits>
#include <math.h>
#include "DataStreamClient.h" //[x]
#include "stdafx.h"
using namespace ViconDataStreamSDK::CPP;
{ ...
This is the start of my file that uses the header file ViconDatastreamSDK.h but in every instance that a class from the file is called Visual Studio gives the error:
'ViconDataStreamSDK': is not a class or namespace name
The solution explorer shows that the file is included in the external directories.
How do I get VisualStudio to recognize these classes?

CLion C++ fatal error: 'process.h' file not found

When I use CLion on a Mac to compile C++ code for highlight removal in a single image, there is an error:
Please help me fix it.
#ifndef QX_CVPR09_CTBF_BASIC_H
#define QX_CVPR09_CTBF_BASIC_H
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <numeric>
#include <vector>
#include <process.h>
#include <direct.h>
#include <io.h>
#include <time.h>
#include <string>
#include <memory.h>
#include <algorithm>
#include <functional> // For greater<int>()
#include <iostream>
#if _MSC_VER > 1020 // if VC++ version is > 4.2
using namespace std; // std c++ libs implemented in std
#endif
#define QX_DEF_PADDING 10
#define QX_DEF_THRESHOLD_ZERO 1e-6
class qx_timer {public: void start(); float stop(); void time_display(char *disp=""); void fps_display(char *disp=""); private: clock_t m_begin; clock_t m_end;};
It's a part of my code. The full code is too long.
process.h
process.h is a C header file which contains function declarations and
macros used in working with threads and processes. Most C compilers
that target DOS, Windows 3.1x, Win32, OS/2, Novell NetWare or DOS
extenders supply this header and the library functions in their C
library. Neither the header file nor most of the functions are defined
by either the ANSI/ISO C standard or by POSIX.
Depends on which platform you compile and what standard you use. If you are on linux or compile with c99/ansi standard then this header will probably just not be available (which might be your error)

g++ doesn't detect that a file exists, and won't link it

I downloaded the Dlib example for face detection:http://dlib.net/face_detection_ex.cpp.html
and only changed three lines, for where my libraries are:
Before:
#include <dlib/image_processing/frontal_face_detector.h>
#include <dlib/gui_widgets.h>
#include <dlib/image_io.h>
#include <iostream>
using namespace dlib;
using namespace std;
After:
#include <~/dlib/image_processing/frontal_face_detector.h>
#include <~/dlib/gui_widgets.h>
#include <~/dlib/image_io.h>
#include <iostream>
using namespace dlib;
using namespace std;
And yet for some reason, g++ can't find them:
$ g++ face_detection_ex.cpp -o face_detection_ex
face_detection_ex.cpp:40:59: fatal error: ~/dlib/image_processing/frontal_face_detector.h: No such file or directory
#include <~/dlib/image_processing/frontal_face_detector.h>
^
compilation terminated.
Despite all of the libraries being in that folder:
[me]#[me]-Dell-ProBook-6760g:~$ cat ~/dlib/image_processing/frontal_face_detector.h
// Copyright (C) 2013 Davis E. King (davis#dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_FRONTAL_FACE_DETECTOr_Hh_
#define DLIB_FRONTAL_FACE_DETECTOr_Hh_
#include "frontal_face_detector_abstract.h"
#include "../image_processing/object_detector.h"
#include "../image_processing/scan_fhog_pyramid.h"
#include <sstream>
#include "../compress_stream.h"
#include "../base64.h"
namespace dlib
Why won't this work? What am I doing wrong here?

‘hash’ is already declared in this scope using tr1::hash;

I am trying to compile C++ code shown below but I got an error saying,
In file included from src/LM.h:3:0,
from src/LM.cpp:1:
src/common.h:30:13: error: ‘hash’ is already declared in this scope
using tr1::hash;
This is the command I used to compile the files below.
g++ -std=c++11 -Wall src/Foo.cpp
Foo.cpp
#include "Foo.h"
...
Foo.h
#ifndef FOO_H
#define FOO_H
#include "common.h"
//more code here
#endif
common.h
#ifndef _COMMON_H_
#define _COMMON_H_
#include <iostream>
#include <fstream>
#include <cmath>
#include <cassert>
#include <cstdlib>
#include <utility>
#include <vector>
#include <string>
#include <array>
#include <algorithm>
#include <set>
#include <tr1/unordered_map>
#include <tr1/functional>
namespace std {
using tr1::unordered_map;
using tr1::hash;
} // namespace std
using namespace std;
//more code here
#endif
I want the source code to use std::tr1::unordered_map and std::tr1::hash rather than std::unordered_map and std::hash(Actually I am making some modifications to distributed files which does uses std::tr1::unordered_map and std::tr1::hash).
What is possibly wrong with my codes?
UPD:
https://github.com/clab/fast_align/blob/master/src/port.h seems to do the same thing as mine. However, this compiles without any problem... Have any idea?
There is already std::hash in C++11. You cannot redefine it. You can use another name for tr1::hash.
Probably the best idea (if you really want to use std::tr1::hash/std::tr1::unordered_map instead of C++11 structures) is to write your own namespace in which using all structures, that you want without std::hash/std::unordered_map.
namespace common
{
using std::tr1::hash;
using std::tr1::unordered_map;
using std::vector;
// and so on
}