Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6882683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:17:09+00:00 2026-05-27T05:17:09+00:00

I keep getting complaint from the g++ compiler that the following code has problems.

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T05:17:09+00:00Added an answer on May 27, 2026 at 5:17 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I keep getting the following InvalidOperationException : The relationship between the two objects cannot
I keep getting the following error Could not find a base address that matches
I keep getting Clang errors on the following type of code and I can't
I keep getting a 400 bad request code from a hotfile.com page when I
I keep getting an error in the following code when I close the print
I keep getting this error: cannot convert parameter 1 from 'char' to 'LPCWSTR' int
I keep getting an AccessViolationException when calling the following from an external DLL: FILES_GetMemoryMapping(MapFile,
I keep getting the feeling that I'm benefitting from maybe half of the features
Keep getting this error after inserting a subdatasheet into a query and trying to
I keep getting tasks that are above my skill level. How can I address this without coming accross as grossly incompetent?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.