Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How can I reference a static array defined in another .cpp file

Reply
Thread Tools

How can I reference a static array defined in another .cpp file

 
 
silverburgh.meryl@gmail.com
Guest
Posts: n/a
 
      02-18-2007

Hi,

In one A.cpp file, I have defined a static array of JSFunctionSpec,
like this:

static JSFunctionSpec JProfFunctions[] = {
{"JProfStartProfiling", JProfStartProfiling,
0, 0, 0
},
{"JProfStopProfiling", JProfStopProfiling,
0, 0, 0},
{nsnull, nsnull,
0, 0, 0}
};


how can I use that JProfFunctions in another .cpp file (say B.cpp)?

i tried this in my B.cpp:

extern JSFunctionSpec[] JProfFunctions[];

void aFunction () {
::JS_DefineFunctions(JProfFunctions);
}

I get compilation error: 'JProfFunctions' was not declared in this
scope

 
Reply With Quote
 
 
 
 
Dave Rahardja
Guest
Posts: n/a
 
      02-18-2007
On 17 Feb 2007 21:00:10 -0800, "(E-Mail Removed)"
<(E-Mail Removed)> wrote:

>
>Hi,
>
>In one A.cpp file, I have defined a static array of JSFunctionSpec,
>like this:
>
>static JSFunctionSpec JProfFunctions[] = {
> {"JProfStartProfiling", JProfStartProfiling,
>0, 0, 0
> },
> {"JProfStopProfiling", JProfStopProfiling,
>0, 0, 0},
> {nsnull, nsnull,
>0, 0, 0}
> };
>
>
>how can I use that JProfFunctions in another .cpp file (say B.cpp)?
>
>i tried this in my B.cpp:
>
>extern JSFunctionSpec[] JProfFunctions[];
>
>void aFunction () {
> ::JS_DefineFunctions(JProfFunctions);
>}
>
>I get compilation error: 'JProfFunctions' was not declared in this
>scope


The fact that you declared the array static means that it is not visible from
another compilation unit.

What are you trying to achieve? Is that array supposed to have internal
(static) linkage?

-dr
 
Reply With Quote
 
 
 
 
silverburgh.meryl@gmail.com
Guest
Posts: n/a
 
      02-18-2007
On Feb 17, 11:07 pm, Dave Rahardja
<(E-Mail Removed)> wrote:
> On 17 Feb 2007 21:00:10 -0800, "(E-Mail Removed)"
>
>
>
> <(E-Mail Removed)> wrote:
>
> >Hi,

>
> >In one A.cpp file, I have defined a static array of JSFunctionSpec,
> >like this:

>
> >static JSFunctionSpec JProfFunctions[] = {
> > {"JProfStartProfiling", JProfStartProfiling,
> >0, 0, 0
> > },
> > {"JProfStopProfiling", JProfStopProfiling,
> >0, 0, 0},
> > {nsnull, nsnull,
> >0, 0, 0}
> > };

>
> >how can I use that JProfFunctions in another .cpp file (say B.cpp)?

>
> >i tried this in my B.cpp:

>
> >extern JSFunctionSpec[] JProfFunctions[];

>
> >void aFunction () {
> > ::JS_DefineFunctions(JProfFunctions);
> >}

>
> >I get compilation error: 'JProfFunctions' was not declared in this
> >scope

>
> The fact that you declared the array static means that it is not visible from
> another compilation unit.
>
> What are you trying to achieve? Is that array supposed to have internal
> (static) linkage?
>
> -dr



I want to define that array in 1 .cpp file, but other .cpp files can
use the array.
so I should remove the 'static' in my A.cpp file, but how can other
files (e.g. B.cpp) see that array (defined in A.cpp)?


I tried this in my B.cpp:

extern JSFunctionSpec[] JProfFunctions[];

void aFunction () {
::JS_DefineFunctions(JProfFunctions);
}
But I get compilation error: 'JProfFunctions' was not declared in this
scope.

> On 17 Feb 2007 21:00:10 -0800, "(E-Mail Removed)"
>
>
>
> <(E-Mail Removed)> wrote:
>
> >Hi,

>
> >In one A.cpp file, I have defined a static array of JSFunctionSpec,
> >like this:

>
> >static JSFunctionSpec JProfFunctions[] = {
> > {"JProfStartProfiling", JProfStartProfiling,
> >0, 0, 0
> > },
> > {"JProfStopProfiling", JProfStopProfiling,
> >0, 0, 0},
> > {nsnull, nsnull,
> >0, 0, 0}
> > };

>
> >how can I use that JProfFunctions in another .cpp file (say B.cpp)?

>
> >i tried this in my B.cpp:

>
> >extern JSFunctionSpec[] JProfFunctions[];

>
> >void aFunction () {
> > ::JS_DefineFunctions(JProfFunctions);
> >}

But I get compilation error: 'JProfFunctions' was not declared in this
scope.



 
Reply With Quote
 
I V
Guest
Posts: n/a
 
      02-18-2007
On Sat, 17 Feb 2007 21:38:09 -0800, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
>> >how can I use that JProfFunctions in another .cpp file (say B.cpp)?

>>
>> >i tried this in my B.cpp:

>>
>> >extern JSFunctionSpec[] JProfFunctions[];


This is a syntax error. Try:

extern JSFunctionSpec JProfFunctions[];

>> >void aFunction () {
>> > ::JS_DefineFunctions(JProfFunctions);
>> >}


 
Reply With Quote
 
John Carson
Guest
Posts: n/a
 
      02-18-2007
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com
> On Feb 17, 11:07 pm, Dave Rahardja
> <(E-Mail Removed)> wrote:
>> On 17 Feb 2007 21:00:10 -0800, "(E-Mail Removed)"
>>
>>
>>
>> <(E-Mail Removed)> wrote:
>>
>>> Hi,

>>
>>> In one A.cpp file, I have defined a static array of JSFunctionSpec,
>>> like this:

>>
>>> static JSFunctionSpec JProfFunctions[] = {
>>> {"JProfStartProfiling", JProfStartProfiling,
>>> 0, 0, 0
>>> },
>>> {"JProfStopProfiling", JProfStopProfiling,
>>> 0, 0, 0},
>>> {nsnull, nsnull,
>>> 0, 0, 0}
>>> };

>>
>>> how can I use that JProfFunctions in another .cpp file (say B.cpp)?

>>
>>> i tried this in my B.cpp:

>>
>>> extern JSFunctionSpec[] JProfFunctions[];

>>
>>> void aFunction () {
>>> ::JS_DefineFunctions(JProfFunctions);
>>> }

>>
>>> I get compilation error: 'JProfFunctions' was not declared in this
>>> scope

>>
>> The fact that you declared the array static means that it is not
>> visible from another compilation unit.
>>
>> What are you trying to achieve? Is that array supposed to have
>> internal (static) linkage?
>>
>> -dr

>
>
> I want to define that array in 1 .cpp file, but other .cpp files can
> use the array.
> so I should remove the 'static' in my A.cpp file



Yes. static in this context means that the definition is restricted to the
translation unit in which it appears. Drop it (and fix the syntax error
pointed out by IV).

--
John Carson


 
Reply With Quote
 
s5n
Guest
Posts: n/a
 
      02-18-2007
On Feb 18, 6:00 am, "(E-Mail Removed)"
<(E-Mail Removed)> wrote:
> Hi,
>
> In one A.cpp file, I have defined a static array of JSFunctionSpec,
> like this:
>
> static JSFunctionSpec JProfFunctions[] = {
> {"JProfStartProfiling", JProfStartProfiling,
> 0, 0, 0
> },
> {"JProfStopProfiling", JProfStopProfiling,
> 0, 0, 0},
> {nsnull, nsnull,
> 0, 0, 0}
> };


Try this:

In A.cpp:

JSFunctionSpec * my_funcs()
{
static std::vector<JSFunctionSpec *> funcs;
if( funcs.empty() ){
... populate funcs and end with a trailing null
JSFunctionSpec because JS_DefinFunctions() likes
is that way ...
}
return &funcs[0];
]

Now add that function to your API (A.hpp) so you can reference it from
B.cpp.

[Then, since you're using SpiderMonkey from C++, take a look at my C++
toolkit for SpiderMonkey, which simplifies this type of thing:
http://SpiderApe.sf.net ]

Here's an example taken from the SpiderApe code:

typedef std::vector<JSFunctionSpec *> JSFuncVector;

/**
Used by MonkeyWrapper to get the default array of
script-callable functions.

You may use this vector with JS_DefineFunctions(), but
should follow this guideline:

<pre>
JSFuncVector myvec( standard_js_functions() );
myvec.push_back( 0 );
JS_DefineFunctions(cx,obj, myvec[0]);
</pre>

This ensures that the vector ends with a NULL element, as
required by JS_DefineFunctions(). Since arbitrary code can
add functions to standard_js_functions(), there is normally
no guaranty that the final element is null. It is poor
practice
to add a NULL element to standard_js_functions() because
elements
added after it would be ignored by JS_DefineFunctions().

On the first call to this function, the returned vector is
pre-populated with whatever functions the ape API provides
for script-side code, so this vector will always have at
least a few entries unless client code is foolish enough to
empty it.
*/
APE_PUBLIC_API JSFuncVector & standard_js_functions();



JSBool
init_functions( JSContext * cx, JSObject * obj )
{
/**
2006.05.30: we COPY standard_js_functions() because
older internal code used vector::reserve() and
operator[] to populate the vector. This led to an
incorrect value being returned from vector::size()
and an
uncertainty about whether or not the vector has a
trailing
NULL entry (needed by JS_DefineFunctions()). It is
a wonder
that it ever worked before.
*/
JSFuncVector funcs( standard_js_functions() );
funcs.push_back( 0 ); // ensure a trailing NULL entry
if( ! JS_DefineFunctions( cx, obj, funcs[0] ) )
{
JS_ReportError(cx,"ape::init_functions():
JS_DefineFunctions() failed!");
return JS_FALSE;
}
return JS_TRUE;
}


----- stephan

 
Reply With Quote
 
silverburgh.meryl@gmail.com
Guest
Posts: n/a
 
      02-18-2007
On Feb 18, 11:08 am, "s5n" <(E-Mail Removed)> wrote:
> On Feb 18, 6:00 am, "(E-Mail Removed)"
>
> <(E-Mail Removed)> wrote:
> > Hi,

>
> > In one A.cpp file, I have defined a static array of JSFunctionSpec,
> > like this:

>
> > static JSFunctionSpec JProfFunctions[] = {
> > {"JProfStartProfiling", JProfStartProfiling,
> > 0, 0, 0
> > },
> > {"JProfStopProfiling", JProfStopProfiling,
> > 0, 0, 0},
> > {nsnull, nsnull,
> > 0, 0, 0}
> > };

>
> Try this:
>
> In A.cpp:
>
> JSFunctionSpec * my_funcs()
> {
> static std::vector<JSFunctionSpec *> funcs;
> if( funcs.empty() ){
> ... populate funcs and end with a trailing null
> JSFunctionSpec because JS_DefinFunctions() likes
> is that way ...
> }
> return &funcs[0];
> ]
>
> Now add that function to your API (A.hpp) so you can reference it from
> B.cpp.
>
> [Then, since you're using SpiderMonkey from C++, take a look at my C++
> toolkit for SpiderMonkey, which simplifies this type of thing:http://SpiderApe.sf.net ]
>
> Here's an example taken from the SpiderApe code:
>
> typedef std::vector<JSFunctionSpec *> JSFuncVector;
>
> /**
> Used by MonkeyWrapper to get the default array of
> script-callable functions.
>
> You may use this vector with JS_DefineFunctions(), but
> should follow this guideline:
>
> <pre>
> JSFuncVector myvec( standard_js_functions() );
> myvec.push_back( 0 );
> JS_DefineFunctions(cx,obj, myvec[0]);
> </pre>
>
> This ensures that the vector ends with a NULL element, as
> required by JS_DefineFunctions(). Since arbitrary code can
> add functions to standard_js_functions(), there is normally
> no guaranty that the final element is null. It is poor
> practice
> to add a NULL element to standard_js_functions() because
> elements
> added after it would be ignored by JS_DefineFunctions().
>
> On the first call to this function, the returned vector is
> pre-populated with whatever functions the ape API provides
> for script-side code, so this vector will always have at
> least a few entries unless client code is foolish enough to
> empty it.
> */
> APE_PUBLIC_API JSFuncVector & standard_js_functions();
>
> JSBool
> init_functions( JSContext * cx, JSObject * obj )
> {
> /**
> 2006.05.30: we COPY standard_js_functions() because
> older internal code used vector::reserve() and
> operator[] to populate the vector. This led to an
> incorrect value being returned from vector::size()
> and an
> uncertainty about whether or not the vector has a
> trailing
> NULL entry (needed by JS_DefineFunctions()). It is
> a wonder
> that it ever worked before.
> */
> JSFuncVector funcs( standard_js_functions() );
> funcs.push_back( 0 ); // ensure a trailing NULL entry
> if( ! JS_DefineFunctions( cx, obj, funcs[0] ) )
> {
> JS_ReportError(cx,"ape::init_functions():
> JS_DefineFunctions() failed!");
> return JS_FALSE;
> }
> return JS_TRUE;
> }
>
> ----- stephan

Thanks for all the help.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Static array defined in one module, but used in another module pozzugno@gmail.com C Programming 6 10-24-2012 07:30 PM
how can I access an array defined in another module vabby Perl Misc 6 01-02-2007 11:43 AM
#if (defined(__STDC__) && !defined(NO_PROTOTYPE)) || defined(__cplusplus) Oodini C Programming 1 09-27-2005 07:58 PM
how to use static function defined in one file in another file is that impposiible in 'c ' rashmi C Programming 5 04-29-2005 03:26 PM



Advertisments