Discussion:
pthreads in a static library
(too old to reply)
J***@gmail.com
2005-08-31 11:45:15 UTC
Permalink
Hello,
I usually use pthreads in a dll, but now I want to use them in a static
library.
I want my program to work on any win NT pc. I'm compiling using Intel
C++ 9 compiler. I use win32-pthreads. I had no problems creating the
static lib (just had to define PTW32_BUILD).
Now when I'm trying to use them in a program I have for every function
a link error (I did included the header files, and it doesn't has
anything to do with the calling conventions...).
Here are some examples:
°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°
teststatic.obj : error LNK2001: unresolved external symbol
__imp__pthread_join
teststatic.obj : error LNK2001: unresolved external symbol
__imp__pthread_mutex_init
teststatic.obj : error LNK2001: unresolved external symbol
__imp__pthread_mutex_destroy
teststatic.obj : error LNK2001: unresolved external symbol
__imp__sem_init
°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°
What can I be doing wrong? Thanks for any suggestions.

Jim
Yaytay
2005-08-31 16:19:33 UTC
Permalink
Post by J***@gmail.com
Now when I'm trying to use them in a program I have for every function
a link error (I did included the header files, and it doesn't has
anything to do with the calling conventions...).
Jim,

This isn't a threading problem, it's just a question of knowing what the
static lib is doing.

You don't make it clear whether you are trying to build a lib that contains
pthread or one that intends to make use of pthread from a DLL.
It appears that, whichever you want, you've asked for the second of these.

When you build a static lib it just contains all of the object files that
you've just compiled, unless you take extra steps it doesn't contain much
else (a bit of housekeeping).
What it most explictly doesn't contain is anything that is referenced by
your object files.

So your object files still reference pthread functions that come from the
pthread DLL.
You have two choices, you can either reference the import lib for pthreads
when you link the final program or you can explicitly add the import lib for
pthreads to the list of things that lib will include.

If, on the other hand, you are trying to produce a static lib that just
contains pthreads then I think you have more work ahead of you as pthreads
doesn't appear to be designed to work that way.
A number of the pthread headers contain preprocessor instructions that make
all of the pthread functions either DLL import (for using pthreads in a DLL)
or DLL export (for making them available from a DLL) - neither of these is
correct for making them available from a static lib.

J.T.
J***@gmail.com
2005-08-31 16:53:32 UTC
Permalink
Hello, and thanks for your replie.
Post by J***@gmail.com
usually use pthreads in a dll, but now I want to use them in a static
library.
What I wated to say was:
I usally link with the pthreads-dll in my programs which make use of
pthreads.
Now I've created (just compiled as a new project: "static lib") a
static version of pthreads.
I wanted to use this static version to link my program with.
( so: now I add: #pragma comment(lib,"mystaticversion.lib") in stead of
#pragma comment(lib,"thedynamicversion_which_was_pre-builded.lib") )
When I do that however, I have the errors I described.
I think you misunderstood my question, did you? (I know it's my fault,
sorry for that).

Sorry for my poor english, but as you see, I'm not english...
Yaytay
2005-09-01 09:30:42 UTC
Permalink
Post by J***@gmail.com
Hello, and thanks for your replie.
Post by J***@gmail.com
usually use pthreads in a dll, but now I want to use them in a static
library.
I usally link with the pthreads-dll in my programs which make use of
pthreads.
Now I've created (just compiled as a new project: "static lib") a
static version of pthreads.
I wanted to use this static version to link my program with.
( so: now I add: #pragma comment(lib,"mystaticversion.lib") in stead of
#pragma comment(lib,"thedynamicversion_which_was_pre-builded.lib") )
When I do that however, I have the errors I described.
I think you misunderstood my question, did you? (I know it's my fault,
sorry for that).
Sorry for my poor english, but as you see, I'm not english...
I did misunderstand at first, then realised that that might be what you are
trying to do and covered that possibility in my last paragraph.
Basically pthreads is not designed to be used that way, so you'll have to
make a number of changes to the source (and I don't know what).
The primary issue is that the source declares functions as either dllimport
or dllexport - neither of which is correct for a static library.
Minimally you will need to identify those functions that are declared as
dllexport and change them - this might be as simple as changing the header
files that use PTW32_BUILD (probably requiring you to set up another macro
to indicate what you are trying to do and override the PTW32_BUILD
control) - but then again it might not.

Sorry I can't be more help, your answer is still not relevant to threading,
but it does require a knowledge of the internals of pthreads.

J.T.
J***@gmail.com
2005-09-01 10:08:33 UTC
Permalink
OK. Thanks a lot.
I'll try and see if it works;
if it doesn't it's not THAT bad either...

Thanks a lot for your explanation.

Jim
Post by Yaytay
Post by J***@gmail.com
Hello, and thanks for your replie.
Post by J***@gmail.com
usually use pthreads in a dll, but now I want to use them in a static
library.
I usally link with the pthreads-dll in my programs which make use of
pthreads.
Now I've created (just compiled as a new project: "static lib") a
static version of pthreads.
I wanted to use this static version to link my program with.
( so: now I add: #pragma comment(lib,"mystaticversion.lib") in stead of
#pragma comment(lib,"thedynamicversion_which_was_pre-builded.lib") )
When I do that however, I have the errors I described.
I think you misunderstood my question, did you? (I know it's my fault,
sorry for that).
Sorry for my poor english, but as you see, I'm not english...
I did misunderstand at first, then realised that that might be what you are
trying to do and covered that possibility in my last paragraph.
Basically pthreads is not designed to be used that way, so you'll have to
make a number of changes to the source (and I don't know what).
The primary issue is that the source declares functions as either dllimport
or dllexport - neither of which is correct for a static library.
Minimally you will need to identify those functions that are declared as
dllexport and change them - this might be as simple as changing the header
files that use PTW32_BUILD (probably requiring you to set up another macro
to indicate what you are trying to do and override the PTW32_BUILD
control) - but then again it might not.
Sorry I can't be more help, your answer is still not relevant to threading,
but it does require a knowledge of the internals of pthreads.
J.T.
Loading...