"Undefined Reference" linker errors Topic is solved

Do you have a question about makefiles, a compiler or IDE you are using and need to know how to set it up for wxWidgets or why it doesn't compile but other IDE's do ? Post your questions here.
Post Reply
User avatar
ColleenKobe
Earned some good credits
Earned some good credits
Posts: 109
Joined: Mon Aug 31, 2015 3:47 pm

"Undefined Reference" linker errors

Post by ColleenKobe »

I'm having a problem that I don't know how to fix. wxWidgets compiles all my source files without any problem, but then I get a list of error messages saying "undefined reference to 'x'," where 'x' is the name of a user-defined procedure or function in one of my *.h files. See the attached print-screen.
undefined references to procedures listed in an h file
undefined references to procedures listed in an h file
Overview. I have five user-defined classes in this project. One displays a wxFrame; three display a wxDialog; and the last one, which is the one giving me troubles, does not display a page at all.

The troublesome class is called Global_Data_Class. It contains a storehouse of values that can be "Set" or "Get" from any of the other classes. Any class (file) calling those procedures or functions simply adds the
#include "Global_Data.h"
compiler command at the top of the file. All of the source files compile with zero errors.

All the *.cpp and *.h and the make files reside in the same directory on my computer, and the linker finds all of those files.

To try to solve this problem, I've clicked Build, Clean Workspace, and then Build, Rebuild Workspace. Fail. I've tried switching to wxCrafter and clicking "Generate Code," and then cleaning and rebuilding again. Fail.
I've also searched the wxWidgets forum for "undefined reference," but the hits I got were mainly for system-type missing files, not user-defined procedures.

I'm open to suggestions for places to look to fix this problem.

Thank you in advance!

Software Versions
CodeLite 10.0.4
gcc 4.9.3
tdm-gcc 5.1.0.2
Windows 10 Pro, 64-bit
wxWidgets 3.1.0
wxCrafter 2.6

Target platform 32-bit
Target build debug
User avatar
doublemax
Moderator
Moderator
Posts: 19103
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: "Undefined Reference" linker errors

Post by doublemax »

My best guess would be that all those methods are declared (in the header file), but they have no body. Are they defined in the .CPP file?
Use the source, Luke!
User avatar
ColleenKobe
Earned some good credits
Earned some good credits
Posts: 109
Joined: Mon Aug 31, 2015 3:47 pm

Re: "Undefined Reference" linker errors

Post by ColleenKobe »

doublemax wrote:My best guess would be that all those methods are declared (in the header file), but they have no body. Are they defined in the .CPP file?
Thank you for replying, doublemax.

Yes, the procedures and functions are defined in both the .h file and the .CPP body. Otherwise, I'm pretty sure the code would have generated compiler errors. I've attached Global_Data.h and Global_Data.cpp. They're pretty long, and I don't expect you to scrutinize them. But maybe something will be obvious to you that isn't obvious to me.
Global_Data.h
Global_Data.h
(18.91 KiB) Downloaded 143 times
Global_Data.cpp
Global_Data.cpp
(22.45 KiB) Downloaded 132 times
User avatar
doublemax
Moderator
Moderator
Posts: 19103
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: "Undefined Reference" linker errors

Post by doublemax »

Code: Select all

void Initialize (Global_Data_Class* GM_Ptr)
{
    Local_GM_Ptr    = GM_Ptr;           // Get a copy of the pointer to the
                                        // global data class.  Use the global
                                        // data pointer throughout this class to
                                        // update global memory values.
Should be:

Code: Select all

void Global_Data_Class::Initialize (Global_Data_Class* GM_Ptr)
{
    Local_GM_Ptr    = GM_Ptr;           // Get a copy of the pointer to the
                                        // global data class.  Use the global
                                        // data pointer throughout this class to
                                        // update global memory values.
Same for the other methods.

Basically you just created some global functions instead of class methods.

The syntax without the "Global_Data_Class::" only works inside the actual class declaration.

You only didn't get any compiler errors, because you were not accessing any member variables, just global variables.
Use the source, Luke!
User avatar
ColleenKobe
Earned some good credits
Earned some good credits
Posts: 109
Joined: Mon Aug 31, 2015 3:47 pm

Re: "Undefined Reference" linker errors

Post by ColleenKobe »

doublemax wrote:

Code: Select all

void Initialize (Global_Data_Class* GM_Ptr)
{
    Local_GM_Ptr    = GM_Ptr;           // Get a copy of the pointer to the
                                        // global data class.  Use the global
                                        // data pointer throughout this class to
                                        // update global memory values.
Should be:

Code: Select all

void Global_Data_Class::Initialize (Global_Data_Class* GM_Ptr)
{
    Local_GM_Ptr    = GM_Ptr;           // Get a copy of the pointer to the
                                        // global data class.  Use the global
                                        // data pointer throughout this class to
                                        // update global memory values.
Same for the other methods.

Basically you just created some global functions instead of class methods.

The syntax without the "Global_Data_Class::" only works inside the actual class declaration.

You only didn't get any compiler errors, because you were not accessing any member variables, just global variables.
Thank you for your quick response. I figured something obvious would be wrong.

Actually, I did start by adding the

Code: Select all

Global_Data_Class::
text preceding each procedure name in the cpp file. But then, and now, I get the following compiler error messages when I do that:

Code: Select all

error: prototype for 'void Global_Data_Class::Initialize(Global_Data_Class*) does not match any in class 'Global_Data_Class'
error: candidate is: virtual void Global_Data_Class::Initialize()
error: 'Initialize' was not declared in this scope
Since I got compiler errors, I figured that was the wrong way to declare the...method?. So I left off the Global_Data_Class:: text. And now compiles but it doesn't link, so that's a fail. :(

How do I modify the declaration(s) in the h file so that I eliminate those errors?
User avatar
doublemax
Moderator
Moderator
Posts: 19103
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: "Undefined Reference" linker errors

Post by doublemax »

In the declaration "Initialize" doesn't take a parameter:

Code: Select all

virtual void        Initialize (void);
They must be a perfect match, so you have to change one of them.
Use the source, Luke!
User avatar
ColleenKobe
Earned some good credits
Earned some good credits
Posts: 109
Joined: Mon Aug 31, 2015 3:47 pm

Re: "Undefined Reference" linker errors

Post by ColleenKobe »

doublemax, I do understand that the declarations in the h and cpp files must be identical. I changed the declarations in the cpp file, made a temp copy of the file, and ran a macro to strip out all the lines in the temp file that were not procedure/function declarations. Then I appended semicolons at the end of each declaration, deleted the original procedure/function declarations in the h file, and merged the new procedure and function declarations into the h file.

So the declarations in both places are identical, EXCEPT that there may be extra spaces between words in the h file that don't appear in the cpp file.

Now I'm getting "extra qualification" errors when I compile Global_Data.h. See the screen dump.
Extra qualification
Extra qualification
I must be missing something else. I did take out the "virtual" command. Do I need to add it back in?

Thank you for your patience.
User avatar
doublemax
Moderator
Moderator
Posts: 19103
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: "Undefined Reference" linker errors

Post by doublemax »

Don't use the "Global_Data_Class::" in the header file. These lines are inside the class declaration already.
I did take out the "virtual" command. Do I need to add it back in?
The "virtual" is not related to any of the errors. It just means that these methods can be overridden by classes deriving from "Global_Data_Class". But looking at the purpose of the class, i don't think that will happen. So you can remove the "virtual" everywhere if you want. But it doesn't hurt either.
Attachments
Global_Data.h
(18.93 KiB) Downloaded 133 times
Global_Data.cpp
(24.41 KiB) Downloaded 127 times
Use the source, Luke!
User avatar
ColleenKobe
Earned some good credits
Earned some good credits
Posts: 109
Joined: Mon Aug 31, 2015 3:47 pm

Re: "Undefined Reference" linker errors

Post by ColleenKobe »

That worked, doublemax! Thank you!!!
Post Reply