C ++, including the ".h" file, confusion with duplication of functions

I am currently writing a program and cannot understand why I received an error message (note: I already fixed it, I wonder WHY there was an error, and what it means to include .h files).

Basically, my program was structured as follows:

In the current file I'm working with, I will name Current.cc (which is an implementation of Current.h ).

Current.cc includes a header file called CalledByCurrent.h (which has an associated implementation called CalledByCurrent.cc ). CalledByCurrent.h contains the class definition.

In CalledByCurrent.cc , called thisFunction() , a non-classical function was defined. thisFunction() not declared in CalledByCurrent.h since it was not actually a member function of the class (just a small helper function). In Current.cc I needed to use this function, so I just overridden thisFunction() at the top of Current.cc . However, when I did this, I got an error saying that the function is duplicated. Why is this when myFunction() is not even declared in CalledByCurrent.h ?

So I just deleted the function from Current.cc , now assuming that Current.cc had access to thisFunction() from CalledByCurrent.cc . However, when I did this, I found that Current.cc did not know what function I was talking about. What the heck? Then I copied the function definition for thisFunction() to the top of my CalledByCurrent.h file, and this solved the problem. Could you help me understand this behavior? In particular, why did he think that there was a duplicate, but he did not know how to use the original?

ps - I apologize for how confused this post is. Please let me know if there is anything that I can clarify.

+4
source share
6 answers

You cannot have two global functions with the same name (even in two different translation units). To avoid a linker error, define the function as static so that it is not visible outside the translation unit.

EDIT

You can use this function in another .cpp file using the extern keyword. See this example:

 //Test.cpp void myfunc() { } //Main.cpp extern void myfunc(); int main() { myfunc(); } 

It will select myfunc() defined in test.cpp .

+3
source

You get several definitions from the linker - it sees two functions with the same name and complains. For instance:

 // a.cpp void f() {} // b.cpp void f() {} 

then

 g++ a.cpp b.cpp 

gives:

 C:\Users\neilb\Temp\ccZU9pkv.o:b.cpp:(.text+0x0): multiple definition of `f()' 

How to do this, either put the definition in only one .cpp file, or declare one or both functions static:

 // b.cpp static void f() {} 
+4
source

The header file inclusion mechanism should be tolerant of duplicate header file inclusions.

+1
source

This is because whenever you simply declare a function that it looked at in the extern (global) scope (whether you declare it in the header file or not). The linker will have several implementations for the same function signature.

If these functions are truly helper functions, declare them as:

 static void thisFunction(); 

In another way, if you use the same function as the helper, simply declare it in a common header file, say:

 //CalledByCurrent.h (is included in both .cc files) void thisFunction(); 

And implement thisFunction () in any of the .cc files . This should solve the problem correctly.

+1
source

Here are some ideas:

  • You did not enter a header in the protection file in your header file. If it is turned on twice, you may get this error.
  • The function prototype (above) does not match its signature 100%.
  • You put the body of the function in the header file.
  • You have two functions of the same signature in two different source files, but they are not marked static .

If you use gcc (you did not say which compiler you are using), you can use the -E switch to view the output of the preprocessor. This includes an extension of all #define and including all #include s.

Each time something expands, it tells you which file and line it was in. Using this, you can determine where thisFunction() .

0
source

There are 2 different errors coming from two different assembly phases.

In the first case, when you have a duplicate, COMPILER is happy, but LINKER complains, because when it selects all the function definitions in the different source files that they notice, they are called the same. As other answers claim, you can use a static keyword or use a generic definition.

In the second case, when you see that your function is not declared in this area, this is because COMPILER complains, because each file must know what functions it can use.

Compilation takes place before linking, so COMPILER cannot know in advance whether LINKER will find the corresponding function, so you use the declarations to notify COMPILER that a definition will be found later.

As you can see, your 2 errors are not inconsistent, they are the result of two separate processes in the assembly that have a specific order.

0
source

All Articles