Extern variables across different function binaries

P

Paul M

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
 
M

Malcolm

Paul M said:
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.
 
D

Dave Thompson

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top