473,473 Members | 1,714 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

duplicate functions in multiple modules

Hi,

I have a project with multiple c files. Some of these modules have
functions that have the same name and their operations are exactly the
same.

My rusty memory tells me there is a way with preprocessor directives to
make sure these functions are not included more than once, but I forgot
how to.

Can somebody please give me an example?

Nov 13 '06 #1
4 3129

"wanwan" <er*******@yahoo.comwrote in message
news:11*********************@m7g2000cwm.googlegrou ps.com...
Hi,

I have a project with multiple c files. Some of these modules have
functions that have the same name and their operations are exactly the
same.

My rusty memory tells me there is a way with preprocessor directives to
make sure these functions are not included more than once, but I forgot
how to.

Can somebody please give me an example?
Sounds like you are thinking of the
#ifndef xxx
#define xxx
...
#endif
that is used to ensure that INCLUDE files are not included multiple times.
However, for .c files, you need to do other things to ensure there
are no name clashes.

Some ways:
For each function that is not a public function called from outside the
file, make the function static.

Add a prefix on each function that is publicly available; say,
a two-letter prefix that indicates the file or file grouping.

--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project
Nov 14 '06 #2
The only answer I can think of (given the low amount of details you are
giving) is to surround these functions (declaration and implementation)
with preprocessor directives as follow:

#ifndef XXX //if not defined yet
#define XXX //now it is defined
your functions here
....
#endif

You should normally always surround header files with such declaration,
to make sure they are not included several times.
Remember that C doesn't support function overloading, so it's generally
not a good idea to have functions with the same name. But you may have
your reasons.

Brice


wanwan wrote:
Hi,

I have a project with multiple c files. Some of these modules have
functions that have the same name and their operations are exactly the
same.

My rusty memory tells me there is a way with preprocessor directives to
make sure these functions are not included more than once, but I forgot
how to.

Can somebody please give me an example?
Nov 14 '06 #3
On 13 Nov 2006 15:30:47 -0800, "wanwan" <er*******@yahoo.comwrote in
comp.lang.c:
Hi,

I have a project with multiple c files. Some of these modules have
functions that have the same name and their operations are exactly the
same.
Your description is not clear. C does not have "modules". C has
"translation units". In general terms, a translation unit consists of
what we usually call a source file and any headers and anything else
it includes.
My rusty memory tells me there is a way with preprocessor directives to
make sure these functions are not included more than once, but I forgot
how to.
There is no preprocessor directive that can help you if you have more
than one source file that defines a function with external linkage and
the same name. Other than the nasty macros in each file (or all but
one) redefining the name. But it is better to rename them yourself.
Can somebody please give me an example?
If you have defined functions or objects in header files without the
static keyword and included such header files in more than one source
file, there is nothing the preprocessor can do, either.

You need to post a cut-down sample, such as a header file and two
source files that contain such functions. But the best option is not
to do that.

A function should be defined in exactly one source file. A prototype
for that function should be placed in a header file. That header file
should be included in the source file that defines the function, and
also in any other source files that want to call the function.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '06 #4
Remember that C doesn't support function overloading, so it's generally
Even C++ doesn't support function overloading if this is used in the
c++ files,

#ifdef __cplusplus
extern "C" {
#endif

;-)

Nov 14 '06 #5

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

Similar topics

99
by: David MacQuigg | last post by:
I'm not getting any feedback on the most important benefit in my proposed "Ideas for Python 3" thread - the unification of methods and functions. Perhaps it was buried among too many other less...
7
by: Lowell Kirsh | last post by:
I have a script which I use to find all duplicates of files within a given directory and all its subdirectories. It seems like it's longer than it needs to be but I can't figure out how to shorten...
11
by: Danny Pressley | last post by:
I have a VS.NET 2003 Visual C++ MFC Application Project in which I added a new resource file, I then added an icon to this new resource file and did a rebuild and got the following error: "fatal...
3
by: Ruud van Gaal | last post by:
Hi, I'm probably wrong at this, but can't understand the following: I have 2 separate modules, mod1.cpp and mod2.cpp: --- mod1.cpp --- #include <stdio.h> void mod2();
5
by: Thomas Matthews | last post by:
Hi, I have several translation units (modules) which contain static {local} variables. These function have short global accessor functions. I would like to change these functions into macros,...
1
by: Mahesh D. Rane | last post by:
Make 5 Modules in Business Object Layer ( This will make one common dll for all the modules in Business Object layer) Make 5 Modules in Data Access Layer ( This will make one common dll for all...
13
by: Robin Haswell | last post by:
Hey people I'm an experience PHP programmer who's been writing python for a couple of weeks now. I'm writing quite a large application which I've decided to break down in to lots of modules...
47
by: teju | last post by:
hi, i am trying 2 merge 2 projects into one project.One project is using c language and the other one is using c++ code. both are working very fine independently.But now i need to merge both...
11
by: mdh | last post by:
I decided to make a single file containing all the repetitive functions in K&R so that I could concentrate on the new discussions. This went along just fine, and with each new function, added the...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
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,...
1
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...
0
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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...

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.