By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,417 Members | 1,087 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,417 IT Pros & Developers. It's quick & easy.

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

P: n/a

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

Feb 18 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On 17 Feb 2007 21:00:10 -0800, "si***************@gmail.com"
<si***************@gmail.comwrote:
>
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
Feb 18 '07 #2

P: n/a
On Feb 17, 11:07 pm, Dave Rahardja
<drahardja_atsign_pobox_dot_...@pobox.comwrote:
On 17 Feb 2007 21:00:10 -0800, "silverburgh.me...@gmail.com"

<silverburgh.me...@gmail.comwrote:
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, "silverburgh.me...@gmail.com"

<silverburgh.me...@gmail.comwrote:
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.

Feb 18 '07 #3

P: n/a
I V
On Sat, 17 Feb 2007 21:38:09 -0800, si***************@gmail.com 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);
}
Feb 18 '07 #4

P: n/a
<si***************@gmail.comwrote in message
news:11**********************@t69g2000cwt.googlegr oups.com
On Feb 17, 11:07 pm, Dave Rahardja
<drahardja_atsign_pobox_dot_...@pobox.comwrote:
>On 17 Feb 2007 21:00:10 -0800, "silverburgh.me...@gmail.com"

<silverburgh.me...@gmail.comwrote:
>>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
Feb 18 '07 #5

P: n/a
s5n
On Feb 18, 6:00 am, "silverburgh.me...@gmail.com"
<silverburgh.me...@gmail.comwrote:
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

Feb 18 '07 #6

P: n/a
On Feb 18, 11:08 am, "s5n" <step...@s11n.netwrote:
On Feb 18, 6:00 am, "silverburgh.me...@gmail.com"

<silverburgh.me...@gmail.comwrote:
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.

Feb 19 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.