473,883 Members | 1,786 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.mod erated - moderation address: cl**@plethora.n et
Nov 13 '05 #1
2 4209
"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], "functionon e))
functionone();
else if(!strcmp(argv[1], "functiontw o"))
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.mod erated - moderation address: cl**@plethora.n et
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.ne t
--
comp.lang.c.mod erated - moderation address: cl**@plethora.n et
Nov 13 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
6222
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: // main.cpp // This is defined in a C module extern "C" void fake_qsort(void*, std::size_t, std::size_t, int (*compare)(const void*, const void*));
6
9585
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 i have also other functions defined in that file, i can use the global in all functions, but once i split up the rest of the function in other files, i cannot use the global? Isn't that strange, all the files compiled should be treated as one...
7
9047
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: extern char muser, mpass; However, in one of these functions outside of main.c I need to snprintf into these buffers. Usually when using snprintf I call it like this: snprintf(muser, sizeof muser, "text");
18
4297
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 where it's defined.
19
3878
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 storage-class specifier. My (little) C experience tells me that an object with "extern" is to let the linker knows that the object is referencing the object defined in somewhere, and this "somewhere" object does not have the storage-class specifier...
5
16612
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 extern storage class: /****************************************************************** SOURCE FILE ONE *******************************************************************/ extern int i; /* Reference to i, defined below */
17
4943
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
2912
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 extern where we declare it....So what is exactly happening here?
10
237
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 file that contains num's body? Or should it be declared as this, extern num (int n);
0
9792
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11142
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10848
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10417
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9575
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5798
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5992
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4614
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3234
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.