![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Feb 2006
Posts: 36
Rep Power: 0
![]() |
Function pointer?
Is it possible to define function pointer globally?
I'm getting strange errors when I do that, but defining it in function body works fine. void (*show) (String msg, int level, Type tip); in global scope gives me (path broken to multiple lines): multiple definition of `show' parse.o: c:/msys/mingw/bin/../ lib/gcc/mingw32/3.4.2/../../../../ include/c++/3.4.2/bits/stl_algobase.h:(.bss+0x0): first defined here Bye |
|
|
|
|
|
#2 |
|
Unverified User
Join Date: Dec 2006
Location: Bristol UK
Posts: 19
Rep Power: 0
![]() |
Well if you read the error message properly and try to do some of your own thinking it will help you.
What do you think the first line, multiple definition of `show' ", and the last line, "first defined here" means? When you have globals (which are a bad idea) you run the risk of have to Identifiers with the same name. This if probably what is happening here. The name show is used somewhere in the the depths of the C++ library you are using. To solve it you will have to change your identifier name to something else or not have it as a global. |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Feb 2006
Posts: 36
Rep Power: 0
![]() |
This was the case of multiple include problem it seems...
Here is the problem: Function pointer is in header file which gets included by multiple source files and then in linking process I get "multiple definition" error. How do I put the definition of function pointer in header file without geting the linker error? Bye |
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,206
Rep Power: 5
![]() |
You need to understand the distinction between a definition and a declaration. The following may seem a bit long, but it'll walk you through the things you need to know to fix your problem.
I'll use a global variable of type int to illustrate, but the idea's exactly the same if the global is of a different type (eg a pointer to a function). Within a complete program (i.e. what is assembled by the linker, if it succeeds) there can only be one instance of a global. The way you ensure there is an instance is to define it. In a single source file, this is easy; int global_var;
void function()
{
// do things with global_var
}
int main()
{
// do things with global_var
function(); // will do more things with global_var
// do even more things with global_var
}But what happens if we break the project up into parts. First a header file declaring our global; // header file named global.h
#ifndef GLOBAL_H_INCLUDED
#define GLOBAL_H_INCLUDED
int global_var;
int function(); // prototype for calling function()
#endif// global.cpp (assume cpp is an extension for C++ files recognised by our compiler)
#include "global.h"
void function()
{
// do things with global_var
}// main.cpp (assume cpp is an extension for C++ files recognised by our compiler)
#include "global.h"
int main()
{
// do things with global_var
function(); // will do more things with global_var
// do even more things with global
}The above will, typically, result in a multiple definition error such you have seen. The reason is that, when compiling each source file, the compiler sees the declaration; int global_var; This means we have to help the linker out so there is only one definition globally. That means we need to tell the compiler to do things differently. The formal description (in the C++ standard) is that we need to obey the "one definition rule" -- if anything is defined in more than one way within a complete program, we have undefined behaviour. In the above example, we have defined global_var twice, which intuitively might suggest we have violated something called the "one definition rule". To fix the problem, we come back to the notion that a definition is a particular type of declaration. So we need to introduce a declaration that does not make the compiler generate one definition for each source file. The header file is the thing used by both source files, so let's have a look there. // header file named global.h
int global_var;
int function(); // prototype for calling function() // header file named global.h
extern int global_var;
int function(); // prototype for calling function()So we have to introduce one, and only one definition somewhere so the linker sees a definition. If we put such a definition into a header file, we're back where we started. So, we have to put it into one (and only one) source file. It doesn't matter where we put it, as long as it is in a source file that has #include'd our global.h. I'll put it into main.cpp // main.cpp (assume cpp is an extension for C++ files recognised by our compiler)
#include "global.h"
int global_var; // our one and only definition
int main()
{
// do things with global_var
function(); // will do more things with global_var
// do even more things with global
}The only other comment is that the header file has what is called an include guard. It is a technique designed to prevent confusing the compiler should a header file be #included more than once. The name of the macro GLOBAL_H_INCLUDED needs to be chosen unique and distinct for every header file. |
|
|
|
|
|
#5 |
|
Programmer
Join Date: Feb 2006
Posts: 36
Rep Power: 0
![]() |
My reply is a bit shorter.
Thanks! |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Combining languages | titaniumdecoy | Other Programming Languages | 12 | Jul 13th, 2006 2:03 PM |
| Compiling Maverik 6.2 (from C) | megamind5005 | C | 16 | May 3rd, 2006 5:41 PM |
| libraries | matko | C | 1 | Jan 22nd, 2006 2:12 PM |
| Jackpot game | zorin | Visual Basic | 3 | Jun 10th, 2005 1:19 PM |
| Pointers in C (Part II) | Stack Overflow | C | 2 | Apr 29th, 2005 10:39 AM |