473,809 Members | 2,699 Online
Bytes | Software Development & Data Engineering Community
+ 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 3157

"wanwan" <er*******@yaho o.comwrote in message
news:11******** *************@m 7g2000cwm.googl egroups.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*******@yaho o.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
"translatio n 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.l earn.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
5934
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 important changes, so in this thread I would like to focus on that issue alone. I have edited the Proposed Syntax example below to take out the changes unecessary to this discussion. I left in the change of "instance variable" syntax (...
7
2337
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 it. Perhaps there are some python features or libraries I'm not taking advantage of. The way it works is that it puts references to all the files in a dictionary with file size being the key. The dictionary can hold multiple values per key....
11
13174
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 error CVT1100: duplicate resource. type:ICON, name:1, language:0x0409". But the resource ID is unique and only located in the new Resource1.h and MyResourceFile.rc files. Any ideas why this error is occuring? To generate the issue real quick, you...
3
1327
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
802
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, but still preserve the data hiding issue. How do I construct a macro to access static {local} variables in a module? The macro will be in a header file that is included in several modules. Example: timer_isr.c
1
1510
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 the modules in Data Access layer) After that what i will do is add the reference of Facade layer , Business Object Layer and Data Access layer in the Web Project. By doing this actually i am adding the three dll's reference in web project. ...
13
2374
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 (replacement for PHP's include() statement). My problem is, in PHP if you open a database connection it's always in scope for the duration of the script. Even if you use an abstraction layer ($db = DB::connect(...)) you can `global $db` and bring...
47
4989
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 and my c++ code should call c code.but when i tried to call a function in c code externing that function in my c++ code, i am getting unresolved external symbol error. Whatever i try its giving more and more errrors...so is it possible to merge 2...
11
4683
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 declaration to foo.h and the function to foo.c. Then, on one compilation, I suddenly got a massive number of failures and warnings. Like "Redefinition errors. errors complaining about a variable not having been declared, when it was etc"
0
9603
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
10640
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...
0
10376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10387
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
10120
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
6881
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5689
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4332
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
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.