473,320 Members | 1,802 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

static extern?

Problem:

A.cpp:
------
static FOO* gFoo=NULL;

A.h
extern FOO* gFoo;

gives: L2001 - unresolved external: "symbol struct FOO* gFoo"

if A.h looks like:
extern static FOO* gFoo;

it gives:
C2159: more than one storage class specified.

<blink, blink>
How would I do this now? I have a prefedined macro that makes a
"static FOO*". And I want to tell all my other .cpp files that it
exists in stdafx.h.

Thank you,
--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

________________________________________
Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com
Jul 22 '05 #1
10 28323
Try this:

A.h
static FOO* gFoo;

A.cpp:
extern static FOO* gFoo=NULL;

Vince

"Gernot Frisch" <Me@Privacy.net> wrote in message
news:2k*************@uni-berlin.de...
Problem:

A.cpp:
------
static FOO* gFoo=NULL;

A.h
extern FOO* gFoo;

gives: L2001 - unresolved external: "symbol struct FOO* gFoo"

if A.h looks like:
extern static FOO* gFoo;

it gives:
C2159: more than one storage class specified.

<blink, blink>
How would I do this now? I have a prefedined macro that makes a
"static FOO*". And I want to tell all my other .cpp files that it
exists in stdafx.h.

Thank you,
--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

________________________________________
Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com

Jul 22 '05 #2
Gernot Frisch wrote:
Problem:

A.cpp:
------
static FOO* gFoo=NULL;

A.h
extern FOO* gFoo;

gives: L2001 - unresolved external: "symbol struct FOO* gFoo"

if A.h looks like:
extern static FOO* gFoo;

it gives:
C2159: more than one storage class specified.
Yes, the variable gFoo cannot be static and extern at the same time.
<blink, blink>
How would I do this now? I have a prefedined macro that makes a
"static FOO*". And I want to tell all my other .cpp files that it
exists in stdafx.h.

Thank you,


Solution:

A.cpp:
------
FOO* gFoo=NULL; // Note no static

A.h
----
extern FOO* gFoo;
HTH

Mark
Jul 22 '05 #3
> Solution:

A.cpp:
------
FOO* gFoo=NULL; // Note no static

A.h
----
extern FOO* gFoo;


I rewrote the code so I can do this now. But I don't understand it.
Why can't I tell another .cpp file that there is an static variable
somewhere else?
-Gernot
Jul 22 '05 #4

"Gernot Frisch" <Me@Privacy.net> wrote in message
news:2k*************@uni-berlin.de...
Solution:


I rewrote the code so I can do this now. But I don't understand it.
Why can't I tell another .cpp file that there is an static variable
somewhere else?


Because static variables having namespace scope have internal linkage i.e.
the entity it denotes can be referred to by names from other scopes in the
same translation unit only.
Jul 22 '05 #5
Gernot Frisch wrote:
Problem:

A.cpp:
------
static FOO* gFoo=NULL;

A.h
extern FOO* gFoo;

gives: L2001 - unresolved external: "symbol struct FOO* gFoo"
Well, static means internal linkage, extern means external linkage. You
have to choose one. If the variable is static, it doesn't make much
sense to declare it as extern in the header.
if A.h looks like:
extern static FOO* gFoo;

it gives:
C2159: more than one storage class specified.

<blink, blink>
How would I do this now? I have a prefedined macro that makes a
"static FOO*". And I want to tell all my other .cpp files that it
exists in stdafx.h.


Why? It's static, so it doesn't exist outside of the .cpp file where
it's defined.

Jul 22 '05 #6
> Why? It's static, so it doesn't exist outside of the .cpp file where
it's defined.


Because: That is exactly what I didn't know. :) Thank you,
Gernot
Jul 22 '05 #7
Gernot Frisch wrote:
...
Why can't I tell another .cpp file that there is an static variable
somewhere else?


Because that what this particular use of 'static' is intended to do in
the first place! It is intended to make the variable "invisible" from
other translation units. If you want to be able to link to that variable
from other translation units - give it external linkage.

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #8
On Mon, 28 Jun 2004 17:53:22 +0800, "Vince Yuan" <sh*******@citiz.net>
wrote in comp.lang.c++:

First, don't top post.
Try this:

A.h
static FOO* gFoo;

A.cpp:
extern static FOO* gFoo=NULL;

Vince


Second, this is complete nonsense.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #9

"Rolf Magnus" <ra******@t-online.de> wrote in message
news:cb*************@news.t-online.com...
Gernot Frisch wrote:
Problem:

A.cpp:
------
static FOO* gFoo=NULL;

A.h
extern FOO* gFoo;

gives: L2001 - unresolved external: "symbol struct FOO* gFoo"


Well, static means internal linkage, extern means external linkage. You
have to choose one. If the variable is static, it doesn't make much
sense to declare it as extern in the header.


<rant>

You know, I can't for the life of me figure out why "static" was used to
mean internal linkage in this case. I'd think "intern" or something similar
would be much more logical, and simply eliminate the use of "static" for
non-member functions and variables. I know this is not the forum for asking
"why", but it really bugs me to have a keyword that has no apparent relation
to its use. One would think that "static" should have the same (or at least
similar) meaning in this context as it does in member functions and
variables. And since it makes no sense to try to apply the same meaning in
this case as in the member (or local variable) case, it really just
shouldn't exist at all here. "But that's just my opinion...I could be
wrong."

</rant>

-Howard

Jul 22 '05 #10
Howard wrote:
Well, static means internal linkage, extern means external linkage.
You have to choose one. If the variable is static, it doesn't make
much sense to declare it as extern in the header.

<rant>

You know, I can't for the life of me figure out why "static" was used
to
mean internal linkage in this case. I'd think "intern" or something
similar would be much more logical, and simply eliminate the use of
"static" for non-member functions and variables.


I think they didn't want to introduce a new keyword. Also, they wanted
to remain compatible to C, which alread had defined it this way. And
actionally the meaning that static has within a class definition is the
youngest one.
I know this is not the forum for asking "why", but it really bugs me
to have a keyword that has no apparent relation to its use.
I had more problems with the syntax they chose for pure virtual
functions. "Initializing" a function declaration with 0 looks very odd
to me. But again, I suspect they didn't want to add a new keyword
"pure".
One would think that "static" should have the same (or at
least similar) meaning in this context as it does in member functions
and variables.


It depends on how you look at it. If you look at it the right way,
static has more or less always the same meaning.
Within a class, static means that the function or variable is only there
once, for the class, not for each instance of it. Same for a local
static variable, if you count calling a function as "instantiating" it.
Well, and a static variable/function on namespace scope is there for
this one translation unit. It always has a meaning similar to "there is
only one".

Jul 22 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Joe | last post by:
I have 2 cpp modules (test.cpp, main.cpp). There is a static int record_count variable declared globally in the test.cpp and incremented in a function contained in the test.cpp module. I would...
10
by: Mark A. Gibbs | last post by:
I have a question about mixing C and C++. In a C++ translation unit, I want to define a function with internal linkage and C calling convention. Here's a sample of what I want to do: //...
2
by: Fredrik Olsson | last post by:
Hello I am developing a cross-platform solution for scheduled messages, primary for SMS. The server parts are written in ASNI/ISO C to compile and run cleanly on any POSIX compatible server. For...
4
by: ss | last post by:
hi! i am trying to use private static extern int PlaySound(String pszSound, int falgs); but it doesn't play the sound although it exists (even by checking file.exist) any solutions? 10x
4
by: Sean | last post by:
I am a little confused by the "extern inline and static inline" rules. I understand that "extern inline" guarantees no function storage is created (all are inlined). But the following test seems to...
5
by: Christian Christmann | last post by:
Hi, I've tree questions on the storage class specifier "extern": 1) Code example: int main( void ) { int b = -2; // my line 3 if ( a ) {
2
by: anuragch1 | last post by:
#include <stdio.h> void main() { static int i = 1; void cal(); printf("%d\n",i); cal(); } void cal()
14
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
1
by: Dinesh143 | last post by:
Where it is store the variables auto,static,extern,register,typedef,_declspec?
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.