• Advertisement

Archived

This topic is now archived and is closed to further replies.

static/extern problem

This topic is 5345 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

mjpeg.h:

static int framecount = 0;
void Update();
--------------------

mjpeg.cpp:
#include "mjpeg.h"

Update()
{
	framecount++;
}
--------------------

main.cpp:
#include "mjpeg.h"

Problem()
{
	extern int framecount;
	cout << framecount << endl;
	if(framecount == 100)
		stop();
	else
		Update();
}
--------------------
  
We've tried different combinations of modifiers for framecount, but the couting always reports 0. What exactly are we missing about C++? (The actual compiler is embedded Visual C++) [edited by - meng_mao on July 2, 2003 12:57:18 PM]

Share this post


Link to post
Share on other sites
Advertisement
static means "This exists only in the current compilation unit."
extern means "Look for a variable named this in other compilation units."

Remove the static keyword.

Share this post


Link to post
Share on other sites
Yes, but I mentioned we tried all combinations.
Removing static will have the variable doubly linkaged,
even with #ifndef __MJPEG_H__, etc.

Share this post


Link to post
Share on other sites

mjpeg.h:

extern int framecount;
void Update();
--------------------

mjpeg.cpp:
#include "mjpeg.h"

int framecount;

Update()
{
framecount++;
}
--------------------

main.cpp:
#include "mjpeg.h"

Problem()
{
cout << framecount << endl;
if(framecount == 100)
stop();
else
Update();
}
--------------------


[edited by - foofightr on July 2, 2003 1:23:40 PM]

Share this post


Link to post
Share on other sites
Variable declarations should be in a cpp file, not a header file.

I stand by my original advice. No static, extern in other compilation units.

Extern is bad style anyway.

Share this post


Link to post
Share on other sites
quote:
Original post by antareus

Extern is bad style anyway.




extern _CRTIMP istream cin;
extern _CRTIMP ostream cout;
extern _CRTIMP ostream cerr, clog;

Share this post


Link to post
Share on other sites
Well obviously it has to be in the standard library. I can understand why.

But seriously who wants to track down externs across a massive project?

Share this post


Link to post
Share on other sites
externs are no different than any other programming tools at your disposal. Use them wisely, and they are beneficial. Use them not so wisely, and they will bite you in the ass.

Share this post


Link to post
Share on other sites

  • Advertisement