#### Archived

This topic is now archived and is closed to further replies.

# How to create a universal function in VC7

This topic is 5308 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have now a header file that contains a function I intend to use universally in my project. All I am supposed to do is to include this header file in my intend-to-use cpp file. But when I have two cpp files sharing this function, the compiler complains that this function has been repeatedly defined. Can anyone give me a hint how to solve this problem, or can I achieve this uniersal sharing another way around it?

##### Share on other sites
you could try using #ifndef and #endif commands ^^ i thinks in ur header.. eg

#endif
im not sure if this is how but its a start?

##### Share on other sites
You want to go

//Header stuff goes here
#endif

-=|Mr.Oreo|=-
Code Monkey, Serpent Engine

##### Share on other sites
The first two replys are wrong.

you want to either mark the function inline, or put it in a .cpp file and only leave the prototype in the header.

##### Share on other sites
You''ll want to do something like this:

extern int MyGlobalFunction( /*some args*/ );

#endif

and then in one Cpp file implement the function:

int MyGlobalFunction( /*some args*/ )
{
//code goes here
}

##### Share on other sites
Here is a very evil solution

universal.cpp -->

static int MyUniversalFunction (int whatever){  return 0;}

and in every file,

#include "universal.cpp"

[edited by - foofightr on August 13, 2003 4:36:42 AM]

##### Share on other sites
foofighter j00 are T3H EVIL!!

The solution to this problem (as already state) is to declare the funciton in a header file (that gets included everywhere) and define it in a cpp file (that never gets included, but still includes the header).

ToohrVyk

##### Share on other sites
You can also place the ''#pragma once'' pre-processor statement in your header before any declarations, etc...

And also, as people have stated -- function declarations in the header, function definitions in the .cpp

I used to like the #ifndef...#define method... but I find myself liking #pragma once because you don''t need to close the #ifndef at the bottom so its one less thing to worry about...

Ravyne, NYN Interactive Entertainment
[My Site][My School][My Group]

##### Share on other sites
Note:

#pragma once - MSVC specific syntax

Kory

##### Share on other sites
Yeah, I don''t see why people use vendor-specific extensions when there exists a portable solution.

##### Share on other sites
quote:
Original post by 00702000744
I have now a header file that contains a function I intend to use universally in my project. All I am supposed to do is to include this header file in my intend-to-use cpp file. But when I have two cpp files sharing this function, the compiler complains that this function has been repeatedly defined. Can anyone give me a hint how to solve this problem, or can I achieve this uniersal sharing another way around it?

Only include the function prototype in the .h file and include this .h file in both the main file and the file in which the function is defined.

Beginners and experts will find answers here.

##### Share on other sites
quote:
Original post by antareus
Yeah, I don''t see why people use vendor-specific extensions when there exists a portable solution.

''cos #prgama once is shorter than #ifndef _SOMETHING_ #define _SOMETHING_ #endif and if you''re only planning on using VC++ anyway it doesn''t matter if it isn''t going to work on other compilers.

##### Share on other sites
There was a push to make pragma once standard... I think it died out though.

I'll tell you what each different solution does, and why the #ifndef is not a complete solution for this problem.

First, only .cpp files are compiled, and thus only headers included from a .cpp file are compiled; this chain of files is called a translation unit . Essentially, each .cpp in your program creates and/or belongs to a translation unit (when it is compiled).

Doing what you are doing will manifest trouble in two different ways, but for essentially the same reason; duplicate definition. If you include the same header multiple times in the same translation unit the compiler thinks you declared two different functions, but accidentally gave them the same name, and emits an error. The #ifndef method will fix this problem (because it make s certain a header is only included once in that translation unit).

However it's not completely solved yet, because you could include that function in two different translation units. Everything will compile, however when the compiled code is linked together, the linker will find duplicated function(s) and emit an error.

To solve this problem, you somehow need to tell the compiler it's the same function.

The traditional method is to declare the function 'extern' (i.e. global) and separate the function declaration (goes in the header file,.hpp, e.g. extern int MyFunc(int); ) from the definition (goes in the source file, .cpp, e.g. int MyFunc(int i) {return i+1;} ). Sometimes that's alot of trouble for one simple function, and templates cannot be compiled in source files.

The remaining two options are to declare the function either inline or static.
Declaring the function inline has the compiler to do two things; ignore duplicate definitions that are the same, and try to avoid actually making a function call - instead it will 'inline' the code right where it is invoked (kinda like a #define macro).
Declaring the function static tells the compiler that you what this function to exist on-it's-own in each translation unit. You effectively get a separate copy of the function in each translation unit. If you use static variables inside of the function, and declare the function static, each translation unit will get it's own static variables. This is different than declaring it extern (or inline), then all translation units would share the same static variables.

- Magmai Kai Holmlor

"No, his mind is not for rent to any god nor government" - Rush, Tom Sawyer

[Look for information | GDNet Start Here | GDNet Search Tool | GDNet FAQ | MSDN RTF[L] | SGI STL Docs | STFW | Asking Smart Questions ]

[Free C++ Libraries | Boost | ACE | Loki | MTL | Blitz++ | wxWindows| Spirit(xBNF)]
[Free C Libraries | zlib ]

[edited by - Magmai Kai Holmlor on August 13, 2003 10:54:16 PM]

##### Share on other sites
Magmai, a function declaration is implicitly extern, so it''s redundant to use that keyword in that case. You must be thinking of variables...

##### Share on other sites
It did seem odd when I wrote that example. Still works though, right?

##### Share on other sites
Yeah, but it''s redundant.