My code is:
test.cpp
#include<iostream>
#include<boost/bind.hpp>
#include "extern.h"
using namespace std;
using namespace boost;
int fun(int x,int y){return x+y;}
/*
*void add(int &m,int &n);
*/
int main(){
int m=1;int n=2;
cout << m << " "<< n << endl;
add(m,n);
cout << m << " "<< n << endl;
return 0;
}
extern.h:
#include<iostream>
#include<boost/bind.hpp>
using namespace std;
using namespace boost;
void add(int &n,int &m);
extern.cpp:
#include<iostream>
#include<boost/bind.hpp>
using namespace std;
using namespace boost;
extern int m;
extern int n;
void add(int &n,int &m) {
n = n+1;
m = m+1;
}
When I compile it with
g++ -Wall -o test test.cpp
It turns out to be:
/tmp/ccMHVRNo.o: In function `main':
test.cpp:(.text+0x7b): undefined reference to `add(int&, int&)'
collect2: ld returned 1 exit status
But when I compile it with:
g++ -Wall -o test test.cpp extern.cpp
It works well:
$ ./test
1 2
2 3
So the reason is that test.cpp can't find the implementation of the add() function.
But I have added extern.h to test.cpp, why does it still say "undefined reference to add(int&, int&)"?
The header file extern.h only tells your program how the prototype of the function is made. The linker needs the actual implementation of the function, so it looks for the code add(int&,int&) references to, and it cannot find it unless you give to the compiler all the files it needs (in this case, extern.cpp is the file the linker needs when looking for the add function).
The implementation of the function void add(int&,int&) is in the source file extern.cpp. The compiler can't know that this files is related to the program until you tell it.
You must specify all source files at the command line:
g++ -Wall -o test test.cpp extern.cpp
If you want to avoid compiling of all source files you can also specify the already compiled object file:
g++ -Wall -o test test.cpp extern.o
When you don't want to think every time what commands you need to compile you should use make and create an Makefile with rules that define how the target is build. with the makefile you can just start
make
and get the result.
This is the usual formula:
g++ -Wall -c test.cpp
g++ -Wall -c extern.cpp
g++ -o test test.o extern.o
There are one-liners, but those don't scale so well to larger projects.
Related
hope you guys are doing well. I am just getting linker error in C++ , I don't know why? Everything is correct....
Check below testing.h file
#ifndef __MYClass__
#define __MYClass__
#include<iostream>
using namespace std;
class Abc {
private:
int a;
public:
void input();
void display();
};
#endif
and here's implementation of these functions in Functions.cpp file.
#include"testing.h"
void Abc::input() {
cout<<"Enter any value : ";
cin>>a;
}
void Abc::display() {
cout<<"You Entered : "<<a;
}
And now, in main.cpp
#include<iostream>
#include"testing.h"
using namespace std;
int main() {
Abc obj;
obj.input();
obj.display();
return 0;
}
All files are compiled successfully.
In main.cpp Linker says....
g++ -Wall -o "main" "main.cpp" (in directory: /home/Welcome/C++ Practices/testingLinux)
/usr/bin/ld: /tmp/ccYI9LAy.o: in function main': main.cpp:(.text+0x10): undefined reference to Abc::input()'
/usr/bin/ld: main.cpp:(.text+0x1c): undefined reference to `Abc::display()'
collect2: error: ld returned 1 exit status
Compilation failed.
I'm using built-in linux compiler...
There are multiple ways you can fix this but before that please read up on Translation Unit.
Coming to your problem.
When you write
g++ -Wall -o main main.cpp
The compiler will pick up main.cpp for compilation and expand testing.h that includes the declaration for class ABC and with this header file it can determine what is the size of ABC and be able to generate instructions reserving space for obj on the stack. It can't see the definition for input() and display() hence defers that task to the linker. Note that testing.cpp is not in the picture at all since the compiler doesn't know that the implementation of ABC is in testing.cpp. Now when the linker tries to resolve the symbols input() it fails to find the definition for it and throws the error
undefined reference to Abc::input()
So, to fix this you can tell explicitly upfront that it also needs to take in testing.cpp while compiling main.cpp by
g++ -o main main.cpp testing.cpp
Another way is to create a dynamic library out of testing.h and testing.cpp
g++ -shared -fPIC testing.cpp -o libtest
and then link it against main.cpp
g++ -o main main.cpp -I. -L. libtest
What this does is that the compiler still can't figure out the definition of input() and display() but the linker can since now the library containing the definitions is provided to it.
You are not compiling Functions.cpp file.
This should fix your issue:
g++ main.cpp Functions.cpp
I know there are many similar topics but there are equally many unique mistakes that may lead to this problem (so I think). Therefore I ask, after some research.
My problem is that the compiler, GNU GCC, when compiling one file does not see my namespace declared in another file. The IDE (CodeBlocks) evidently does see it as it auto-completes the name of the namespace. I tried to isolate the problem and came up with this:
File main.cpp:
namespace MyName
{
int MyVar;
}
#include "T1.cpp"
int main()
{
return 0;
}
File T1.cpp:
using namespace MyName;
error: 'MyName' is not a name-space name.
In my project I have a header file, say T1.h, and an implementation file T1.cpp — and MyName isn't accessible in either of them.
Any help or guidance would be appreciated.
What's happening is that CodeBlocks is compiling both main.cpp and T1.cpp. Here is what happens when you try to compile each one:
main.cpp:
$ g++ main.cpp
$
T1.cpp
$ g++ T1.cpp
T1.cpp:1:17: error: ‘MyName’ is not a namespace-name
using namespace MyName;
^
T1.cpp:1:23: error: expected namespace-name before ‘;’ token
using namespace MyName;
^
$
T1.cpp, when compiled on it's own, has no knowledge of MyName. To fix this, don't include .cpp files, and put your declarations in header files.
Edit: From what I gather, this may be a better way to organize your example:
T1.h:
namespace MyName {
extern int MyVar;
}
T1.cpp
#include "T1.h"
int MyName::MyVar = 5;
main.cpp
#include "T1.h"
#include <iostream>
using namespace MyName;
int main()
{
std::cout << MyVar << std::endl;
return 0;
}
Now it will compile correctly:
$ g++ -c T1.cpp -o T1.o
$ g++ -c main.cpp -o main.o
$ g++ T1.o main.o
$ ./a.out
5
I have a basic C++ file like so:
#include <iostream>
using namespace std;
int main() {
float x = rand();
cout << x << endl;
return 0;
}
When I run this through g++ on Ubuntu with g++ test.cpp -o test -std=c++11, I get no errors, and the program runs just fine. But when I run it through g++ on MinGW with the same command, I get the following error:
test.cpp: In function 'int main()':
test.cpp:6:17: error: 'rand' was not declared in this scope
float x = rand();
^
I have GCC version 5.3.0. Attempting to compile with g++ test.cpp -o test.exe -std=gnu++11 or g++ test.cpp -o test.exe -std=c++0x yield the same result.
You must include library for the random function first
i-e
#include < cstdlib >
After that your code will work perfectly
Here is the correct code
#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
float x = rand();
cout << x << endl;
return 0;
}
Some compilers allow you to use random function without including library but standard C++ compilers doesn't allow you.
Hope this will help you
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.
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.