Archived

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

00702000744

How to create a universal function in VC7

Recommended Posts

00702000744    122
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 this post


Link to post
Share on other sites
MrOreo    124
You want to go

#ifndef __MY_HEADER_FILE
#define __MY_HEADER_FILE
//Header stuff goes here
#endif

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

Share this post


Link to post
Share on other sites
C-Junkie    1099
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 this post


Link to post
Share on other sites
mhamlin    520
You''ll want to do something like this:

in your header:

#ifndef _MY_GLOBAL_HEADER_
#define _MY_GLOBAL_HEADER_

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

#endif

and then in one Cpp file implement the function:

#include "my_global_header.h"

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

Share this post


Link to post
Share on other sites
foofightr    130
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 this post


Link to post
Share on other sites
ToohrVyk    1595
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 this post


Link to post
Share on other sites
Ravyne    14300
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 this post


Link to post
Share on other sites
Mathematix    259
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 this post


Link to post
Share on other sites
Naku    151
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 this post


Link to post
Share on other sites
Shannon Barber    1681
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 this post


Link to post
Share on other sites