469,610 Members | 1,494 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,610 developers. It's quick & easy.

Extern variables across different function binaries

Hi,

This is on an AS/400 which can be a little strange but I think the
basic question is portable.

I have a (non-C) program that needs to make series of calls
to some C programs/functions. The problem is that I need to
initialise certain variables within the first C program called and
then use them in subsequent calls to other C functions.

I have done exactly this using COBOL subprograms by specifying the
EXTERNAL keyword after rmy global variables. This works as long as the
COBOL program executes in the same thread as the main program, which I
can control on the AS/400.

C seems to behave differently in that I only seem to be able to share
variables if all of my C functions are linked together - which makes
it difficult to call them individually from my main program.

How can I reference extern or global variables from different C
functions which haven't been linked together into a single binary?

Any and all comments would be greatly appreciated.

Cheers,
Paul
--
comp.lang.c.moderated - moderation address: cl**@plethora.net
Nov 13 '05 #1
2 3983
"Paul M" <ne**@moonray32.vcisp.net> wrote in message

This is on an AS/400 which can be a little strange but I think the
basic question is portable.
Unfortunately the question isn't really platform-independent. However I
don't think you're wasting our time.
I have a (non-C) program that needs to make series of calls
to some C programs/functions. The problem is that I need to
initialise certain variables within the first C program called and
then use them in subsequent calls to other C functions.
Normally a C program ceases to have any connection to the C language once it
is compiled to an executable. Also, it is usual for programs to have
protected memory spaces - one program cannot alter variables in another one
without using special facilities of the operating system.
I have done exactly this using COBOL subprograms by specifying the
EXTERNAL keyword after rmy global variables. This works as long as the
COBOL program executes in the same thread as the main program, which > I can control on the AS/400. I don't know anything about COBOL. It is quite possible that COBOL programs
are not compiled to native machine code like C programs
C seems to behave differently in that I only seem to be able to share
variables if all of my C functions are linked together - which makes
it difficult to call them individually from my main program.
I presume your main program evokes the C (ie machine code program) somehow.
What you can do is write your C program to take a function name as a command
line parameter.

eg

int main(int argc, char **argv)
{
if(!strcmp(argv[1], "functionone))
functionone();
else if(!strcmp(argv[1], "functiontwo"))
functiontwo();
}
You will still have the problem that the shared variables evaporate when the
program returns. You will have to write them out to a file or use a similar
workaround.
How can I reference extern or global variables from different C
functions which haven't been linked together into a single binary?

Generally this can't be done because all the symbols are stripped from the
final executable, and even if you had the symbols two machine code programs
can't access each others' memory space easily. However your platform will
probably provide some services for interprocess communication. These tend to
be tricky to use, particularly if you are just beginning with C.
--
comp.lang.c.moderated - moderation address: cl**@plethora.net
Nov 13 '05 #2
On 09 Jul 2003 17:28:05 GMT, Paul M <ne**@moonray32.vcisp.net> wrote:
Hi,

This is on an AS/400 which can be a little strange but I think the
basic question is portable.
Sorry, it's not.
I have a (non-C) program that needs to make series of calls
to some C programs/functions. The problem is that I need to
initialise certain variables within the first C program called and
then use them in subsequent calls to other C functions.

I have done exactly this using COBOL subprograms by specifying the
EXTERNAL keyword [and] execut[ing] in the same thread [...]
How can I reference extern or global variables from different C
functions which haven't been linked together into a single binary?

There is no standard requirement nor portable solution for this,
in fact it isn't possible at all on some platforms. Nor is there any
standard requirement for C to be called from and/or call to other
language(s), although that is much more common.

If there is a solution for your platform, and there probably is if
there is one for COBOL, it is platform specific and needs to be sought
in a platform specific group or source. Good luck.

- David.Thompson1 at worldnet.att.net
--
comp.lang.c.moderated - moderation address: cl**@plethora.net
Nov 13 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Mark A. Gibbs | last post: by
6 posts views Thread by atv | last post: by
7 posts views Thread by Kieran Simkin | last post: by
18 posts views Thread by tweak | last post: by
19 posts views Thread by ccwork | last post: by
5 posts views Thread by siliconwafer | last post: by
17 posts views Thread by Tapeesh | last post: by
2 posts views Thread by Shraddha | last post: by
10 posts views Thread by Bill Cunningham | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.