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 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
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
> 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
"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.
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.
> 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
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
"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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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!
|
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*));
|
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...
|
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
|
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
| |
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 ) {
|
by: anuragch1 |
last post by:
#include <stdio.h>
void main()
{
static int i = 1;
void cal();
printf("%d\n",i);
cal();
}
void cal()
|
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.
|
by: Dinesh143 |
last post by:
Where it is store the variables auto,static,extern,register,typedef,_declspec?
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |