I am trying to compile my boost simple code:
#include <iostream>
#include <boost/thread.hpp>
void workerFunc(const char* msg, float delay_ms)
{
boost::posix_time::milliseconds workTime(delay_ms);
std::cout << "Worker: running, message = " << msg << std::endl;
// Pretend to do something useful...
boost::this_thread::sleep(workTime);
std::cout << "Worker: finished" << std::endl;
}
int main(int argc, char* argv[])
{
std::cout << "main: startup" << std::endl;
boost::thread workerThread(workerFunc, "Hello, Boost!", 2.5e3);
std::cout << "main: waiting for thread" << std::endl;
workerThread.join();
std::cout << "main: done" << std::endl;
return 0;
}
using g++ with this command
g++ main.cpp -o main
but i get an error like this:
main.cpp: In function `void workerFunc(const char*, float)':
main.cpp:7: error: `boost::posix_time' has not been declared
main.cpp:7: error: `milliseconds' was not declared in this scope
main.cpp:7: error: expected `;' before "workTime"
main.cpp:12: error: `boost::this_thread' has not been declared
main.cpp:12: error: `workTime' was not declared in this scope
main.cpp: In function `int main(int, char**)':
main.cpp:21: error: no matching function for call to `boost::thread::thread(void (&)(const char*, float), const char[14], double)'
/usr/include/boost/thread/thread.hpp:35: note: candidates are: boost::thread::thread(const boost::thread&)
/usr/include/boost/thread/thread.hpp:38: note: boost::thread::thread(const boost::function0<void, std::allocator<boost::function_base> >&)
/usr/include/boost/thread/thread.hpp:37: note: boost::thread::thread()
What's wrong and how should i compile it...?
According to this
http://www.boost.org/doc/libs/1_45_0/doc/html/date_time/posix_time.html
You need this
#include "boost/date_time/posix_time/posix_time.hpp"
I suspect you have an old version of Boost installed in your system. Read the file /usr/include/boost/version.hpp. And depending on which version you have, consult the version specific documentation (see Boost Documentation). Or install the latest version of Boost by either using you system's packaging functionality, if available, or manually following the install directions (see Getting Started on Unix Variants).
Since the compiler doesn't recognize some type, it means that you are missing some includes.
For posix_time you need to add #include "boost/date_time/posix_time/posix_time.hpp" on top of your code.
You need to include the header declaring posix_time. Look at the boost doc to see which it is (you can try #include "boost/date_time/posix_time/posix_time_system.hpp" but I'm not sure that would be enough).
Related
I would like to compile my c++11 project (recently moved to c++11) with MinGW. And I have compiling errors about c++11 code like "std::thread not found".
I used the last MinGW with gcc 5.3.0 (December 2015). A the end, I would like only to compile this example before to compile my big project :
#include <iostream>
#include <thread>
#include <chrono>
void foo()
{
// simulate expensive operation
std::this_thread::sleep_for(std::chrono::seconds(1));
}
void bar()
{
// simulate expensive operation
std::this_thread::sleep_for(std::chrono::seconds(1));
}
int main()
{
std::cout << "starting first helper...\n";
std::thread helper1(foo);
std::cout << "starting second helper...\n";
std::thread helper2(bar);
std::cout << "waiting for helpers to finish..." << std::endl;
helper1.join();
helper2.join();
std::cout << "done!\n";
}
(source : http://en.cppreference.com/w/cpp/thread/thread/join)
I tried "g++ -std=c++11 main.cpp" and "g++ main.cpp -std=c++0x" but I have always those following errors :
main.cpp: In function 'void foo()':
main.cpp:8:10: error: 'std::this_thread' has not been declared
std::this_thread::sleep_for(std::chrono::seconds(1));
^
main.cpp: In function 'void bar()':
main.cpp:14:10: error: 'std::this_thread' has not been declared
std::this_thread::sleep_for(std::chrono::seconds(1));
^
main.cpp: In function 'int main()':
main.cpp:20:5: error: 'thread' is not a member of 'std'
std::thread helper1(foo);
^
main.cpp:23:5: error: 'thread' is not a member of 'std'
std::thread helper2(bar);
^
main.cpp:26:5: error: 'helper1' was not declared in this scope
helper1.join();
^
main.cpp:27:5: error: 'helper2' was not declared in this scope
helper2.join();
^
MinGW mostly does not have a port of glibc which supports pthreading or gthreading like in GCC.
For solving this, the first solution can be installing library of thread headers.
The other solution can be working with GCC compiler.
I am trying to implement mutlithreading in a C++11 program.
I separated the threading from my main program and tried to get the most basic example working:
#include <iostream>
#include <thread>
void first_procedure() {
std::cout << "First procedure output." << std::endl;
}
void second_procedure() {
std::cout << "Second procedure output." << std::endl;
}
int main() {
std::thread first_thread(first_procedure);
std::thread second_thread(second_procedure);
first_thread.join();
second_thread.join();
return 0;
}
However, even with this example, I get the following error:
c:\program files (x86)\dev-cpp\mingw64\lib\gcc\x86_64-w64-mingw32\4.8.1\include\c++\thread In function 'bool std::operator<(std::thread::id, std::thread::id)':
88 30 c:\program files (x86)\dev-cpp\mingw64\lib\gcc\x86_64-w64-mingw32\4.8.1\include\c++\thread [Error] no match for 'operator<' (operand types are 'std::thread::native_handle_type {aka ptw32_handle_t}' and 'std::thread::native_handle_type {aka ptw32_handle_t}')
I am using the Orwell Dev-C++ 5.7.1 with TDM-GCC 4.7.1 64-bit on Windows 8. When calling the linker, I add -static-libgcc -std=c++11.
Edit 1: I joined the threads and get the same error message.
This was noticed in the comments first:
From your error messages and supplementary details:
compiler: TDM-GCC 4.7.1
includes: gcc\x86_64-w64-mingw32\4.8.1\include
Your compiler is attempting to use libraries that are for a different compiler.
Either use 4.7.1 or 4.8.1, not a mix of the two.
The good news is that your code looks fine. Sort out the toolchain and it should compile without issue.
I have error: ‘class std::queue<int>’ has no member named ‘swap’ while compiling following code
#include <iostream> // std::cout
#include <queue> // std::queue
int main ()
{
std::queue<int> foo,bar;
foo.push (10); foo.push(20); foo.push(30);
bar.push (111); bar.push(222);
foo.swap(bar);
std::cout << "size of foo: " << foo.size() << '\n';
std::cout << "size of bar: " << bar.size() << '\n';
return 0;
}
I'm using g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3 for compiling this code,can anyone have a idea for this error?
Use:
std::swap(foo, bar);
It appear that since c++11, you have std::queue::swap
http://www.cplusplus.com/reference/queue/queue/swap-free/
g++ 4.6 appears to not accept -std=c++11, so you must upgrade your compiler for this method to work.
[edit]
g++ 4.6 accepts -std=c++0x to enable c++11
When building/compiling I am getting this error:
C:\Ethe\main.cpp: In function 'int main()':
C:\Ethe\main.cpp:11:4: error: 'zmq' has not been declared
C:\Ethe\main.cpp:11:19: error: expected ';' before 'context'
C:\Ethe\main.cpp:12:4: error: 'zmq' has not been declared
C:\Ethe\main.cpp:12:18: error: expected ';' before 'socket'
C:\Ethe\main.cpp:14:4: error: 'zmq' has not been declared
main.cpp:
#include <zmq.h>
#include <iostream>
#include <string>
int main()
{
std::string tip;
std::cout << "Enter Target IP: ";
std::cin >> tip;
zmq::context_t context (1);
zmq::socket_t socket (context, ZMQ_REQ);
std::cout << "Connecting to " << tip << std::endl;
zmq::socket.connect ("tcp://"+tip+":5555");
return 0;
}
Anyone got any ideas on how i can fix this?
You need to add #include <zmq.hpp> This will include the C++ api of libzmq. However, in the zmq verions 2.x version it was included with the install, nowadays in the zmq-3.x.y version it is not shipped with the library anymore, as you can see from http://github.com/zeromq/zeromq3-x/raw/master/NEWS
The C++ api was excluded from the core library since the less is more strategy from zeromq. It is still downloadable from: https://github.com/zeromq/cppzmq/blob/master/zmq.hpp
This header is written around all C structures and functions the C API zeromq, therefore the entire C++ API is a single headerfile. Downloadable from the link above.
If you wrote zmq.h by yourself, it should be "zmq.h".
The File is at the location /home/shivang/Desktop and the filename is sh1.cpp
Source code for the file is given below
#include iostream
#include json/json.h
#include json/reader.h
using namespace std;
using namespace Json;
int main() {
std::string example = "{\"array\":[\"item1\", \"item2\"], \"not an array\":\"asdf\"}";
Value value;
Reader reader;
bool parsed = reader.parse(example, value, false);
std::cout << parsed;
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
return 0;
}
The following error messages are displayed.
/home/shivang/Desktop/sh1.cpp: In function ‘int main()’:
/home/shivang/Desktop/sh1.cpp:10:2: error: ‘Value’ was not declared in this scope
/home/shivang/Desktop/sh1.cpp:10:8: error: expected ‘;’ before ‘value’
/home/shivang/Desktop/sh1.cpp:11:2: error: ‘Reader’ was not declared in this scope
/home/shivang/Desktop/sh1.cpp:11:9: error: expected ‘;’ before ‘reader’
/home/shivang/Desktop/sh1.cpp:13:16: error: ‘reader’ was not declared in this scope
/home/shivang/Desktop/sh1.cpp:13:38: error: ‘value’ was not declared in this scope
Configuration gcc version 4.5.2 (Ubuntu/Linaro 4.5.2-8ubuntu4)
jsoncpp-src-0.5.0
eclipse-cpp-helios-SR2-linux-gtk
I have never used Json or C++ before. But a little googling around led me to this page. I think adding the following line to your list of includes should help:
#include <json/value.h>