This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 years ago.
The project has 2 classes - Tree and TreeTest class
The TreeTest class tests the functions of Tree class.
TreeTest.h
#ifndef TREETEST_H
#define TREETEST_H
class TreeTest
{
public:
TreeTest();
virtual ~TreeTest();
void InitTreeFunctionTest();
protected:
private:
};
#endif // TREETEST_H
TreeTest.cpp
#include "TreeTest.h"
#include "Tree.h"
#include <cstring>
#include <iostream>
using namespace std;
void TreeTest::InitTreeFunctionTest()
{
//code goes here
}
Main.cpp
#include <iostream>
#include <Tree.h>
#include <TreeTest.h>
using namespace std;
int main(int argc, char* argv[])
{
TreeTest* tt;
tt->InitTreeFunctionTest();
}
But it gives the following error when I compile using
g++ -fprofile-arcs -ftest-coverage main.cpp -I<full path to library> -o test
undefined reference to `TreeTest::InitTreeFunctionTest()'
Can anyone please help me find the error?
Thanks
You have not compiled TreeTest.cpp
Try adding it to your command line:
g++ -fprofile-arcs -ftest-coverage TreeTest.cpp main.cpp -I<full path to headers> -o test
edit:
You will also need to provide the definitions of the constructor and destructor in TestTree.cpp
The other solution is to inline the function definition in the header. But, you probably want to compile it unless the functions are trivial.
Please use #include "TreeTest.h", not #include <TreeTest.h>. If you want to use #include <TreeTest.h>, first, you must make your TreeTest.cpp into a static library. BTW, you can search for the difference between #include "" and #include <>. I hope this can help you.
Related
This question already has answers here:
Code-runner configuration for running multiple cpp classes in vscode
(1 answer)
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 1 year ago.
I've been following tutorial on how to create class using header files and came to a problem even if I did everything like in tutorial. I got Cat.h, Cat.cpp and main.cpp files. All of them are in the same folder.
Cat.h:
#ifndef CAT_H_
#define CAT_H_
class Cat
{
public:
void speak();
};
#endif
Cat.cpp:
#include <iostream>
#include "Cat.h"
using namespace std;
void Cat::speak()
{
cout << "Meeeow!" << endl;
}
main.cpp:
#include <iostream>
#include "Cat.h"
using namespace std;
int main()
{
Cat jim;
jim.speak();
return 0;
}
When i run the program I got error: "undefined reference to `Cat::speak()'". The problem is solved when i add line #include "Cat.cpp" to main.cpp but I dont think thats a way to go and tutorial was done without that.
Solved. For anyone having the same problem using VS Code with Code Runner extension, I found the solution in different thread:
Code-runner configuration for running multiple cpp classes in vscode
I already searched and found a solution for this problem but i find this a little bit strange. Anyway my problem is this:
Personal.h
class Personal
{
public:
Personal();
int money;
~Personal();
}
Personal.cpp
#include "Personal.h"
Personal::Personal()
{
money = 1800;
}
Personal::~Personal(){};
Now i want to compile in main
main.cpp
#include "Personal.h"
#include <iostream>
#include <vector>
int main()
{
std::vector<Personal> test(100);
}
When I write: g++ -Wall main.cpp -o main it gives me :
undefine reference to Personal::Personal()
undefine reference to Personal::~Personal()
The solution:
g++ -Wall Personal.cpp main.cpp -o main
Why do i need compile the Personal.cpp too?
Or the other main version is to include instead of "Personal.h", "Personal.cpp"
main.cpp
#include "Personal.cpp"
#include <iostream>
#include <vector>
Then the normal g++ -Wall main.cpp -o main works
Can someone help me?
Why do i need compile the Personal.cpp too?
Because you use functions that are defined in that file. In particular, you use the functions Personal::Personal and Personal::~Personal.
Can someone help me?
Make sure that all functions (that are odr-used) are defined in exactly one (or in all files, in case of inline functions) of the source files that you compile and link together.
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 6 years ago.
well I want to understand linking with headers and other .cpp (functions for example) so my quastion is why I get "undefined reference to 'afis(). There are sample example and I want to clarify this. Also sorry for my bad english :D.
There is main:
#include <iostream>
#include "functions.h"
using namespace std;
int main()
{
afis();
return 0;
}
There is an function named function.cpp:
#include <iostream>
using namespace std;
void afis(){
cout <<"yehe";
}
And there is the header :
#ifndef FUNCTIONS_H_INCLUDED
#define FUNCTIONS_H_INCLUDED
void afis();
#endif // FUNCTIONS_H_INCLUDED
While the C++ compiler automatically "pulls in" referenced header files, it can't do that for the actual .cpp code files.
Instead of calling
CXX/clang++/g++ main.cpp -o hello
you need to manually include all relevant code files:
CXX/clang++/g++ main.cpp functions.cpp -o hello
I wanted to learn using header files. and I got an error. here is my code:
printmyname.h:
void printMyName();
printmyname.cpp:
#include "printmyname.h"
void printMyName() {
cout << "omer";
}
try.cpp (main file):
#include <iostream>
#include "printmyname.h"
using namespace std;
int main() {
printMyName();
return 0;
}
Here is the error:
undefined reference to `printMyName()`
What's is the problem?
Undefine reference has nothing to do with your header file in this case. It means the linker cannot find the implementation of printMyName which is in printmyname.cpp. If you are using g++, you should try:
g++ try.cpp printmyname.cpp -o yourBinaryName
If you are using a makefile, you should add dependency(printmyname.cpp) correctly for try.cpp.
Edit:
As #zmo suggest in his comment:
you can also do it through a two times compilation (more suitable with Makefiles):
g++ -c printmyname.cpp
g++ try.cpp printmyname.o -o yourBinaryName
If you are using Windows, you need to add the printmyname.cpp to your project too.
Consider adding an include guard to your header
#ifndef PRINTMYNAME_INCLUDED
#define PRINTMYNAME_INCLUDED
void printMyName();
#endif
You will also need to move the #include <iostream> and using namespace std; from the try.cpp to the printmyname.cpp file.
You need to add code/definition in printMyName.cpp inside printMyName.h only.
void printMyName();
{
cout << "omer";
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is an undefined reference/unresolved external symbol error and how do I fix it?
I have main.cpp:
#include "censorship_dec.h"
using namespace std;
int main () {
censorship();
return 0;
}
this is my censorship_dec.h:
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
void censorship();
this is my censorship_mng.cpp:
#include "censorship_dec.h"
using namespace std;
void censorship()
{
cout << "bla bla bla" << endl;
}
I tried to run these files in SSH (Linux), so I wrote: make main, but I got:
g++ main.cpp -o main
/tmp/ccULJJMO.o: In function `main':
main.cpp:(.text+0x71): undefined reference to `censorship()'
collect2: ld returned 1 exit status
make: *** [main] Error 1
please help!
You have to specify the file where censorship is defined.
g++ main.cpp censorship_mng.cpp -o main
You must add censorship_mng.cpp in your compilation command:
g++ main.cpp censorship_mng.cpp -o main
Another solution (if you really don't want change your compile command) is making void censorship(); to a inline function and move it from .cpp to .h.
censorship_dec.h:
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
inline void censorship()
{
// your code
}
And remove void censorship() from censorship_mng.cpp file.
once your project starts using several source-files to be compiled into a single binary, manual compilations become tedious.
this is usually the time when you start using a build-system, such as a Makefile
a very simple Makefile that uses default build-rules could look like
default: main
# these flags are here only for illustration purposes
CPPFLAGS=-I/usr/include
CFLAGS=-g -O3
CXXFLAGS=-g -O3
LDFLAGS=-lm
# objects (.o files) will be compiled automatically from matching .c and .cpp files
OBJECTS=bar.o bla.o foo.o main.o
# application "main" build-depends on all the objects (and linksthem together)
main: $(OBJECTS)