undefined reference to class::() C++ [duplicate] - c++

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.

Related

Linker error: undefined reference in a function template that uses external variables

I have a mini project with two files:
main.cpp
#include <string>
template<int I>
int getint(int i)
{
extern std::string var;
return var.size() * i;
}
int main()
{
return getint<2>(2);
}
and sub.cpp
#include <string>
extern std::string var;
std::string var = "Hello";
But when I compile the project, the linker gives the error:
-------------- Clean: Debug in test (compiler: GNU GCC Compiler)---------------
Cleaned "test - Debug"
-------------- Build: Debug in test (compiler: GNU GCC Compiler)---------------
mingw32-g++.exe -Wall -fexceptions -g -c C:\Users\Fan\Downloads\test\test\main.cpp -o obj\Debug\main.o
mingw32-g++.exe -Wall -fexceptions -g -c C:\Users\Fan\Downloads\test\test\sub.cpp -o obj\Debug\sub.o
mingw32-g++.exe -o bin\Debug\test.exe obj\Debug\main.o obj\Debug\sub.o
obj\Debug\main.o: In function `Z6getintILi2EEii':
C:/Users/Fan/Downloads/test/test/main.cpp:6: undefined reference to `var'
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 0 second(s))
2 error(s), 0 warning(s) (0 minute(s), 0 second(s))
I have no clue how this happens, because as long as I simplified the program a little, the problem goes away.
If I make getint() a plain old function instead of a function template, then the compilation succeeds.
If I move the line "extern std::string var;" outside the function (say to one line above it), then the compilation succeeds.
If I change the type of var to an int (and omit ".size()" in main.cpp, and assign 1 to it in sub.cpp), then the compilation succeeds.
It seems a weird combination of factors that prevent my original project from compiling. I have no idea what they are :(
Edit: In response to the "cannot reproduce" remark, I am trying the compilation on my local windows 7 machine using Code::Blocks 17.12. Output of the compiler and the linker has been updated to include compiler flags.

simple c++ program does not run

While doing programming in Code::Blocks it does not compile for C++. Even for a this kind of a simple program.
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!" << endl;
return 0;
}
it gives those errors.
=== Build: Debug in start (compiler: GNU GCC Compiler) ===
obj\Debug\start.o||In function `main':
C:\Users\dp\Desktop\c++\start\start.cpp|4|multiple definition of `main'
obj\Debug\main.o:C:\Users\dp\Desktop\c++\start\main.cpp|6|first defined here
error: ld returned 1 exit status
=== Build failed: 3 error(s), 0 warning(s) (0 minute(s), 6 second(s)) ===
what can i do to fix this.
As-is the code itself should compile fine. But as the error suggests you have multiple int main() function definitions across multiple source files. One is in the start.cpp and the other is in the main.cpp file. Either keep only one main() entry point or compile a single file. Compile with g++ front-end, not with gcc.

Prototyped function isn't defined? [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 5 years ago.
So I've got a project file in CodeBlocks for Project Euler, but I'm a bit confused about what I've done wrong in setting my code up. I have a main.cpp file for running my programs, and I prototype each problem's function before I use it in the main block. However, I have this error when trying to build it:
||=== Build: Debug in Project Euler (compiler: GNU GCC Compiler) ===|
obj\Debug\main.o||In function main':|
C:\Users\under\cpp-workspace\Project Euler\main.cpp|9|undefined reference top4()'|
||error: ld returned 1 exit status|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
This is what I have, so I'm not sure what's wrong?
main.cpp:
#include <iostream>
using namespace std;
void p4();
int main()
{
p4();
return 0;
}
p4.cpp:
#include <iostream>
using namespace std;
void p4()
{
cout << "hello there" << endl;
}
I'm not sure what's wrong?
My question is not a duplicate, at least not that I can tell. The question this is supposedly a duplicate of never mentions the issue I'm having.
I've checked, and p4() is a void function with no inputs, so I'm not sure what's wrong.
You have declared p4() but you haven't defined it. Add
void p4()
{
}
to your file for an empty definition. If you need to do more in p4, add whatever code you want to.
Update
You just need to add p4.cpp to the set of files in your project.

Multiple declaration of a function make command UNIX [duplicate]

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.

build error when separating c++ code [duplicate]

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.