Multiple declaration of a function make command UNIX [duplicate] - c++

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.

Related

undefined reference to class::() C++ [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 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.

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.

undefined reference to `function_name'

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.

Undefined reference to a class method [closed]

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 !

C++ classes in separate file [duplicate]

This question already has answers here:
creating classes link error
(2 answers)
Closed 8 years ago.
I'm trying to learn how to make classes in C++ where I use a header file, a .cpp file that contains the class function definitions, and a main .cpp file. Here is what I have (taken from an example)
in class.h
class MyClass
{
public:
void foo();
int bar;
};
in class.cpp
#include "class.h"
using namespace std;
void MyClass::foo()
{
cout<< "test";
}
in main.cpp
#include "class.h"
using namespace std;
int main()
{
MyClass a;
a.foo();
return 0;
}
Compiling the main.cpp results in this error:
[Linker error] C:\:(.text+0x16): undefined reference to `MyClass::foo()'
collect2: ld returned 1 exit status
Do I need to compile the class.cpp or class.h? Am I missing a way of linking class.h with class.cpp? If so how do I link them?
You need to compile the implementation files into object files and link them together. The following is an example for when you are using g++:
g++ -c class.cpp -o class.o
g++ -c main.cpp -o main.o
g++ class.o main.o -o main
./main
In reality, you would add more options like -std=c++11 -O3 -Wall -Wextra -Werror etc.
You can try this on Linux shell using g++
Compile Create object files of main.cpp and class.cpp called main.o and class.o
g++ -c class.cpp
g++ -c main.cpp
Linking the object codes main.o and class.o to create executable file called program
g++ -o program main.o class.o
then run the program executable file
./program
You are likely to be compiling only main.cpp and not class.cpp.
What command are you using to generate the output ?
This should work fine :
g++ class.cpp main.cpp -o class
Its working fine
I tried the code in my Compiler
MyClass.h
#include <iostream>
class MyClass
{
public:
void foo();
int bar;
};
MyClass.cpp
#include "MyClass.h"
using namespace std;
void MyClass::foo()
{
cout<< "test";
}
Main.cpp
#include <iostream>
#include "MyClass.h"
int main(int argc, const char * argv[])
{
MyClass a;
a.foo();
return 0;
}
Ive tried the code in Xcode.
Its working just fine.
Use compiler option -I<dir of .h file> while compiling .cpp file. Compile both the .cpp files