LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-28-2012, 06:37 AM   #1
franmf
LQ Newbie
 
Registered: Oct 2012
Posts: 28

Rep: Reputation: Disabled
shared variables in C. Need to access same variable from two .c files


hi, how do I have to do to share a variable that need to modify in one file and need to access from another? thank you.

That would be an array, just that.
 
Old 11-28-2012, 06:51 AM   #2
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
What do you mean? Can you give an example of what you are trying to do? If you mean to access a variable that is defined within one compilation unit from another one, you can use the extern keyword:

Code:
/* variable.h */

#ifndef _VARIABLE_H
#define _VARIABLE_H

extern int externalVariable;

#endif
Code:
/* variable.c */

int externalVariable = 1;
Code:
/* main.c */
#include <stdio.h>
#include "variable.h"

int main() {
    printf("%d\n", externalVariable);
    return 0;
}
edit: The extern keyword declares a variable (but does not define it). It is similar to a function prototype. It tells the compiler when main.c is being compiled that there is a variable called externalVariable, which is of type int and will be available during linking. A variable may be declared multiple times with extern, but at least (and not more than) once defined somewhere.

Last edited by millgates; 11-28-2012 at 07:01 AM.
 
Old 11-28-2012, 06:56 AM   #3
franmf
LQ Newbie
 
Registered: Oct 2012
Posts: 28

Original Poster
Rep: Reputation: Disabled
I think that's what I need. I need to fill an array from class1.c, and I need to access that array from both class1.c and class2.c, so I'll try that. thank you very much.

is it necessary to create variable.c? because I fill the array in class1.c and not in variable.c, is it neccesary to have the same name variable.c and variable.h?

Last edited by franmf; 11-28-2012 at 06:58 AM.
 
Old 11-28-2012, 07:05 AM   #4
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
Quote:
Originally Posted by franmf View Post
how do I have to do to share a variable that need to modify in one file and need to access from another?
...
That would be an array, just that.
Pretty much answered by millgates, but I think I can say it more clearly:

You define your variable as a global in one module and declare it as extern in every other module that uses it.

There is no keyword for global. A variable in C is global if you define it outside of any function and you do not use the keyword static. So:
Code:
int myArray[23];
If that is inside a function definition, it is an ordinary (automatic) variable with scope in just that function. But the same line outside of any function defined a global array.

In other modules
Code:
extern int myArray[23];
In big projects, various header file methods are appropriate to help keep such declarations in sync across all modules using them. The answer by millgates assumes you will want to get in the habit of doing it that way from the start, even if you currently have only two modules.

Edit: I was answering while you were asking more.

Quote:
Originally Posted by franmf View Post
is it necessary to create variable.c? because I fill the array in class1.c and not in variable.c, is it neccesary to have the same name variable.c and variable.h?
No. All those details are just there to make maintenance of large projects practical. In a small project, doing things that way would only be to get in the right habits for later large projects (so you might want to, but that is your decision, not ours).

Last edited by johnsfine; 11-28-2012 at 07:08 AM.
 
Old 11-28-2012, 07:11 AM   #5
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Quote:
Originally Posted by franmf View Post
is it necessary to create variable.c? because I fill the array in class1.c and not in variable.c, is it neccesary to have the same name variable.c and variable.h?
You may name those files any way you like. I just used this as an example. You just define the variable in one file without extern and then with extern in the file from which you want to access it.
But it all depends on what exactly your code looks like. You may avoid the need for using extern alltogether if you redesign your code. You may also just pass the pointer to the array to the other function as parameter. But I don't know what your program looks like and what are you trying to do in the first place, so it's hard to tell.
 
Old 11-28-2012, 07:35 AM   #6
franmf
LQ Newbie
 
Registered: Oct 2012
Posts: 28

Original Poster
Rep: Reputation: Disabled
thank you both. I think I do understand what you mean.

I have the next.

class1.c

Code:
int ip_status[10];
size=10;
void main(){
        ...
	for(i=0;i<size;i++){
		ip_status[i]=2;
	}
        ...
}
class2.c
Code:
extern int ip_status[10];
int status = 2;
for(j=0;j<size;j++){
        if(strcmp(ip_status[j],status) == 0 ){
                printf("match!!\n");
        }
}
but when compiling, the next error appears:

Code:
/home/fran/backfire/build_dir/target-mips_r2_uClibc-0.9.30.1/knock-0.5/src/knockd.c:147: undefined reference to `ip_status'
 
Old 11-28-2012, 07:40 AM   #7
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Code:
extern int ip_status[10];
int status = 2;
if(strcmp(ip_status[i],status) == 0 ){
       printf("match!!\n");
}
"undefined reference ..." is a linker error.
What command do you use to compile the program?
 
Old 11-28-2012, 07:46 AM   #8
franmf
LQ Newbie
 
Registered: Oct 2012
Posts: 28

Original Poster
Rep: Reputation: Disabled
I'm modifing a packet from OpenWrt backfire firmware.

I'm using a makefile archive:
being class1.c and class2.c knock and knockd
Code:
...
CXX = @CC@
CXXFLAGS += @CFLAGS@ -g -Wall -pedantic -fno-exceptions \
            -D_GNU_SOURCE -I.
LDFLAGS += @LDFLAGS@

SRCS = $(SRCDIR)knockd.c \
			 $(SRCDIR)knock.c \
			 $(SRCDIR)list.c

all: knockd knock man

knockd: $(OBJDIR)knockd.o $(OBJDIR)list.o
	$(CXX) $(OBJDIR)knockd.o -lpthread $(OBJDIR)list.o -o $@ $(LDFLAGS) -lpcap

knock: $(OBJDIR)knock.o
	$(CXX) $(OBJDIR)knock.o -o $@ $(LDFLAGS)

.c.o: $(SRCS)
	$(CXX) $(CXXFLAGS) -o $@ -c $<
...
 
Old 11-28-2012, 08:01 AM   #9
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
So class1 and class2 or whatever they're called are two separate binaries? Then I really don't understand what you are trying to achieve. You want to access a variable in one program from another one?
 
Old 11-28-2012, 08:07 AM   #10
franmf
LQ Newbie
 
Registered: Oct 2012
Posts: 28

Original Poster
Rep: Reputation: Disabled
knock and knockd are two .c files in the same directory. Both have their own main, but apart from that, I'd need to share a variable between them. maybe it can't be done and I have to think another way to do such
 
Old 11-28-2012, 08:12 AM   #11
franmf
LQ Newbie
 
Registered: Oct 2012
Posts: 28

Original Poster
Rep: Reputation: Disabled
finally I tried to do your first choice:

variable.h
Code:
#ifndef _GLOBAL_H_
#define _GLOBAL_H_

int ip_status[10];
char ips2[10][30];

#endif
include "variable.h"

and just call them as if they were declared

it compiles.

thank you. Tomorrow morning I'll see if it works because I need the router which is not here righ now.
 
Old 11-28-2012, 08:17 AM   #12
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
In that case that cannot be solved by extern. That would have worked if class1.c and class2.c were compiled into a single binary. In your case, you will have to use some sort of inter-process communication. It is not trivial topic, though. Is there a reason why you can't do what you want to do in one program?

Last edited by millgates; 11-28-2012 at 08:27 AM.
 
Old 11-28-2012, 08:20 AM   #13
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Quote:
Originally Posted by franmf View Post
finally I tried to do your first choice:

variable.h
Code:
#ifndef _GLOBAL_H_
#define _GLOBAL_H_

int ip_status[10];
char ips2[10][30];

#endif
include "variable.h"

and just call them as if they were declared

it compiles.

thank you. Tomorrow morning I'll see if it works because I need the router which is not here righ now.
Actually, this is not exactly the same as what I posted before.

You mean you just include that in both ".c" files? That will certainly compile, but won't work because each program will get its own, independent variables.
 
Old 11-28-2012, 08:27 AM   #14
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
Quote:
Originally Posted by franmf View Post
how do I have to do to share a variable that need to modify in one file and need to access from another?
We initially misunderstood the word "file" in that request.

The answers assumed "file" meant .c file within one program. But now we understand it means two separate programs.

I would tend to think that means two programs running together on one computer, so shared memory is the logical answer.

But:

Quote:
Originally Posted by franmf View Post
I'll see if it works because I need the router which is not here righ now.
That implies two programs running on different computers.

Please clarify what you actually need to accomplish.

For programs running on different computers, you can't actually share a variable. But you can get roughly the same results as a shared variable would have allowed, using methods such as MPI.

Last edited by johnsfine; 11-28-2012 at 08:33 AM.
 
Old 11-28-2012, 08:41 AM   #15
franmf
LQ Newbie
 
Registered: Oct 2012
Posts: 28

Original Poster
Rep: Reputation: Disabled
first of all sorry for my english, it may be confusing.

what I have are two programms running at the same time in the router. So, while one of them is running, I use the other to input external data with scanf. So when I insert that data the already running programm does one thing or another depending on the new data I have just inserted.

I don't know if I explain myselft...sorry for that.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't access shared window files davec51 Slackware 8 08-18-2012 11:41 AM
how to access sysfs files in shared objects akshaynm87 Linux - Newbie 3 05-06-2009 04:44 AM
Trying to access shared files on windows computer dwood108 Linux - Newbie 3 09-25-2007 11:22 AM
Cannot access shared XP files from my SuSE machine Hallowedpoint Linux - Networking 1 01-06-2006 08:52 AM
how to access windows shared files dagul_t Linux - Networking 4 07-07-2005 11:13 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:35 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration