I have a problem with pthread.h and multithreading with c++ and Visual Studio 2013.
Here is my code
#include <iostream>
#include <cstdlib>
#include <pthread.h>
using namespace std;
#define NUM_THREADS 1
void *PrintHello(void *)
{
cout << "Hello World! Thread ID, ";
return NULL;
}
int main()
{
pthread_t threads[NUM_THREADS];
int rc;
rc = pthread_create(&threads[1], NULL,PrintHello, NULL);
if (rc){
cout << "Error:unable to create thread," << rc << endl;
exit(-1);
}
}
When I try to run this
Error 1 error LNK2019: unresolved external symbol __imp__pthread_create referenced in function _main
Error 2 error LNK1120: 1 unresolved externals
I get these errors. But I could not find a way to solve my problem. I need your help.
I used the link provided by Oakdale.
http://web.cs.du.edu/~sturtevant/pthread.html
Minor changes for Visual Studios 2013 Ultimate on a Windows 7 x64.
Here is the source I used: http://sourceforge.net/projects/pthreads4w/?source=typ_redirect
This contains a pre-compiled version of pthreads. Use the x86 directories (i.e. /bin, /lib, /include to pull the .dll, libs, and .h.
There are additional .dll, libs and .h that should be added to the VS2013 directories.
To include pthreads into your VS2013 project:
right-click on your project > Properties > Configuration Properties > C/C++ > Additional Include Directories > Add "-lpthread"
from the same "Project" Properties Pages > Configuration Properties > Linker > Input > Add
pthreadVC2.lib
pthreadVCE2.lib
pthreadVSE2.lib
I was able to compile your example above and execute with debug. I got this on the first shot so I may have missed something for other environments. Please post any continued issues.
Related
I've been attempting to include & use the pthreads library in my C++ project.
I'm using Visual Studio 2022 Community Edition with C++ 14, and I'm building my project in a x64 configuration.
I installed the pthread library through the NuGet Package Manager, I have v2.9.1.4 of the pthreads package.
I was following this example for basic usage of the pthreads library, and I set up my thread-creating code as follows inside my main function:
pthread_t threads[ThreadNum];
ThreadData td[ThreadNum];
int rc;
int i;
for (i = 0; i < ThreadNum; i++)
{
cout << "Creating Thread " << i << "...\n";
td[i].threadID = i;
td[i].message = "Message";
rc = pthread_create(&threads[i], NULL, TEST_FUNCTION, (void*)(&td[i]));
if (rc)
{
cout << "Error: Unable to create Thread " << rc << "!\n";
exit(-1);
}
}
pthread_exit(NULL);
There are no immediate errors thrown at me by VS, however upon a compilation attempt, the following errors are produced:
LNK2019 unresolved external symbol __imp_pthread_create referenced in function main
LNK2019 unresolved external symbol __imp_pthread_exit referenced in function main
LNK2001 unresolved external symbol __imp_pthread_exit
Above my main function definition, I made sure to include both pthread.h and cstdlib, so I'm quite a bit confused as to why my code here isn't compiling.
Where did I mess up here?
Thanks for reading my post, any guidance is appreciated!
The example you are using is bad and incomplete. Try using std::thread instead of pthread, it will work and is easier to use.
Using pthread is complicated and not recommended. If you really want to keep using it, here are some starting points:
Install third party POSIX Threads for Windows
Using pthread in c++ on Visual Studio 2013
In a nutshell
I am tring to use an existing C++ library.
The problem is that it's not compiled.
I took the src code (a visual studio project) and tried to compile it.
The thing is that I can't link my project to the library.
More specific details are:
What I have already done is the following:
I opened the .sln file and added another sub-project called "Sampler"
I downloaded all Microsoft additional packages that the library src code depends on.
I compiled the library to Windows 32 platform x86 (using Visual Studio 2022). The compilation was successful.
I created a directory Dependencies\OPCClientToolKit in same directory of .sln file, Dependencies\OPCClientToolKit contains two directories include and lib.
I put in Dependencies\OPCClientToolKit\lib the .lib - the output after compilation.
I put in Dependencies\OPCClientToolKit\include the .h files of the src code of sdk library.
I added a dependency .h files via Sampler Project > Properies > C++ General. See pictures below.
I added a dependency folder Dependencies\OPCClientToolKit\lib via Sampler Project > Properies > Linker General. See pictures below.
I added a dependency OPCClientToolKit.lib via Sampler Project > Properies > Linker Input. See pictures below.
As far as I know all my step seems valid and fine.
But when, I am compiling (hitting Build via Visual Studio) the Sampler project code (see the code below) I am getting an error message:
Error Message:
1>------ Build started: Project: Sampler, Configuration: Release Win32 ------
1>Sampler.cpp
1>Sampler.obj : error LNK2001: unresolved external symbol "public: static class COPCHost * __cdecl COPCClient::makeHost(class ATL::CStringT<wchar_t,class ATL::StrTraitATL<wchar_t,class ATL::ChTraitsCRT<wchar_t> > > const &)" (?makeHost#COPCClient##SAPAVCOPCHost##ABV?$CStringT#_WV?$StrTraitATL#_WV?$ChTraitsCRT#_W#ATL###ATL###ATL###Z)
1>C:\Users\Mark\Desktop\Projects\PID\OPC_DA\Release\Sampler.exe : fatal error LNK1120: 1 unresolved externals
1>Done building project "Sampler.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Please explain to me how should I resolve this issue. I just want to include and use this library and not to modify it.
Dependecies\OPCClientToolKit\lib
Dependecies\OPCClientToolKit\include
Sampler Project > Properties > C++ > General
Sampler Project > Properties > Linker > General
Sampler Project > Properties > Linker > Input
I am using this library (OPC client sdk):
https://sourceforge.net/projects/opcclient/
A usage example made by other guy on the internet:
https://github.com/Tibalt/OPC_DA/blob/master/OPCClientDemo/OPCClientDemo.cpp
My code just use functions and objects of that library.
In details, just trying to initialize opc connection to a server on localhost:
#include <iostream>
#include <vector>
#include <string>
#include <stdio.h>
#include <sys\timeb.h>
#include "opcda.h"
#include "OPCClient.h"
#include "OPCHost.h"
#include "OPCServer.h"
#include "OPCGroup.h"
#include "OPCItem.h"
using namespace std;
int main(void) {
COPCClient::init();
cout << "Done Init";
cout << endl;
CString hostName = "localhost";
COPCHost* host = COPCClient::makeHost(hostName);
}
That project OPCClientToolKit is very old. It looks like built with using ANSI character set, without using the macro _UNICODE. You should either
Remove the macro _UNICODE from the preprocessor settings in your project.
Or add that entire project OPCClientToolKit as a dependency to your visual studio solution and add the macro _UNICODE to that project.
Or use the solution of that project OPCClientToolKit and rebuild the static library OPCClientToolKit.lib with the macro _UNICODE.
I tried to create empty project for Visual Studio.
// OpenGL1.cpp : main project file.
// #include "stdafx.h"
#include "windows.h"
#include <GL/gl.h>
#include <iostream>
using namespace System;
int main()
{
std::cout << "Hello World" << "\n";
return 0;
}
Having configured these dependencies:
opengl32.lib;glu32.lib;olepro32.lib;%(AdditionalDependencies);C:\Program Files\Microsoft SDKs\Windows\v7.0A\Include\gl;
And I got this error:
.NETFramework,Version=v4.0.AssemblyAttributes.cpp
LINK : fatal error LNK1104: cannot open file 'C:\Program Files\Microsoft SDKs\Windows\v7.0A\Include\gl.obj'
Can you explain why this happens and how to remove the error?
The "dependencies" refer to the filenames of libraries which the linker should use, not to include paths. Include paths are for the compiler, not for the linker.
You must first tell the linker where to find the OpenGL library file:
http://msdn.microsoft.com/en-us/library/1xhzskbe%28v=vs.100%29.aspx
And then, as a dependency, you specify only its name, not a full path. In this case, opengl32.lib.
I realise this i mighty popular question, but all of the other posts seem to follow the same idea. That the include, lib and bin folder need to be correctly configured. I believe my settings are correct, but i am still getting the LNK2019 error. What am i doing wrong?
Using Visual Studio 2012
I am getting the infamous LNK2019 error. Many posts of this issue seem to think it is a linker issue. I am using a library with lib and dll files. The project folder has an include, bin and lib folder that need to be configured in the project.
Install instructions are here.....
http://opensource.mlba-team.de/xdispatch/docs/current/tutorial.html
However, this is clearly a linker issue as it occurs in other libraries of a similar type. I have followed the instructions for other posts and i'm still a bit lost. I believe this should be correct, but i have tried virtually every possible combination. Don't get it.
My Project Configurations
C++ -> General -> Additional Include Directories.
C:\Users\Daniel\Documents\Visual Studio 2012\Projects\LibDispatchTest\xdispatch_0.7.2_Visual Studio 10_i386\include;%(AdditionalIncludeDirectories)
Linker -> General -> Additional Library Directories
C:\Users\Daniel\Documents\Visual Studio 2012\Projects\LibDispatchTest\xdispatch_0.7.2_Visual Studio 10_i386\lib;%(AdditionalLibraryDirectories)
Linker -> Input -> Additional Dependancies:
.....uuid.lib;odbc32.lib;odbccp32.lib;xdispatch.lib;%(AdditionalDependencies)
Environment Variables.
I have the PATH variable set.
C:\Chocolatey\bin;C:\Users\Daniel\Documents\Visual Studio 2012\Projects\VisionBase\xdispatch_0.7.2_Visual Studio 10_i386\bin;
The error messages are:
error LNK2019: unresolved external symbol "_declspec(dllimport) public: void __thiscall xdispatch::queue::async(class std::function<void __cdecl(void)> const &)" (__imp?async#queue#xdispatch##QAEXABV?$function#$$A6AXXZ#std###Z) referenced in function "void __cdecl some_function(void)" (?some_function##YAXXZ) c:\Users\Daniel\documents\visual studio 2012\Projects\LibDispatchTest\LibDispatchTest\main.obj LibDispatchTest
error LNK1120: 1 unresolved externals c:\users\daniel\documents\visual studio 2012\Projects\LibDispatchTest\Debug\LibDispatchTest.exe 1 1 LibDispatchTest
Seriously, am totally lost and i do not see what i am doing wrong here.
EDIT 1
This is similar to a sample from the above link, but modified to just couNT 1000000^2 and print some stuff. We just want to be able to compile and run this sample and i can correct my larger project exhibiting this issue. This project was made fresh with the simplest code that is representative of what i need to get working. Both this sample and my other project have this problem and produce the same error.
#include <xdispatch/dispatch>
#include <vector>
#include <cmath>
class SomeData {
public:
std::vector<double> a;
std::vector<double> b;
std::vector<double> c;
std::vector<double> results;
};
void do_calculations(SomeData* sd){
// our output will go in here
sd->results = std::vector<double>(sd->a.size());
// the calculation - running on one thread only
for(unsigned int i = 0; i < 1000000; i++){
sd->results[i] = 0;
for(unsigned int j = 0; j < 10000000; j++){
for(unsigned int z = 0; z < sd->c.size(); z++){
std::cout << i << " " << j << std::endl;
}
}
} }
/* This function is getting called from your main thread also powering the user interface */
void some_function() {
SomeData* sd = new SomeData();
xdispatch::global_queue().async(${
// execute the heavy code
do_calculations(sd);
}); }
int main() {
some_function();
return 0; }
Is anyone able to help?
The binaries you used where built with and for MS Visual Studio 2010. Since then the CRT has changed quite a bit and might not be compatible, especially when parts of the STL is used. Because of that I do not recommend to ever use a C++ dll built with VS 2010 for use in VS 2012 or later. It could be that building and linking succeeds but during runtime you experience weird and unexpected issues. In your case even linking won’t work because std::function has changed its signature in the meantime, so lucky enough you already discover those incompatibilities at compile time.
I assume xdispatch will work just fine with VS2012 and maybe even VS2013, however you will need to do your own build for that. All information needed is summarized at [1]. A tarball containing the sources can always be found at [2].
[1] http://opensource.mlba-team.de/xdispatch/docs/current/requirements.html
[2] http://opensource.mlba-team.de/xdispatch/files/
I start using OpenCV 2.3 in VS 2010 so there are a lot of things I don't know.
I installed OpenCV by following the guide here: http://siddhantahuja.wordpress.com/2011/07/18/getting-started-with-opencv-2-3-in-microsoft-visual-studio-2010-in-windows-7-64-bit/
but I couldn't chose "Create new project platforms" in step 4 to create a project.
And when I inserted the code, I had many problems.
1>opencv_highgui230d.lib(opencv_highgui230d.dll) : fatal error LNK1112: module machine type 'x64' conflicts with target machine type 'X86'
If I erase waitKey(), I'll have that error:
1>C:\Users\Gia Linh\Documents\Visual Studio 2010\Projects\Testopencv\Debug\Testopencv.exe : fatal error LNK1120: 6 unresolved externals
I don't know how to fix it. Please, help me. Thank you a lot!
#include "stdafx.h"
#include<stdio.h>
#include "cv.h"
#include "highgui.h"
using namespace cv;
int _tmain(int argc, _TCHAR* argv[])
{
char* imagename = "lena.jpg";
Mat img = imread(imagename);
if(img.empty()){
fprintf(stderr,"can't load image %s\n",imagename);
return -1;
}
if(!img.data)
return -1;
namedWindow("show an image",CV_WINDOW_AUTOSIZE);
imshow("show an img",img);
waitKey();
return 0;
}
It seems that you linked to x64 dll's and lib's of opencv and trying to run it under x86 configuration of Visual Studio!
Try adding another path to the environment variables to "opencvDIR/build/x86/vc10/bin" and check all the paths in:
(Im assuming you are trying to run an x86 project)
Project properties > Configuration Properties >Linker > General> Additional Library Directories:
should be something like: opencvDIR\build\x86\vc10\lib.
and
Project properties > Configuration Properties >Linker > Input> Additional Dependencies:
should contain the appropriate lib file name, if you are in debug mode they should be with a "d" at the end of the file name(stands for debug) and without it if you are in release mode.
I would allso suggest creating a property sheet for opencv rather then linking in the project settings!