MinGW 4.8.1 C++11 thread support - gcc

I downloaded the version of MinGW from the official website: http://sourceforge.net/projects/mingw/files/ and installed it on my Windows 7 machine.
Running g++ --version gives me g++.exe (GCC) 4.8.1 and I believe GCC 4.8.1 has support for C++11 features, including threads.
Running g++ -std=c++11 main.cpp successfully compiles the following program.
//main.cpp
#include <memory>
int main() {
std::unique_ptr<int> a(new int);
return 0;
}
But running g++ -std=c++11 main.cpp on the following program:
//main.cpp
#include <mutex>
int main() {
std::mutex myMutex;
return 0;
}
gives errors:
main.cpp: In function `int main()`:
main.cpp:5:5: error: 'mutex' is not a member of 'std'
std::mutex myMutex;
^
main.cpp:5:16: error: expected ';' before 'myMutex'
std::mutex myMutex;
^
as if <mutex> is not supported. The compiler does not complain about #include <mutex> so I have no idea why I'm getting this error.

If I understand well, std threading is still not supported on mingw, but some mingw-w64 builds support it. Fortunately, you can still build 32-bit apps using this version of mingw.
Here is the link for the builds.

There is already a native win32 implementation of std::thread and sync primitives:
https://github.com/meganz/mingw-std-threads
It is a header-only library and should work with any C++11 compliant version of MinGW.

Related

Link OpenBLAS to MinGW

I'm trying to link OpenBLAS library with MinGW w64 compiler on Windows.
This is my code:
#include <cstdio>
#include <cblas.h>
#include <cstdlib>
int main(){
double m[10],n[10];
int i, result;
for(i=0;i<10;i++)
m[i] = 1.0l*rand()/RAND_MAX;
for(i=0;i<10;i++)
n[i] = 1.0l*rand()/RAND_MAX;
result = cblas_ddot(10, m, 1, n, 1);
return 0;
}
and compiling with this command:
g++ ^ -IC:\OpenBLAS-0.3.6-x64\include -LC:\OpenBLAS-0.3.6-x64\lib -lopenblas blas.cpp
and get an error
undefined reference to `cblas_ddot'
I downloaded precompiled binaries from here and using 64bit Windows, g++ (x86_64-win32-seh-rev0, Built by MinGW-W64 project) 8.1.0
How can I fix this error?
A general suggestion is to always put source and object files before linked libraries.
In general not all of the library functions are used, but only the ones needed by the main source of code. Then the linker needs to know the undefined symbols before looking into the libraries.
Then putting blas.cpp before -lopenblas should work.
g++ ^ -IC:\OpenBLAS-0.3.6-x64\include -LC:\OpenBLAS-0.3.6-x64\lib blas.cpp -lopenblas

arm-linux-gnueabi-g++-4.7 cross-compiling c++11

I'm trying to compile an example using std::future with arm-linux-nueabi-g++-4.7 compiler; however, I have the following errors:
user#user-virtual-machine:~/projects/prova$ arm-linux-gnueabi-g++-4.7 -pthread -std=c++11 -c main.cpp
main.cpp: In function ‘int main()’:
main.cpp:8:35: error: variable ‘std::packaged_task task’ has initializer but incomplete type
Can someone please tell me what I did wrong? I installed the compiler as distribution package.
code:
#include <iostream>
#include <future>
#include <thread>
int main()
{
// future from a packaged_task
std::packaged_task<int()> task([](){ return 7; }); // wrap the function
}
I could reproduce the same error on an ubuntu 16.04. Compiling with the following target architecture -march=armv7-a worked. These other architectures armv7-r armv6zk armv6z armv6t2 armv6k also worked (I did not try them all).
It seems for some architectures this code does not compile. I hope your board is suported! :)

How do you compile C++ programs that include LLVM API headers?

I'm trying to use the C++ compiler to compile the following program:
#include <stdio.h>
#include "llvm/IR/LLVMContext.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/IR/Module.h"
int main( int argc, char* argv[] )
{
if( argc < 2 )
llvm::errs() << "Expected an argument - IR file name\n";
llvm::LLVMContext &context = llvm::getGlobalContext();
llvm::SMDiagnostic err;
llvm::Module* module = llvm::ParseIRFile( argv[1], err, context );
if( !mod )
{
err.print( argv[0], errs() );
return 1;
}
return 0;
}
I'm trying to compile the program using the following command:
clang++ main.cpp -o main
However, when I compile, I'm getting the following compile error:
main.cpp:2:10: fatal error: 'llvm/IR/LLVMContext.h' file not found
#include "llvm/IR/LLVMContext.h"
^
1 error generated.
In this case, I'm not exactly sure how to link the LLVM API headers when compiling main.cpp with Clang.
Any help would be greatly appreciated.
You can use the following command:
g++ -std=c++11 main.cpp `llvm-config --system-libs --cppflags --ldflags --libs core` -o main
Where --libs and --system-libs flags are used for linking and --cppflags takes care of include paths.
Thank You
You need LLVM checked out or installed somewhere on your system. You can download a binary release (with headers and libraries you can build against) as explained here: http://llvm.org/releases/download.html#3.5
You can also check out LLVM from its SVN repository as explained here: http://llvm.org/docs/GettingStarted.html#checkout
Once you do that, I recommend looking at the llvm-clang-samples repository that comes with a Makefiles showing how to build sample programs vs. an up-to-date LLVM.

clang support of aggregation initialization on Mac OS X

I'm trying to compile C++11 list congregation initialization on clang++ on mac.
#include <iostream>
#include <list>
#include <string>
using namespace std;
int main(int argc, char *argv[]) {
list<string> aList = {"abc", "def", "xyz"};
}
This is the command for the compilation.
clang++-mp-3.1 -std=c++11 iterator.cpp
I got no matching constructor error.
iterator.cpp:7:23: error: no matching constructor for initialization of
'std::list<string>'
std::list<string> aList = {"abc", "def", "xyz"};
^ ~~~~~~~~~~~~~~~~~~~~~
I tried with XCode
clang -v
Apple clang version 4.1 (tags/Apple/clang-421.11.66) (based on LLVM 3.1svn)
Target: x86_64-apple-darwin11.4.2
Thread model: posix
I also tried with clang++ from port
clang++-mp-3.1 -v
clang version 3.1 (branches/release_31)
Target: x86_64-apple-darwin11.4.2
Thread model: posix
I got the same result. What might be wrong?
clang's support of C++ 11 lambda
I tried icc, got the same error. I guess the issue is with the template not the compiler. icc uses existing template /usr/include/c++/4.2.1 and the implementation doesn't support c++11 fully.
I tried gcc 4.8 from port
sudo port install gcc48
It works fine
/opt/local/bin/g++-mp-4.8 -std=c++11 Iterator.cpp
How to turn on C++0x of Intel C++ Compiler 12.1.2

Why does clang++ lack forward list?

I wrote up a simple C++ program that relies on forward_list like
#include <forward_list>
#include <iostream>
int main() {
std::forward_list<int> my_list;
my_list.push_front(3);
std::cout << my_list.top() << std::endl;
return 0;
}
However, when I compile this program on my Mac with clang++ my_program.cpp -std=c++11 -o my_program, I get this error:
my_program.cpp:1:14: fatal error: 'forward_list' file not found
#include <forward_list>
^
1 error generated.
Why can't clang find forward_list? Other C++11 features are working. For instance, the auto keyword works, albeit a warning appears that tells me that auto is a C++11 feature.
By default clang++ uses an older gcc-4.2 std library which has no C++11 support. You can tell clang to use a C++11-aware std::lib with the command -stdlib=libc++. libc++ has <forward_list>.

Resources