6

I am new to C programming.

I was trying to use the pthread API to write some concurrent program.

I downloaded eclipse IDE for C/C++ Developers, MinGW. I have put all the library, header files into the corresponding location of the MinGW file.

When I tried to build the project, there is always an error "cannot find -lpthread", what happened? I have added the "-pthread" to the GCC compiler.

I have searched a lot in Google but seems no one have similar problem as me.

alk
  • 69,737
  • 10
  • 105
  • 255
neurothew
  • 185
  • 1
  • 2
  • 11
  • 2
    Possible duplicate of [Eclipse MinGW C++ cannot find -lpthread](http://stackoverflow.com/questions/39185248/eclipse-mingw-c-cannot-find-lpthread) – Mike Kinghan Aug 31 '16 at 07:06

4 Answers4

10

The answer to this question by someone who is also missing MinGW pthread library should help you out! Essentially the issue is that the MinGW installer script might not download the lpthread library upon installation. Quoted from link:

Just run and open MinGW Installation Manager, which should be pre-installed with MinGW, select "All Packages" on the left panel, and on the right panel, search for "mingw32-pthreads-w32" packages and install them.

Community
  • 1
  • 1
APaul
  • 376
  • 4
  • 15
  • 1
    I believe this is the right answer. But I used the cygwin library. See my answer. – minghua Nov 25 '16 at 01:03
  • Just a note: this worked for me too, but only after installing the "mingw32-pthreads-w32-dev" package specifically. I think I know why now, but it wasn't totally obvious. – qsfzy Dec 28 '20 at 17:19
3

I downloaded eclipse IDE for C/C++ Developers, MinGW.

MingGW uses the Windows API. The Windows API does not provide PThreads.

You need to install PThreads for Win32 to have PThreads available under Windows, and with this available under MinGW.

alk
  • 69,737
  • 10
  • 105
  • 255
  • I have downloaded everything from the website and put the .lib .a files in to C:\MinGW\lib, header files in C:\MinGW\include and the dll files in C:\Windows. But the problem still happened, I don't know if I am missing anything important. – neurothew Feb 17 '14 at 12:25
  • @user1906523: You might like to quote the **full output** you see on Eclipse's console when building. – alk Feb 17 '14 at 12:53
  • There is just one error - "cannot find -lpthread", nothing else. – neurothew Feb 17 '14 at 15:37
  • @user1906523: The pthread libs under Windows carry a suffix to their name. Check out what the suffix is and adjust Eclipse's library configuration accordingly. If for example the library is called `libpthreadGC2.dll` change `pthread` to `pthreadGC2` in Eclipse. – alk Feb 17 '14 at 15:41
  • I have tried to change the -pthread to -pthreadVC2. The project can be built but there is no exe coming out. The Console is like this: Info: Internal Builder is used for build gcc -O0 -g3 -Wall -pthreadvc2 -c -fmessage-length=0 -o Testing.o "..\\Testing.c" gcc: error: unrecognized command line option '-pthreadvc2' I have put the library files in the correct location, but it still say that there is unrecognized command line option. – neurothew Feb 17 '14 at 16:21
  • @user1906523: Ok, this is more complicated, as there are **two** options. 1st to compile sources using PThreads. For gcc that is `-pthread` and shall not be changed. 2nd there is the option `-l` to link the objects resulting from compilation to the pthread-library. For gcc the latter should be `-lpthreadGC2` or alike, `-lpthreadVC` is definitly wrong for gcc as the suffix `VC` indicates it is for use with the Visual C compiler/linker-suite. Btw: if the suffix is capitialised, apply it capitalised. – alk Feb 17 '14 at 16:32
1

Eclipse is not configured to put the -pthread argument in the gcc compilation. To solve this, go to the Menu: view sourceprint? 1.Project -> Properties

From the bar on the left: view sourceprint? 1.c/c++ build -> GCC C Compiler -> Miscellaneous

Add the “-pthread” argument into the beginning of the “Other Flags” Also go to: view sourceprint? 1.c/c++ build -> Settings -> GCC C Linker -> Libraries

And include the “pthread”library into the other libraries. Click Apply and rebuild the project. Pthreads must work now.

vinod
  • 132
  • 5
  • Please check this link also :http://stackoverflow.com/questions/14770147/eclipse-juno-gcc-compiler-pthread – vinod Feb 17 '14 at 11:14
  • I have done exactly the same thing. The only difference is that I did not find any GCC C Linker but I only found MinGW C Linker. – neurothew Feb 17 '14 at 12:21
1

See the question on mingw.org. I ended up with adding 'C:/cygwin/lib' to the settings for the "Library search path (-L)" at properties >> c/c++ build >> settings >> MinGW C Linker >> Libraries.

minghua
  • 5,981
  • 6
  • 45
  • 71