I am attempting to create a TCP server using boost.asio and following the examples you use co_spawn to start the listener function in a new thread.
When I try to use boost::asio::co_spawn Microsoft Visual Studios tells me it is undefined, I have included all files the boost example does.
TcpServer.h
#include <iostream>
#include <string>
#include <boost/asio/co_spawn.hpp>
#include <boost/asio/detached.hpp>
#include <boost/asio/io_context.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/signal_set.hpp>
#include <boost/asio/write.hpp>
#include <boost/asio/coroutine.hpp>
using namespace boost::asio;
class TcpServer
{
public:
TcpServer(int port);
~TcpServer();
private:
};
TcpServer.cpp
#include "TcpServer.h"
/*
Create a TcpServer on a given port
*/
TcpServer::TcpServer(int port)
{
io_context ioCtx(1);
/* E0020 identifier "co_spawn" is undefined */
co_spawn();
}
TcpServer::~TcpServer()
{
}
The above code tells me that co_spawn is undefined, I have searched other name spaces and checked for alternative functions and there is none. I thought maybe it was outdated documentation but the boost.asio reference for version 1.70.1 still lists co_spawn as a free function. See here
I am using
Boost: 1.70.1
Microsoft Visual Studio 2017: v15.9.7
boost.asio has preprocessor code to detect the capabilities of the compiler and only turn on coroutine support if the compiler supports it.
For MSVC, I think you will need to add the compiler flag /await to the compiler command line option list.
reference: https://learn.microsoft.com/en-us/cpp/build/reference/await-enable-coroutine-support?view=vs-2019
Related
The Question
Programming Arduino is something new to me - I've included what I believe is relevant, though please comment if more code/info is needed.
I'm creating a sketch that uses the ArduinoHttpClient library installed via the IDE's 'Manage Libraries' option.
Yesterday all was well - sketch compiles.
Today I get the error below. Nothing changed between yesterday and today that I can tell.
I have tried the code on other machines - 2 work, 1 doesn't and I cannot tell what is wrong and don't fully understand the actual error.
Can anybody explain a) what the error means and b) what i might be able to do to resolve it?
Code pieces
In file included from
/Users/me/Documents/Arduino/libraries/ArduinoHttpClient/src/ArduinoHttpClient.h:8:0,
from
/Users/me/Documents/Proj/network-comms/ard-mkr-wifi-1010/box_register.h:1,
from
/Users/me/Documents/Proj/network-comms/ard-mkr-wifi-1010/ard-mkr-wifi-1010.ino:11:
/Users/me/Documents/Arduino/libraries/ArduinoHttpClient/src/HttpClient.h:12:1:
error: expected ',' or ';' before 'static' static const int
HTTP_SUCCESS =0; ^~~~~~ exit status 1 Error compiling for board
Arduino MKR WiFi 1010.
The ArduinoHttpClient.h file is as installed and unchanged:
// Library to simplify HTTP fetching on Arduino
// (c) Copyright Arduino. 2016
// Released under Apache License, version 2.0
#ifndef ArduinoHttpClient_h
#define ArduinoHttpClient_h
#include "HttpClient.h"
#include "WebSocketClient.h"
#include "URLEncoder.h"
#endif
box_register.h at line one just includes the lib
#include <ArduinoHttpClient.h>
HttpClient.h from the Arduino lib is as - includes the line being reported:
// Class to simplify HTTP fetching on Arduino
// (c) Copyright MCQN Ltd. 2010-2012
// Released under Apache License, version 2.0
#ifndef HttpClient_h
#define HttpClient_h
#include <Arduino.h>
#include <IPAddress.h>
#include "Client.h"
static const int HTTP_SUCCESS =0; /// <------ LINE 11
// The end of the headers has been reached. This consumes the '\n'
// Could not connect to the server
static const int HTTP_ERROR_CONNECTION_FAILED =-1;
// This call was made when the HttpClient class wasn't expecting it
// to be called. Usually indicates your code is using the class
// incorrectly
static const int HTTP_ERROR_API =-2;
// Spent too long waiting for a reply
ard-mkr-wifi-1010.ino showing line including box_register
// va_start, va_end
#include <stdarg.h>
#include <WiFiNINA.h>
#include <PubSubClient.h>
#include <RTCZero.h>
#include <WiFiUdp.h>
#include <Wire.h>
#include "./arduino_secrets.h"
#include "./box_register.h" // <----- HERE
#include "./ntp.h"
I have recently ran into an error when compiling my program with the boost serialization library.
I have read across many forums that I need to link the boost serialization library (-lboost_serialization). Which, I did.
However, there is one error that I am getting on the code:
/usr/local/boost_1_64_0/boost/serialization/throw_exception.hpp:36: undefined reference to `boost::archive::archive_exception::archive_exception(boost::archive::archive_exception const&)'
So I thought, what if I have to also include the exception library?
So I added this into the libraries for my compiler settings:
-lboost_exception
However, now it is saying that it is unable to find the boost_exception library. Which is interesting because I am able to see it in the file system!
I am wondering if I am doing the right thing or am I forgetting to add something to my compiler settings? Any help will be much appreciated!
Edit:
Here is the code:
// MS compatible compilers support #pragma once
#if defined(_MSC_VER)
# pragma once
#endif
// boost/throw_exception.hpp
//
// Copyright (c) 2002 Peter Dimov and Multi Media Ltd.
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <boost/config.hpp>
#ifndef BOOST_NO_EXCEPTIONS
#include <exception>
#endif
namespace boost {
namespace serialization {
#ifdef BOOST_NO_EXCEPTIONS
inline void throw_exception(std::exception const & e) {
::boost::throw_exception(e);
}
#else
template<class E> inline void throw_exception(E const & e){
throw e;// Error occurs here
}
#endif
} // namespace serialization
} // namespace boost
Here is a code sample where the breakage occurs
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <vector>
#include "boost/serialization/vector.hpp"
#include <string>
#include <fstream>
int main()
{
std::ofstream ofs("/home/phillip/test.txt");
std::vector<std::string> tests;
boost::archive::text_oarchive oa(ofs);
oa << tests;
}
I've seen this happen when linking to the boost library when compiling with a different compiler/version/flags.
E.g. many Boost Serialization answer programs wouldn't link when compiling with Clang on Coliru whereas they'd run fine when compiling with Gcc
I'm making scientific calculator in command line in c++ for my usage and also for practice. I have a problem with compiling it using cmake with mingw on windows. These are my source files:
main.ccp
#include <iostream>
#include <string>
#include "ExpressionCalculations/ExpressionParser.h"
int main()
{
std::string humanReadableExpression;
std::cout<<"Enter expression\n";
std::getline(std::cin, humanReadableExpression);
std::cout<<humanReadableExpression;
ExpressionCalculations::ExpressionParser parser;
auto&& expression = parser.GenerateRpnExpression(humanReadableExpression);
return 0;
}
ExpressionParser.h
#pragma once
#include <memory>
#include <stack>
#include <string>
#include <unordered_map>
namespace ExpressionCalculations
{
class ExpressionParser
{
public:
std::unique_ptr<std::string> GenerateRpnExpression(std::string &humanReadableExpression);
private:
// other code
};
}
ExpressionParser.cpp
#include <memory>
#include <stack>
#include <string>
#include <unordered_map>
#include <iostream>
#include "ExpressionParser.h"
namespace ExpressionCalculations
{
std::unique_ptr<std::string> ExpressionParser::GenerateRpnExpression(
std::string& humanReadableExpression)
{
std::unique_ptr<std::string> rpnExpression;
*rpnExpression="3456";
return rpnExpression;
}
These are cmake files
main CMakeLists.txt
cmake_minimum_required (VERSION 2.8)
project (ScientificCalculator_exe)
add_subdirectory(ExpressionCalculations)
add_executable(ScientificCalculator main.cpp)
target_link_libraries(ScientificCalculator ExpressionCalculations)
module CMakeList.txt
set(calculators ExpressionParser.h ExpressionParser.cpp)
add_library(ExpressionCalculations ${calculators})
When I run it , I can see Enter expression and pass input. Then I get Segmentation fault. However when I remove declaration of ExpressionParser and auto&& expression the string is shown, a string can be inputted and shown in the command. I checked configuration question multiple directories under cmake, https://cmake.org/cmake-tutorial/ and https://www.codeproject.com/Articles/1181455/A-CMake-tutorial-for-Visual-Cplusplus-developers but it seems that I correctly made cmake files. I have no idea why it doesn't work. I use the latest mingw64 on windows with default make compilation parameters.
From the cppreference page on unique_ptr:
The class satisfies the requirements of MoveConstructible and MoveAssignable, but not the requirements of either CopyConstructible or CopyAssignable.
In your ExpressionParser::GenerateRpnExpression function you are attempting to copy the unique_ptr out of the function when you should be moving it. Try return std::move(rpnExpression)
After debugging program compiled with just g++ I found the problem. It was misunderstanding of unique_ptr' default constructor behaviour. I thought it would initialize std::string but after reading doc and checking it it does not initialize object and generates nullptr. Then I looked into Scott Myers's Modern Effective C++ how to initialize the unique_ptr. Instead of std::unique_ptr<std::string> rpnExpression; I used auto rpnExpression = std::make_unique<std::string>();. It works like charm. I checked compiling through cmake and there were not any problems.
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.
How to Add Timeout for a test method in C++ in Microsoft unit testing using CppUnitTestFramework ? Most of the Solutions I found online are for CSharp projects where I can add lines like [TEST_METHOD,TIME_OUT(80)] or such ,but those are not working while testing C++ (VC++) code
I have tried the below code
#include "stdafx.h"
#include "CppUnitTest.h"
#include "../src/factorial_dp.cpp"
#include "stdio.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace spec
{
TEST_CLASS(factorial_dpSpec)
{
public:
//Add Timout for these methods
TEST_METHOD(Smallnumber)
{
int result = fact(5);
Assert::AreEqual(120, result, L"5 fact should be 120", LINE_INFO());
}
};
}
Use managed test classes.
and you can keep Timeouts in that .
[TestMethod(), Timeout(3000)]
void functionName()
{
//
}