Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

undefined reference to B::B & B::~B

Tags:

c++

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
like image 337
q0987 Avatar asked Nov 30 '11 15:11

q0987


People also ask

How do you fix a undefined reference error?

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.

How do I fix undefined reference error in C++?

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.

What causes undefined reference in C++?

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.

What does undefined mean in C++?

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.


1 Answers

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
like image 92
NPE Avatar answered Sep 23 '22 19:09

NPE