undefined reference to function?

Hi so I'm new to C++ and I wanted to try calling a function called "bark" in my main function defined in another .cpp file. I created a header file to prototype the bark function, but when I try to call the bark function in main i have 1 error saying "Undefined reference to bark() I have no idea what I'm doing wrong and I would like to learn how to fix this error.

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

using namespace std;

int main()
{
    bark();

    return 0;
}


Dog.cpp
1
2
3
4
5
6
7
8
9
10
#include <iostream>

using namespace std;

void bark() {

    cout << "Barks" << endl;

}
 



Dog.h
1
2
3
4
5
6
7
8
#ifndef DOG_H_INCLUDED
#define DOG_H_INCLUDED

void bark();


#endif // DOG_H_INCLUDED
Undefined reference means the compiler can't find the definition of the function. Make sure that you are compiling and linking both main.cpp and Dog.cpp (if you use an IDE you should have all files in the same project).
How are you compiling dog.cpp and main.cpp?

The undefined reference is telling you that the linker doesn't know how to link in dog.obj.

If you're using an IDE and if you've added both dog.cpp and main.,cpp to you project, dog.obj should be added to the link step automatically.



I'm only compiling main.

And in my IDE both dog.cpp and main.cpp are included in the project

https://gyazo.com/a6978a9ab437bb01f8b20433d643dd54
Right click on Dog.cpp → Properties → Build → make sure that both compile and link boxes are checked.

Then press build and run button
https://gyazo.com/bc07641ad938f1368967c7dbfab9cdf3

both are checked. I am going to try reinstalling my IDE.
"Belongs in target" should be checked too
Wow I'm so dumb haha. Thank you MiiniPaa you solved my problem!
Topic archived. No new replies allowed.