472,371 Members | 1,696 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,371 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 4093
"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...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...

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.