I keep getting complaint from the g++ compiler that the following code has problems. After careful examination, I still cannot figure out why it cannot find the constructor and destructor of class B from embedMain.cpp.
Can someone give me a little hint?
Thank you
// embedMain.cpp
#include "embed.h"
int main(void)
{
B b("hello world");
return 0;
}
,
// embed.h
#ifndef EMBED_H
#define EMBED_H
#include <string>
class B
{
public:
B(const std::string& _name);
~B();
private:
std::string name;
};
#endif
,
// embed.cpp
#include <iostream>
#include <string>
#include "embed.h"
B::B(const std::string& _name) : name(_name) {}
B::~B() {
std::cout << "This is B::~B()" << std::endl;
}
,
~/Documents/C++ $ g++ --version
g++ (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2
~/Documents/C++ $ g++ -o embedMain embedMain.cpp
/tmp/ccdqT9tn.o: In function `main':
embedMain.cpp:(.text+0x42): undefined reference to `B::B(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
embedMain.cpp:(.text+0x6b): undefined reference to `B::~B()'
embedMain.cpp:(.text+0x93): undefined reference to `B::~B()'
collect2: ld returned 1 exit status
// Updated //
Based on comments from experts here, I have found the right way to link the embedMain.cpp with the embed library.
Here is the detail step:
user@ubuntu:~/Documents/C++$ tree
.
├── embed.cpp
├── embed.h
├── embedMain.cpp
user@ubuntu:~/Documents/C++$ g++ -Wall -c embed.cpp
user@ubuntu:~/Documents/C++$ ar -cvq libembed.a embed.o
user@ubuntu:~/Documents/C++$ g++ -o embedMain embedMain.cpp -L/home/user/Documents/C++ -lembed
user@ubuntu:~/Documents/C++$ tree
.
├── embed.cpp
├── embed.h
├── embedMain
├── embedMain.cpp
├── embed.o
├── libembed.a
So when we try to assign it a value in the main function, the linker doesn't find the symbol and may result in an “unresolved external symbol” or “undefined reference”. The way to fix this error is to explicitly scope the variable using '::' outside the main before using it.
You can fix undefined reference in C++ by investigating the linker error messages and then providing the missing definition for the given symbols. Note that not all linker errors are undefined references, and the same programmer error does not cause all undefined reference errors.
A likely cause for the "undefined reference" is that the compilation and link phase are being done with the gcc compiler instead of the g++ compiler. To confirm this, set the environment variable ATTOLSTUDIO_VERBOSE to 1 or within the GUI go to Edit -> Preferences -> Project and make sure "Verbose output" is checked.
Undefined Behavior in C and C++ So, in C/C++ programming, undefined behavior means when the program fails to compile, or it may execute incorrectly, either crashes or generates incorrect results, or when it may fortuitously do exactly what the programmer intended.
You need to compile embed.cpp
and link it into your executable, like so:
g++ -o embedMain embedMain.cpp embed.cpp
This compiles both files and links everything. To separate the three steps:
g++ -c embed.cpp
g++ -c embedMain.cpp
g++ -o embedMain embedMain.o embed.o
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With