473,807 Members | 2,884 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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%c gl%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 28430
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%c gl%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*******@citi z.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.l earn.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

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

Similar topics

6
1306
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 like to output the record_count value at the end of my main function in main.cpp. How shoudl I declare this in main.cpp or is there a better way to do this entirely? Thanks!
10
6202
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: // main.cpp // This is defined in a C module extern "C" void fake_qsort(void*, std::size_t, std::size_t, int (*compare)(const void*, const void*));
2
1695
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 the client-side API a generic API in ANSI/ISO C is written as well, and then wrappers for Perl, Objective-C, C++, COM, Java etc are written on top so that all developers can use whatever they feel comfortable with (Or more realistically what...
4
2114
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
7250
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 contradict what I expect---I found inc_i() is actually compiled as a linkable function in the a.out >g++ main.cpp call_inc.cpp >nm a.out ...... 0804875e W _Z5inc_iv
5
2862
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
1653
by: anuragch1 | last post by:
#include <stdio.h> void main() { static int i = 1; void cal(); printf("%d\n",i); cal(); } void cal()
14
6031
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 static inside functions (i.e. local static objects) 5. objects declared at file scope.
1
2295
by: Dinesh143 | last post by:
Where it is store the variables auto,static,extern,register,typedef,_declspec?
0
9720
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10626
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10112
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9193
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5546
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4330
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3854
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3011
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.