Re: Puzzled about the use of static var/function in C++
- From: Logan Shaw <lshaw-usenet@xxxxxxxxxxxxx>
- Date: Sat, 08 Sep 2007 05:04:48 -0500
GeertVc wrote:
The intention is to use that mutex to protect all my other functions
in the class CI2cHal, such that when a communication towards the I2C
driver is ongoing, no other thread can interrupt until the mutex is
unlocked again. This way, I'm safeguarding my driver against multi-
threading, right?
Since I want the mutex to be initialised only once and also that all
instances of the class CI2cHal "see" the same mutex, I added the
following line in the "main" function of my program (this is the first
statement executed, to be sure there are no threads created before):
void main( void )
{
CI2cHal::InitMutex();
.
.
while( 1 );
}
However, when compiling this code, I get the following error the
moment "main.cpp" gets compiled.
/home/geertvc/appdata/domotics/i2clibtest/src/main.o: In function
`CI2cHal::InitMutex()':
/home/geertvc/appdata/domotics/i2clibtest/src/main.cpp:45: undefined
reference to `CI2cHal::I2cMutex'
This I don't understand: InitMutex() is a static function, that uses
the static private variable I2cMutex.
It is, and it does. The problem is that while the class definition
exists and so defines the type of the static variable, making a class
definition doesn't actually declare the static members.
There is an analogous situation in straight C where if you do this
in a header file foo.h:
extern int globalVariable;
you are just creating a symbol table entry for the variable but not
actually declaring the variable's storage. You need to put this:
int globalVariable;
into the corresponding source file foo.c; that actually creates the
storage in one of the .o files and puts it somewhere where the linker
can see it.
A C++ static class variable works the same way: the class definition
in the header file defines the typename and member name that goes
with it, but it doesn't allocate storage. You need to manually do
that in the source file where you have the code for the class: just
like you define the bodies of the function in there, you need to
define the "body" of the variable there.
So basically, put this:
CMutex CI2cHal::I2cMutex;
into CI2cHal.cpp.
This is C++ FAQ #10.10, by the way. See
http://www.faqs.org/faqs/C++-faq/part4/ .
Having said all that, if you are already putting code into your
main() function to initialize this up front, why not simply make
the CMutex a regular instance variable and create a single object
on the stack (or the heap, whatever) in main()? If you only have
one CI2cHal object, you could simply create it in main() and make
the mutex an instance variable. Or if you have no CI2cHal objects
and all the methods are static, you could convert them into instance
methods and have more objects. Or if you have multiple CI2cHal
objects that all need to share the same mutex (not sure if this
would make sense, but I guess it could if the kernel driver shares
some global state between device instances), you could create one
object for that in main() and pass a pointer to it to all the
CI2cHal objects. Basically the point is, I think static members
are inherently ugly. :-)
- Logan
.
- Follow-Ups:
- Re: Puzzled about the use of static var/function in C++
- From: GeertVc
- Re: Puzzled about the use of static var/function in C++
- References:
- Puzzled about the use of static var/function in C++
- From: GeertVc
- Puzzled about the use of static var/function in C++
- Prev by Date: Puzzled about the use of static var/function in C++
- Next by Date: shared memory attachment
- Previous by thread: Puzzled about the use of static var/function in C++
- Next by thread: Re: Puzzled about the use of static var/function in C++
- Index(es):
Relevant Pages
|