Cannot compile with openMP

This is my little program (omp.cpp):

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <omp.h>

int main() {
  std::cout << "Before" << std::endl;
  #pragma omp parallel
  {
    std::cout << "Hello ";
    std::cout << "World!" << std::endl;
  }
  std::cout << "End" << std::endl;
}


I've tried to compile this with the command g++ omp.cpp -fopenmp (or g++ -fopenmp omp.cpp) but I always get the error message:
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: cannot find -lpthread
collect2.exe: error: ld returned 1 exit status



What I've tried to do:

I've asked this question on StackOverfow and I've been told that I'm missing pthread. I've downloaded phtread from here: https://sourceware.org/pthreads-win32/
I've added pthread.h, shed.h and semaphore.h to MinGW/include.
libpthreadGC2.a and libpthreadGCE2.a are in MinGW/lib.
pthreadGC2.dll and pthreadGCE2.dll are in MinGW/bin

I've tried various other locations, but I just can't get openmp running. What do I have to do? I'm using Windows 7 if that matters. I'm a total newbie regarding such matters, so bear with me.

PS: I think I've got it working for Visual Studio, but I'd like to be able to work with Notepad++ and DOS
Last edited on
Some other page did recommend
mingw-get mingw32-pthreads-w32


The pthreads package that you did install may be for Windows, but if your compiler has a purpose-built package, then that is more likely to "fit in".
Thank you for your answer, but what exactly am I supposed to do? I've googled what you typed there and I've ended up downloading a file named pthreads-w32-2.9.1-1-mingw32-dll.tar.lzma but neither am I sure whether this is the right one, nor am I sure what to do with it.

I'd really need a more detailed explanation, because as I said, I'm a fullfledged newbie considering stuff like this. :(
LZMA is a compressed archive format, a bit like a zip or a rar file, so you'll need to unpack it. I think you can use 7zip for that.
The mingw-get is a package manager. According to net, it has both CLI and GUI modes. I hope it has some kind of manual/help too.

Looking further about it, the more proper CLI command might be "mingw-get install mingw32-pthreads-w32"


The idea of a package manager is that it somehow knows where to put the files (and thus can "uninstall" too), updates only installed packages, can at least tell whether some other packages have to be installed too as dependencies, and usually is able to download necessary packages from a network repository. Package management is miles better than trying to manually guess and remember where files do belong.


An another question is that I merely made guesses about "mingw32" and "mingw-get". You did not state how you have installed your GCC. For what I know, you might have installed it along some IDE (other than MSVS). Such IDE could have its own installer/package manager.
I've done that now. So that file had pthreadGC2.dll and pthreadGCE2.dll in it, both of which I already have in my MinGW/bin folder.
IIRC, tdm-gcc has an installer with bundled openMP support.
http://tdm-gcc.tdragon.net/download
@JLBorges

I've installed that one now. It has its own folder in C:\. Do I have to include it in some kind of PATH-variable? Becasue as of now I still can't compile my code with -fopenmp
I've just downloaded and tested it, and it works.

a. Installation: be sure to install the "openmp" optional package.
(It is not selected in the default install; you have to explicitly select it)

b. To place tdm-gcc in the path, from the command prompt, run the batch file
mingwvars.bat in the installation directory.

c. Check that everything is ok by entering the command: g++ --version
You should get:
g++ (tdm64-1) 4.9.2
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


d. You are ready to go; just compile with
g++ -std=c++14 -Wall -Wextra -pedantic-errors -fopenmp openmp_test.cpp
I've done (a). I cannot do (b) because I have no mingwvars.bat file [by installation directory I assume you mean the TDM-GCC-64/__install directory?]. However, the installation apparently does add it to PATH? Because it is there. I've added it to the user variables as well.

When running (c) I get:
g++ (GCC) 4.8.1
Copyright (c) 2013 ...


Do I have to update my MinGW or should that have happened with TDM?


I'm sorry for being such a bonehead.
> by installation directory I assume you mean the TDM-GCC-64/__install directory?

No. The root of the installation: if you have installed tdm gcc in C:\TDM-GCC-64 look for C:\TDM-GCC-64\mingwvars.bat


It appears that an older installation of GCC (version 4.8.1) is placed ahead in the PATH
(You should get g++ (tdm64-1) 4.9.2 if it is tdm gcc)
If you type PATH from the command line, what does it print oput?
Last edited on
Much much much much appreciated. Thank you so much for your patience. It works now.

So I had to delete MinGW/bin from my path, as it occured before TDM. Another mistake I made was that I didn't "refresh" my command-line. I thought it'd recognize every change made, but it didn't, I think. So after re-opening it (after having changed the PATH) it did work.

One last question. Should I add MinGW\bin to the end of the PATH, or is it unnecessary at this point?
It is unnecessary; if TDM-GCC-64\bin is in the path, it would have the things that you need.
Alright. Then it's time to practice some openMP for me. Again, thanks a lot!
Topic archived. No new replies allowed.