473,394 Members | 2,090 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 4178
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
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: //...
6
by: atv | last post by:
Alright, i have some questions concerning include files en global variables.I hope someone is willing to answer these. 1).Why is it that if i define a global variable in a file, say main.c, and...
7
by: Kieran Simkin | last post by:
I have in my project in main.c a number of arrays defined like this (outside of any functions): char muser, mpass; I'm declaring them in a number of other .c files (inside functions) like this:...
18
by: tweak | last post by:
What's the best way to use extern when using multiplefiles that is easiest to maintain? Is it best to declare: extern int a; in a header file and include the header file in all files except...
19
by: ccwork | last post by:
Hi all, I am reading "C: A Reference Manual" 4th ed and I get lost for the "extern". It says that global object without specifying the storage-class specifier will have "extern" as the default...
5
by: siliconwafer | last post by:
Hi all, I wanted to know that is use of extern keyword mandatory in case of global variables and functions used in other source files? i.e consider a following piece of code from MSDN explaining...
17
by: Tapeesh | last post by:
I would like to know what is the expected behaviour of C compilers when an extern decleration is intialized. When the following code is compiled using gcc //File extern.c int arr ; int a ;
2
by: Shraddha | last post by:
When I read about volatile keyword....while explaning there was an declaration like... extern const a; But as a rule const shoule be initialised where it is defined...but we do ot initialise...
10
by: Bill Cunningham | last post by:
Is extern used to externalize functions as well as variables? If I have this function, int num(int n); Should it be declared as above in the file in which main is called and compiled with the...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
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,...
0
jinu1996
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.