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 6627161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:58:51+00:00 2026-05-25T21:58:51+00:00

Please help with the following noob question about C++ and g++ compilation and linking.

  • 0

Please help with the following noob question about C++ and g++ compilation and linking. Essentially I have 2 classes in 2 different files, and can compile them but when I attempt to link, one class can’t see the methods of the other, even though I am linking both. Order of object files does not help in this case.

The problem seems related to a non-default constructor that takes a parameter.

I have distilled and reproduced the problem in the following simple code:

File: a.cpp:

#include <iostream>

class A
{
  public:
  int my_int;
    A(int i) {
      my_int = i;
      std::cout << "A";
    }
};

File: a.hpp:

#ifndef __A_H_
#define __A_H_

class A
{
  public:
  A(int i);
};

#endif

File b.cpp:

#include <iostream>

using namespace std;

#include <a.hpp>

class B
{
  public:
  int my_int;
    B(int i) {
      my_int = i;
      A a(i);
      cout << "B\n";
    }
};

int main(int argc, char* argv[])
{
  B b(5);
  cout << "hello world: ";
  cout.flush();
  return 0;
}

Commands I use to build:

g++ -c -I. a.cpp
g++ -c -I. b.cpp
g++ -o c_test a.o b.o

Alternately, I’ve tried each of these:

g++ -o c_test b.o a.o
g++ -I. -o c_test a.cpp b.cpp
g++ -I. -o c_test b.cpp a.cpp

Error I get in any of above link scenarios:

b.o: In function `B::B(int)':
b.cpp:(.text._ZN1BC1Ei[B::B(int)]+0x1c): undefined reference to `A::A(int)'
collect2: ld returned 1 exit status

Thanks in advance for any insight.

(sorry if this is a re-post — I thought I posted it and don’t see it…)

  • 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-25T21:58:52+00:00Added an answer on May 25, 2026 at 9:58 pm

    It doesn’t work that way. What you’ve come across is technically an ODR violation, which roughly means that A in both a.cpp and b.cpp must be the same thing. It isn’t.

    Moreover, the constructor is implicitly inline in a.cpp and therefore its code needn’t be emitted.

    Changing a.cpp to

    #include <iostream>
    #include "a.hpp"
    
    A::A(int i) {
          my_int = i;
          std::cout << "A";
    }
    

    will fix the error.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please can someone help? I have the following code which uploads a file to
Please help me with the following problem: I have the following classes: class ChemicalElement
Please help regarding the following issue. I have enabled the Block popup option in
Can anyone please help me to work out how to achieve the following? I
Can someone please help me figure out a way to achieve the following (see
Please kindly help me in the following, I have $sub = C:\views\sght\gzad\text\hksdk\akldls\hool.java = C:\views\sght\bdsk\text\hksdfg\sdjks\same.java
Please help me to write the following sql. I have a table like this,
Please help me to understand why the following code will not compile: #include <stdio.h>
the following regexp is wrong - can someone please help to find all :-)
can someone please help me with the following query i need to update a

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.