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
Related
I'm very new to using visual studio, and c++.
I was trying to build a game to test my OOP understandings using new language, then I realized I might need to start using scripting language with in my C++ game. I found LUA to be a good candidate for the scripting part of the game, so I decided to follow a tutorial I've found at :
http://www.gamedev.net/page/resources/_/technical/game-programming/the-lua-tutorial-r2999
#include "stdafx.h"
#include <lua.hpp>
#include<iostream>
int main()
{
char *Lua =
"x = 8 "
"return ( x > 7 ) ";
lua_State *luaState;
luaState = luaL_newstate();
int iStatus = luaL_loadstring(luaState, szLua);
if (iStatus)
{
std::cout << "Error: " << lua_tostring(luaState, -1);
return 1;
}
return 0;
}
However VS 2015 debugger is giving
unresolved external symbol _luaL_newstate
unresolved external symbol _luaL_loadstring
unresolved external symbol _lua_tolstring
I'm currently using Lua 5.1.5, and followed the tutorial setting tutorial section step by step, where it tells me to add lua folders to project settings.
Can someone tell me what i'm doing wrong?
The linker errors indicate that you are missing Lua functions from the executable you want to build. It's possible that you missed this step in the tutorial you've been following: add the Lua source files to your project's "Source Files". There is a list of the files in C:\dev\lua-5.1.5\etc\all.c; you want all of those files except for lua.c.
In general, you need to add the Lua library, lua DLL, or Lua files (in this case it's not enough to specify path to them), so that the references for the functions you are using in your code are properly resolved (statically or dynamically).
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 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.
I have recently updated to Visual Studio 2013, and to avoid the known problems with uninstalling one of the two programs (I previously used 2010) while the other is installed I uninstalled 2010 before I installed 2013.
Despite what code I place in it, even simple code that is only a few lines, Everything gets this LNK2019 error.
#include<iostream>
using namespace std;
int main()
{
cout << "Testing" << endl;
system("pause");
return 0;
}
Before anyone comments with the "Do not use system("pause")" I know this and I did it purely for simplicity reasons to see if the code would even compile as it would not with any of my longer code either.
Here is a copy of the Error message.
Error 1 error LNK2019: unresolved external symbol WinMain#16 referenced in >function __tmainCRTStartup C:\Users\z49203\Documents\Visual Studio >2013\Projects\JCCNEW\JCCNEW\MSVCRTD.lib(crtexew.obj) JCCNEW
As for extensions in Visual Studio, I have the English version of it with the Japanese language pack. As I need to be able to code using both character sets.
Make your project a Console application not a Window Form one. Otherwise the program needs a WinMain as entry point instead of main.
I just installed Boost on my machine. I'm working with the Visual Studio 2010 Ultimate. To install Boost I followed the instruction here: http://www.boost.org/doc/libs/1_48_0/more/getting_started/windows.html. In particular this line: "The installers supplied by BoostPro Computing will download and install pre-compiled binaries into the lib\ subdirectory of the boost root". So I found I have boost_1_47 now running on my machine. And I started a little test program, to play with the boost::thread library. However this code which is the FIRST example code on the introduction to boost::thread won't compile:
#include <boost/thread.hpp>
boost::thread make_thread();
void f()
{
boost::thread some_thread = make_thread();
some_thread.join();
}
int main()
{
f();
return 0;
}
This is the error message:
error LNK2019: unresolved external symbol "class boost::thread __cdecl make_thread(void)" (?make_thread##YA?AVthread#boost##XZ) referenced in function "void __cdecl f(void)" (?f##YAXXZ)
However this code compiles:
#include <boost/thread.hpp>
void testFunction()
{
}
int main()
{
boost::thread_group group;
group.create_thread(&testFunction);
group.join_all();
return 0;
}
The above code I copy/pasted from some forum entry. But what is the reason for all this? Is make_thread() not supported by version 47? If so, why does only the linker complain then? What am I missing?
EDIT:
My apologies for having asked this question, I find it hard to admit, but this belongs to the category RTFM. But however stumbles about this: read the answers below.
After a quick search on Google, and reading the documentation about thread management, it seems to me that the function make_thread is just a dummy function used in an example to show that threads can be moved between different thread objects.
If you want a specific function that creates a thread, you have to make it yourself.
There is no make_thread function defined in boost. I think you misunderstood the example. This line:
boost::thread make_thread();
is just a prototype of a "custom" make_thread function, but without an implementation. That's why the linker fails to find it.