I am having a problem constructing an instance of a class in a larger program.
In the main function I have:
//main.cpp
#include "MyClass.h"
MyClass aMyClass;
//do stuff with MyClass
MyClass' header is only a constructor at this point and looks like this:
//MyClass.h
class MyClass {
public:
MyClass();
};
MyClass' source then looks like this, again only a constructor at this point:
//MyClass.cpp
#include "MyClass.h"
//--CONSTRUCTOR--//
MyClass::MyClass(){
cout << "constructing MyClass object..." << endl;
}
When I try to run my program I get this error:
undefined reference to `MyClass::MyClass()'
collect2: error: ld returned 1 exit status
Edit :
I'm compiling the program with the following commands using the command line:
g++ main.cpp -o mainProgram
Edit (solved) :
The compilation needs to include MyClass.cpp, the correct command is:
g++ man.cpp MyClass.cpp -o mainProgram
While I'm sure it's something small, where am I slipping up here? I've tried declaring the object earlier in the program, but that did not solve the problem and I got the same error.
Does anyone see a problem here?
you also need to compile MyClass.cpp - that's where the implementation is.
g++ main.cpp MyClass.cpp -o mainProgram
Related
I can't seem to get the errors to go away. The errors are below. I have looked on Google Search and still I can't figure it out. It is not like I am new to C++, but I have not fooled around with it in a while.
The weird thing is it worked with g++ on Windows...
Errors using:
g++ main.cpp
Output:
/tmp/ccJL2ZHE.o: In function main': \ main.cpp:(.text+0x11): undefined reference to Help::Help()'
main.cpp:(.text+0x1d): undefined reference to Help::sayName()' \ main.cpp:(.text+0x2e): undefined reference to Help::~Help()'
main.cpp:(.text+0x46): undefined reference to `Help::~Help()'
collect2: ld returned 1 exit status
File main.cpp
#include <iostream>
#include "Help.h"
using namespace std;
int main () {
Help h;
h.sayName();
// ***
// ***
// ***
return 0;
}
File Help.h
#ifndef HELP_H
#define HELP_H
class Help {
public:
Help();
~Help();
void sayName();
protected:
private:
};
#endif // HELP_H
File Help.cpp
#include <iostream>
#include "Help.h"
using namespace std;
Help::Help() { // Constructor
}
Help::~Help() { // Destructor
}
void Help::sayName() {
cout << " ***************" << endl;
cout << " ************************************" << endl;
cout << " ************" << endl;
cout << " *********************" << endl;
}
Use
g++ main.cpp Help.cpp
You have to tell the compiler all the files that you want it to compile, not just the first one.
You should add help.o to your g++ line:
g++ -c help.cpp -o help.o
g++ help.o main.cpp
By splitting it to two lines you can save compilation time (in case of larger projects), because you can compile help.cpp only when it was changed. make and Makefile used well will save you a lot of headache:
#Makefile
all: main
main: help main.cpp
g++ -o main help.o main.cpp
help: help.cpp
g++ -c -o help.o help.cpp
I had the same problem with my Linux Lubuntu distribution and it was creating the problem for my constructor and destructor. It was not recognizing them.
Actually, this goes off if you just compile all of the three files together. So, once you saved all your files, just do this:
g++ main.cpp Help.h Help.cpp
./a.out
./a.out is the executable file for the Linux. Sorry, but I don't know about the Windows. And your program would run smoothly.
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 8 years ago.
I have recently made a little example in order to practice separating C++ code in .h and .cpp files using Geany. The code compiles without issue, but the following error occurs when I build:
g++ -std=c++11 -Wall -o "parent1" "parent1.cpp" (in directory: C:\Program Files (x86)\Geany)
C:\Users\Sabine\AppData\Local\Temp\ccnonGdW.o:parent1.cpp:(.text+0x15): undefined reference to `grandparent::grandparent(int)'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/lib/libmingw32.a(main.o): In function `main':
i:\p\giaw\src\pkg\mingwrt-4.0.2-1-mingw32-src\bld/../mingwrt-4.0.2-1-mingw32-src/src/libcrt/crt/main.c:91: undefined reference to `WinMain#16'
collect2.exe: error: ld returned 1 exit status
Compilation failed.
The source files:
parent1.h:
#ifndef PARENT1_H
#define PARENT1_H
#include "grandparent.h"
class parent1 :public grandparent
{ private:
int i ;
public:
parent1(int p1a, int p1b, int p1c);
};
#endif
parent1.cpp:
#include "parent1.h"
#include "grandparent.h"
#include <iostream>
#include <string>
using namespace std;
parent1::parent1(int a, int b, int c)
: grandparent(a)
{}
Hi thanks for the quick replies.
Remove this line #include "grandparent.h" in parent1.cpp --- that didn`t work. ( Error: expected class-name before {-token )
grandparent.h looks like this:
#ifndef GRANDPARENT_H
#define GRANDPARENT_H
class grandparent
{ private:
int gx;
public:
grandparent(int gx);
};
#endif
You need to link with the grandparent object - grandparent.o or the library it belongs to.
UPDATE: In particular you need (assuming you've already compiled grandparent.cpp):
g++ -std=c++11 -Wall -o "parent1" "parent1.cpp" "grandparent.o"
I believe that
g++ -std=c++11 -Wall -o "parent1" "parent1.cpp" "grandparent.cpp"
will also work.
I moved from Windows to Ubuntu and I wanted to try some C++ programming on Ubuntu. So here is very simple code and very stupid error which I can't resolve:
horse.h
#ifndef _horse_
#define _horse_
class Horse{
int speed;
public:
void saySomething();
};
#endif
horse.cpp
#include "horse.h"
#include <iostream>
using namespace std;
void Horse::saySomething(){
cout << "iiiihaaaaaaa brrrrr."<<endl;
}
and Main.cpp
#include "horse.h"
int main(){
Horse h;
h.saySomething();
}
After I compile (compilation is successful) and run this I get this error message:
/tmp/ccxuDyrd.o: In function `main':
Main.cpp:(.text+0x11): undefined reference to `Horse::saySomething()'
collect2: ld returned 1 exit status
Please help me somehow.
Try
g++ -c main.cpp horse.cpp (to compile)
g++ -o a.out main.o horse.o (to link)
It seems you only compiled your code but did not link the resulting object files. You probably invoked the compiler like this:
g++ main.cpp
You should instead compile every *.cpp file separately and then link each resulting *.o file. And you should do this with a Makefile.
Actually, the basic idea is the same on Windows with MSVC. The compiler produces object files, the linker links them together.
I have made the following C++ program which is made up of 3 files:
The thing.h file
#ifndef THING_H
#define THING_H
class thing{
double something;
public:
thing(double);
~thing();
double getthing();
void setthing(double);
void print();
};
#endif
The thing.cpp file
#include <iostream>
#include "thing.h"
thing::thing(double d){
something=d;
}
thing::~thing(){
std::cout << "Destructed" << std::endl;
}
double thing::getthing(){
return something;
}
void thing::setthing(double d){
something = d;
}
void thing::print(){
std::cout <<"The someting is " << something << std::endl;
}
The main file
#include <iostream>
#include "thing.h"
int main(){
thing t1(5.5);
t1.print();
t1.setthing(7.);
double d=t1.getthing();
std::cout << d << std::endl;
system("pause");
return 0;
}
I had made this program previously all in one file and it ran perfectly but when I try split it into seperate files to create a header I get a linker error, here is the errors I get when I try run it from the main file:
[Linker error] undefined reference to `thing::thing(double)'
[Linker error] undefined reference to `thing::print()'
[Linker error] undefined reference to `thing::setthing(double)'
[Linker error] undefined reference to `thing::getthing()'
[Linker error] undefined reference to `thing::~thing()'
[Linker error] undefined reference to `thing::~thing()'
ld returned 1 exit status
From the above errors it seems asthough the main file doesnt recognise the functions inside the header, how do I fix this please?
In slightly less pedantic terms:
Your header file thing.h declares "what class thing should look like", but not its implementation, which is in the source file thing.cpp. By including the header in your main file (we'll call it main.cpp), the compiler is informed of the description of class thing when compiling the file, but not how class thing actually works. When the linker tries to create the entire program, it then complains that the implementation (thing::print() and friends) cannot be found.
The solution is to link all the files together when creating the actual program binary. When using the g++ frontend, you can do this by specifying all the source files together on the command line. For example:
g++ -o main thing.cpp main.cpp
will create the main program called "main".
It seems you are not linking thing.cpp into your "project".
If you are compiling using gcc:
g++ thing.cpp -o thing.o
g++ main.cpp -o main.o
g++ main.o thing.o -o my-best-application-ever
How to add the file to your project depends on the compiler/IDE/build-system you are using.
#sheu is right.. But you don't have to do anything if you just include thing.cpp in your main.cpp
Since you're already including thing.h in thing.cpp everything will work just fine if you include thing.cpp
Compiler knows about declarations of functions, but nothing about definitions. You need to say them where they are. The easiest way to do that is to create 'project' and add all files to it. Then compiler knows where to search all files.
Put some code in thing.cpp which let you know that it is being compiled i.e.
error ...
apparently it is not being compiled and linked...
When I run and build a simple program, it fails.
Here is the error message:
g++ -Wall -o "main" "main.cpp" (in directory: /home/markuz/Desktop)
/tmp/ccHV9wPu.o: In function main':
main.cpp:(.text+0x11): undefined reference toTest::display()'
collect2: ld returned 1 exit status
Compilation failed.
Here are the files. The compile and build command is the default of geany 1.22
//main.cpp
#include "imba.h"
int main(){
Test t;
t.display();
return 0;
}
//imba.h
class Test{
public:
void display();
};
//imba.cpp
#include <iostream>
#include "imba.h"
void Test::display(){
std::cout << "oi";
}
Any ideas about this?
Thanks.
You need to also add the imba.cpp file in the compilation step. Although you have included the header in your main file, you have not compiled the source for it and so the linker cannot find the object file for imba.cpp - that is what the error is complaining about