Undefined reference error Code::Blocks

Pages: 12
Hello, I received undefined reference error at a project with three files: SUM.h, SUM.cpp, main.cpp. Give me "undefined reference to 'SUM::Sumarise()' " error. What can I do?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44


[code]
// file SUM.h
class SUM
{
private:
    int a;
    int b;

public:
    int Get_first() {return a;}
    int Get_second() {return b;}
    void Set_first(int x) {a=x;}
    void Set_second(int y) {b=y;}
    int Summarise();
};



//file SUM.cpp

#include "SUM.h"

int SUM::Summarise() {return a+b;}


//file main.cpp

#include <iostream>
#include "SUM.h"

using namespace std;

int main()
{
    SUM fx;
    fx.Set_first(15);
    fx.Set_second(3);
    fx.Summarise();
    cout<<fx.Summarise();
    return 0;
}
Last edited on
Are you sure SUM.cpp has actually been added to your project?

Hello ghegheg,

Post the whole error message. No what you think it means.

There is to much missing information.

Andy
I would send a picture, but I do not how do this, and I will copy it here.

Project name: Gabi
Sources - SUM.cpp
-main.cpp
Headers -SUM.h

In build messages:

==Build: Debug in Gabi(compiler:GNU GCC Compiler)
obj\Debug\main.o In function 'main':
D:\C++\main.cpp --- 11 --- undefined reference to 'SUM::Summarise()'
D:\C++\main.cpp --- 12 --- undefined reference to 'SUM::Summarise()'
error: ld returned 1 exit status
==Build failed: 3 error(s). o warning(s), (0 min. 1sec)


Line 11 is "fx.Summarise();".
Last edited on
Try cleaning your project: Build -> Clean

then try rebuilding: Build -> Rebuild

Sometimes IDEs get a bit confused and need a kick in the 'nads to work correctly.
I tried it but It do not work, I have the same error. Is it possible be a bad of code::blocks? I will uninstall and install again code::blocks. I do not know what to do.
Is it possible be a bad of code::blocks?

Possible maybe, probable no. It is more likely something that you have failed to do.

For example are you using a project?

If so did you properly add the source files to your project?

Try to copy your function into the main.cpp file and see what happens.
You use header guards, don’t you?
I can’t replicate the error:
Sum.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef CLASS_SUM_HEADER
#define CLASS_SUM_HEADER


class Sum {
private:
    int a {};
    int b {};

public:
    int get_first() const;
    int get_second() const;
    void set_first(int val);
    void set_second(int val);
    int summarise() const;
};


#endif // CLASS_SUM_HEADER 


Sum.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include "Sum.hpp"


int Sum::get_first() const
{
    return a;
}


int Sum::get_second() const
{
    return b;
}


void Sum::set_first(int val)
{
    a = val;
}

void Sum::set_second(int val)
{
    b = val;
}


int Sum::summarise() const
{
    return a + b;
}


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
#include "Sum.hpp"
#include <iostream>


int main()
{
    Sum fx;
    fx.set_first(15);
    fx.set_second(3);
    std::cout << fx.summarise() << '\n';
    return 0;
}


Output:
18

I reinstall code::blocks again and I have same error. What can I do?
The file SUM.h was created File/New/File/C/C++header/ I named the file, clicked ... and indicated the project director.
The file SUM.cpp was created File/New/File/C/C++source/ I named the file, clicked ... and indicated the project director.
What is it wrong?
What is the name of your project?

I used a project. Gabi is name of project.
I did not understand Enoizat's post: I know that a header file has .h extension, why has he written "Sum.hpp" name of header file? I have clicked File/New/File/ C/C++header and the program has created a file with .h extension.

Unfortunately I do not know how can I post some picture in this forum?
Using a .h extension for a header is fine.

What operating system are you using?

Please post a copy of the contents of the Gabi.gdb file.

Also please cut and paste the errors from your IDE exactly as they appear in your development environment.
Hello ghegheg,

Sorry for the delay. Was not feeling well earlier.

I put your projects in CB, but left out the "Sum.cpp" as part of the project which reproduced your error. When I figured out how to add the file to the project, (not familiar with CB), the error went away.

I do not know how long you have been using CB, so I will just say it. On the left in the window marked "Management", under the heading of "source" you should see the files "main.cpp" and "Sum.cpp". If not then you will need to add the "Sum.cpp" file to the project.

If that does not work try creating a new project with a new name.

For what its worth I created the program in VS2017. First with all the files as part of the project and it compiled fine with no errors. When I removed the "Sum.cpp" file from the project I received VS's version of the same error.

This tells me that "Sum.cpp" is not being compiled into the project and at link time the call to "fx.Summarise()" is not being found.

Also in "main" you have the line fx.Summarise(); before the "cout" statement. This function call does nothing. The one in the "cout" statement is all you need because it does use the returned value of the function.

Hope that helps,

Andy
for jlb:

I use Windows 10 with legal license.

The Gabi.gdb shows us nothing, when I click it Code::Blocks starts with my project.

I cut exactly error:

||=== Build: Debug in Gabi (compiler: GNU GCC Compiler) ===|
obj\Debug\main.o||In function `main':|
D:\C++\Gabi\main.cpp|11|undefined reference to `SUM::Summarise()'|
D:\C++\Gabi\main.cpp|12|undefined reference to `SUM::Summarise()'|
||error: ld returned 1 exit status|
||=== Build failed: 3 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
The Gabi.gdb shows us nothing,

This probably means that you didn't create a project. So from the "File" menu select "New" then "Project...". Then select "Console application" from the selection window, then select a C++ project, lastly enter the project name and select the directory where you want to create this project.

Hello ghegheg,

I did not understand Enoizat's post: I know that a header file has .h extension, why has he written "Sum.hpp" name of header file?

The ".h" extension are used with C header files and tend to make one think of C header files when used.

I believe that when C++ was created the ".cpp" extension was used to show the difference between the Cs' ".c" extension and C++s' ".cpp" extension. But history was never my strong subject, so I may have this a bit wrong. I hope the idea of the concept is understandable.

IMHO I like using the ".hpp" extension when working with C++ programs, but as jlb said using either is fine.

Unfortunately I do not know how can I post some picture in this forum?
I do not know of any way to do this either, but I have seen people use outside sources like https://postimages.org/ and then put the link in the message. There are others, but I do not have links for them yet.

Just in case: when you post a link there should be nothing or a space before the "http" and either a space or "Enter" after the last character.

And for code that may be to large to fit in a message there are also:

https://pastebin.com/
https://github.com/
https://repl.it/

And some others that I do not have links for.

Hope that helps,

Andy
for Andy

It is very curiously that It worked on your CB. Did you use only VS2017?

They appear on the left, seam ok.

Source - SUM.cpp
-main.cpp
Header -SUM.h

I have tried this code with more file names but I have received the same errors. I'll try VS2017
Last edited on
Hello ghegheg,

Well I started with VS2017 because that is what I use the most. Today is the first time I really used CB and some of it is new to me.

I would refer to jlb's last post for directions as I am not quite sure how I did everything yet.

You might still try closing down CB with the problem and open a new instance of the program and start over. This at least leaves your old files to copy and paste into a new project with a new name.

As mentioned I have seen a problem with VS where it would not see or accept the changes and use old files to build a project. I have even had to go find the ".exe" file and manually delete it to make a rebuild work. So sometimes it is easier to start over than trying to fix your problem until you have gained more experience with the IDE.

Short of giving someone remote access to your computer it is a bit of a guessing game to figure out what you did wrong.

It sounds like what jlb said:
This probably means that you didn't create a project.
and I feel that this is a strong possibility.

Oh BTW in Enoizat's post the header guards are worth learning and understanding:
1
2
3
4
5
6
#ifndef CLASS_SUM_HEADER
#define CLASS_SUM_HEADER

// Your code here.

#endif // CLASS_SUM_HEADER  

The part in all caps you can call whatever you like. It helps when it is something meaningful that goes along with the code that follows.

Hope that helps,

Andy
I found it!!!

Indeed, the SUM.cpp did not belongs to project.

I right clicked on name of project, clicked "properties" and clicked tag "Build targets", and pointed SUM.cpp. Now, my compiler see this file end the error has disappear!!!

I'll learn Code::Bloks to avoid these mistakes.

Thank everyone!

Pages: 12