By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,421 Members | 1,128 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,421 IT Pros & Developers. It's quick & easy.

same name global variable multi files

P: n/a
Hi,

I used to have different c code in different files and compiled as
different programs. For example, I have

proc1.c:

int x1, x2;

void work1()
{
int b = x1 + x2;
...
}

x1 and x2 are global variables and proc1.c and other c files are
compiled as to a program called "p1".
proc2.c:

int x1, x2;

void work2()
{
int b = x1 * x2;
...
}

proc2.c and other c files are compiled to a program called "p2".

I am now working on a new program where I would like to have both
function "work1" and "work2". I tried to share the same code but it did
not link because x1 and x2 has the same name in proc1 and proc2. Since
proc1 and proc2 have many many global variable with the same names and
I have even proc3, proc4...., I do not want to change them one by one.
Is there a good way to solve the naming conflict? Since I still use the
code in my separated program p1 and p2, I would like to keep the change
as little as possible. I do not want to put proc.1 into a c++ class,
for example.

Thanks,
qq

Jun 30 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a

<qu******@yahoo.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...
Hi,

I used to have different c code in different files and compiled as
different programs. For example, I have

proc1.c:

int x1, x2;

void work1()
{
int b = x1 + x2;
...
}

x1 and x2 are global variables and proc1.c and other c files are
compiled as to a program called "p1".
proc2.c:

int x1, x2;

void work2()
{
int b = x1 * x2;
...
}

proc2.c and other c files are compiled to a program called "p2".

I am now working on a new program where I would like to have both
function "work1" and "work2". I tried to share the same code but it did
not link because x1 and x2 has the same name in proc1 and proc2. Since
proc1 and proc2 have many many global variable with the same names and
I have even proc3, proc4...., I do not want to change them one by one.
Is there a good way to solve the naming conflict? Since I still use the
code in my separated program p1 and p2, I would like to keep the change
as little as possible. I do not want to put proc.1 into a c++ class,
for example.


You could wrap the contents of either or both files in their own namespace.
That's the usual C++ solution to name conflicts.

Even better would be to fix the problem by getting rid of the global
variables altogether. But I guess you don't want to do the work to make it
right. You're seeing now one of the problems of not doing it right the
first time.

By the way, are you actually compiling these as C++? The filenames ending
in ".c" often tell a compiler to compile as C, not C++.

-Howard


Jun 30 '06 #2

P: n/a
qu******@yahoo.com posted:
I tried to share the same code but it did
not link because x1 and x2 has the same name in proc1 and proc2.


C solution: Define the objects as "static".

C++ solution: Define the objects with an anonymous namespace.
--

Frederick Gotham
Jun 30 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.