including boost library, prevent print to console - c++

I install boost library and i want to test it. I build and run the following project. But i have a problem. When i build project:
Info: Internal Builder is used for build
g++ -std=c++0x "-IC:\\boost\\include\\boost-1_68" -O0 -g3 -Wall -c -fmessage-length=0
-o "src\\MyTestProject.o" "..\\src\\MyTestProject.cpp"
g++ "-LC:\\boost\\lib\\" -o MyTestProject.exe "src\\MyTestProject.o" -lws2_32
-llibboost_iostreams-mgw63-mt-x32-1_68
-llibboost_filesystem-mgw63-mt-x32-1_68
-llibboost_system-mgw63-mt-x32-1_68
Build Finished. 0 errors, 0 warnings.
After i click run project: "Info: Nothing to build for MyTestProject" and it doesn't print anything to MyTestProject.exe console.
If i comment out <boost/asio.hpp>, it prints "Hello world!". What is the problem? I'm using mingw-64, eclipse CDT, boost_1_68_0 on windows. Thank you!
#include <iostream>
#include <boost/asio.hpp>
//#include <boost/date_time/posix_time/posix_time.hpp>
using namespace std;
int main()
{
//boost::asio::io_context io;
//boost::asio::deadline_timer t(io, boost::posix_time::seconds(5));
//t.wait();
cout << "Hello world!" << std::endl;
return 0;
}

i find the solution. I add libboost_system-mgw63-mt-d-x32-1_68.dll file to my project directory(..\Users\workspace\MyTestProject). Actually, i have already added -L c:\boost\lib and -l libboost_system-mgw63-mt-d-x32-1_68 into linker. Also boost is added to PATH. I don't know why it cannot find the dll, but for now i solved problem in this way.

When you print to the console and immediately return, there is a chance that the console will disappear without giving you a chance to read anything. Try reading an acknowledgement from cin afterwards.
Also, you should always say if (cout), otherwise you do not know if the stream failed.

Related

C++ CodeLite Enable Multithreading?

I'm experiencing a bit of strange behaviour when I try to compile any program that uses multithreading in CodeLite.
I get the error:
terminate called after throwing an instance of 'std::system_error'
what(): Enable multithreading to use std::thread: Operation not premitted.
after some quick googling I found out I had to add "-pthread" to the compiler options.
Note: that CodeLite puts -l in front of libraries, so it does use -lpthread
After I clean, and rebuild the project, I still get the error though. As far as I can tell the build log looks fine?
The truly frustrating part comes about when I compile it manually via the command line, It works just fine.
I've searched, but none of the solutions seem to work for me? perhaps I've missed a step somewhere?
here's my test code.
I should also note I'm using Ubuntu14.04, and CodeLite 9.1.0
#include <iostream>
#include <string>
#include <thread>
void test()
{
std::cout << " Look it works! \n";
}
void int main(int argc, char** argv)
{
std::thread thrd_1 = std::thread(test);
thrd_1.join();
return 0;
}
Any help would be greatly appreciated!
You are passing -pthread in the compiler options. You need to pass it
in the linker options, and in the linker options you do not need
to specify pthread as a library. The -pthread option means
do whatever it is that links the posix threads library on this platform.
$ g++ -c -O0 -std=c++11 -o main.o main.cpp
$ g++ -o threadtest -pthread main.o
$ ./threadtest
Look it works!

code::blocks createprocess "No such file or directory" no idea how to fix

I am VERY new to using the code::blocks compiler and It doesn't seem to be working.
I downloaded the code::blocks and mingw package of of there website and I ran a very simple program that would say "hello world" and every way I try it I keep getting this error:
mingw32-g++.exe -Wall -fexceptions -g -c C:\Users\Owner\Desktop\console_test\main.cpp -o obj\Debug\main.o.
I have looked at all the other articles in stack overflow and I keep seeing people saying to set the enviroment variables or run mingw in a different directory but I have no idea how to do that.
The program:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!" << endl;
return 0;
}

Exec Format Error Eclipse CDT

I have a basic C++ program in Eclipse CDT:
#include <iostream>
using namespace std;
int main()
{
std::cout << "Hello World!";
}
However, when I try to build it, I get an Exec Format Error. Here is the output produced by the compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o hey.o "..\\hey.cpp"
g++: error: spawn: Exec format error
I am using MinGW Toolchain. I am on 64-bit Windows, and I think that may have something to do with it. Would anyone know how to get this program running?
Edit
Running the exact command in command prompt in the directory where my source file is works just fine, without throwing errors, but it still doesn't work in Eclipse
So, I have fixed this myself by installing the 64-bit version of MinGW (http://sourceforge.net/projects/mingw-w64/). It now compiles and builds noramlly

Eclipse c++ build error: no such file or directory

I installed MinGW and CDT following some tutorials. I'm trying to compile and run "hello world" code. There are no errors in eclipse but when I compile the code I get this error:
22:48:32 **** Incremental Build of configuration Debug for project test3 ****
Info: Internal Builder is used for build
g++ "-IC:\\MinGW\\lib\\gcc\\mingw32\\4.8.1\\include\\c++" "-IC:\\MinGW\\lib\\gcc\\mingw32\\4.8.1\\include\\c++\\mingw32" -O0 -g3 -Wall -c -fmessage-length=0 -o "src\\test3.o" "..\\src\\test3.cpp"
g++: error: CreateProcess: No such file or directory
22:48:32 Build Finished (took 135ms)
The code:
#include <iostream>
using namespace std;
int main() {
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
return 0;
}
Might be a silly question to ask, but did you save the file somewhere before building it? I've had an error like this that was solved by saving it on my Desktop or wherever.
Edit: There seems to be several people with problems using MinGW. You mentioned you looked at previous stackoverflow questions, do they include these:
Eclipse CDT error: Unable to compile
MinGW error: No such file or directory exists
Other solutions suggest you should try compiling the program via command line, which...shouldn't be a problem since it's just "Hello, World".

Compiling multithread code with g++ (-Wl,--no-as-needed NOT working)

My problem is actually described here: Compiling multithread code with g++.
But the answer regarding the work around by using "-Wl,--no-as-needed" is not working for me.
I've added -Wl,--no-as-needed -pthread -std=c++0x in different orders also, but I still get the:
terminate called after throwing an instance of 'std::system_error'
what(): Enable multithreading to use std::thread: Operation not permitted"
What to do?
Info:
Ubuntu 12.04LTS
Running Eclipse CDT
g++ v4.8.1
Edit:
I tried building with -Wl,--no-as-needed -lpthread -std=c++0x with no luck.
The code:
#include <iostream>
#include <chrono>
#include <thread>
void foo()
{
std::cout << "Thread 1 created.." << std::endl;
}
int main()
{
std::thread t1(foo);
t1.join();
return 0;
}
Edit:
So unfortunately none of your suggestions worked. I decided to use Boost instead.
it's -Wl,--no-as-needed not -Wl,--no_as_needed, you use the hyphen
-pthread is a flag for the compiler, not the linker, the right one for the linker is -lpthread
Mingw doesn't always comes with the same threading library, there are more than 1 options for multithreading with MinGW, you should document yourself about this according to your MinGW build
g++ filename.c -std=c++11 -lpthread
i am compiling your code with above command its working perfect.