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.
Related
This question already has answers here:
create many cpp files in one project [closed]
(4 answers)
Closed 7 years ago.
I have 2 files and i want to compile and run them using make command. I created a Makefile named "Makefile". They are compiled but shows an error
all: hello
hello: pgm1.o pgm2.o
g++ pgm1.o pgm2.o -o hello
pgm1.o: pgm1.cpp
g++ -c pgm1.cpp
pgm2.o: pgm2.cpp
g++ -c pgm2.cpp
They are compiled but shows an error
make -f Makefile
g++ pgm1.o pgm2.o -o hello
pgm2.o: In function `print2()':
pgm2.cpp:(.text+0x0): multiple definition of `print2()'
pgm1.o:pgm1.cpp:(.text+0x0): first defined here
collect2: ld returned 1 exit status
make: *** [hello] Error 1
pgm1.cpp
#include <iostream>
#include "pgm2.cpp"
using namespace std;
int main()
{
cout<<"Thiss is program 1";
print2();
return 0;
}
<>pgm2.cpp
#include <iostream>
using namespace std;
void print2()
{
cout<<"Thiss is program 2";
}
What is that error? How can i rectify it?
You are compiling both these files into singular output file but your pgm1.cpp already contains the function print2() by virtue of the line #include "pgm2.cpp"...
Possible solutions can be:
1) Remove the include file and instead add a function declaration.
void print2();
2) As already pointed out create a header file and use include it instead of a .cpp file.
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.
I'm just learning constructor and destructor I'm following the tutorial this guy is doing bucky tutorial. I think the video is outdated? since I followed every single step he says and i'm still getting an error.
main.cpp
#include <iostream>
#include "TESTING.h"
using namespace std;
int main(){
TESTING so;
cout << "TEST" << endl;
}
TESTING.h
#ifndef TESTING_H
#define TESTING_H
class TESTING
{
public:
TESTING();
protected:
private:
};
#endif // TESTING_H
TESTING.cpp
#include "TESTING.h"
#include <iostream>
using namespace std;
TESTING::TESTING()
{
cout << "TESTTTTT!!" << endl;
}
Error messages
\main.o:main.cpp undefined reference to 'TESTING::TESTING()'
Build log
mingw32-g++.exe -c D:\C++\TESTING!\main.cpp -o D:\C++\TESTING!\main.o
mingw32-g++.exe -o D:\C++\TESTING!\main.exe D:\C++\TESTING!\main.o
D:\C++\TESTING!\main.o:main.cpp:(.text+0x52): undefined reference to `TESTING::TESTING()'
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 1 second(s))
1 error(s), 0 warning(s) (0 minute(s), 1 second(s))
You only build and link the main source file, not the TESTING source file. You need to compile TESTING.cpp as well, and then link with TESTING.o:
mingw32-g++.exe -c D:\C++\TESTING!\TESTING.cpp -o D:\C++\TESTING!\TESTING.o
mingw32-g++.exe -o D:\C++\TESTING!\main.exe D:\C++\TESTING!\main.o D:\C++\TESTING!\TESTING.o
You need to include both the compilation units in the build.
That means both source files need to be compiled, and both corresponding object files specified in the link command.
At present, only main.cpp is being compiled to an object, and only main.o is being linked.
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.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have one class, Pose.hpp and Pose.cpp, and a main.cpp to use this, but when compile I'm getting error.
I'm using this to compile:
$g++ -c main.cpp
$g++ -c Pose.cpp
$g++ -o Pose.o main.o
// Pose.hpp
#include "Vec3f.hh"
#include <vector>
using namespace std;
using std::vector;
class Pose
{
Vec3f root_position;
vector < Vec3f > bonesAngles;
vector < Vec3f > bonesPosition;
public:
void setRootPosition (Vec3f position);
void addBone (Vec3f newBone);
};
//Pose.cpp
#include "Pose.hpp"
using namespace std;
void Pose :: setRootPosition (Vec3f position)
{
root_position = position;
}
void Pose :: addBone (Vec3f newBone)
{
bonesAngles.push_back(newBone);
}
//main.cpp
#include <iostream>
#include "Pose.hpp"
using namespace std;
int main()
{
Pose pose;
Vec3f aux(.2,.3,.4);
pose.addBone(aux);
return 0;
}
I got the error:
/tmp/ccSzsctS.o:main.cpp:function main: error: undefined reference to 'Pose::addBone(Vec3f)'
collect2: erro: ld returned 1 exit status
You must compile a .o file for each .cpp. Try this :
$g++ -c Pose.cpp -o Pose.o
$g++ -c main.cpp -o main.o
$g++ -o Pose.o main.o MyProgram
The .o files are called "object" files, there will contain basically all the information for final compilation. In your case, you need the functions definitions (it's what your compile error says). Compiling as you did only checks that the syntax is correct.
There is 2 steps during compiling : compiling and linking. Compiling (with your command lines) is only checking that the syntax is correct basically. At the end of this step the object files are produced. Linking is using the object files (and possibly other types of files, like .dll, .lib) to create a final product (in your case an executable file, but you can produce many different other things, like .dll, .lib, .a, etc ...).
Understanding compilation is a critical step in programming, I invite you to get into it.
For a more detailed explanation, have a look at : http://www.cs.fsu.edu/~jestes/howto/g++compiling.txt
Good luck !
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