• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • About
  • Life
  • Tech
  • Travel
  • Work
  • Questions
  • Contact

Welcome

.

Linking Order and Constructor Definition

April 10, 2020 by

Questions › Linking Order and Constructor Definition
0
Vote Up
Vote Down
Garmaine asked 4 years ago

I have the following files:

ClassA.h:

#ifndef CLASS_A_H
#define CLASS_A_H

class A
{
public:
    A();
    double foo();
private:
    double value;
};

#endif

ClassA.cpp

#include "ClassA.h"

A::A()
{
    this->value = 1.0;
}

double A::foo()
{
    return this->value;
}

ClassB.h

#ifndef CLASS_B_H
#define CLASS_B_H

#include "ClassA.h"

class B
{
public:
    B();
    double bar();

private:
    A a;
};

#endif

Class B.cpp

#include "ClassB.h"

B::B(){}

double B::bar()
{
    return this->a.foo();
}

main.cpp

#include "ClassB.h"
#include <iostream>

int main()
{
    B b;
    std::cout << b.bar() << std::endl;
    return 0;
}

If I compile everything together into an executable and run (g++ *.cpp && ./a.out), I get the expected output.

However, if I compile the two classes into separate libraries and try to link them into a single executable, I get linking errors:

$ g++ -fPIC -c CLassA.cpp
$ g++ -fPIC -c CLassB.cpp
$ g++ -shared ClassA.o -o libClasssA.so
$ g++ -shared ClassB.o -o libClasssB.so
$ g++ main.cpp -L./ -lClassA -lClassB

$ .//libClassB.so: undefined reference to `A::A()'
$.//libClassB.so: undefined reference to `A::foo()'
$ collect2: error: ld returned 1 exit status

One way to fix the link error is to simply swap the order of -lClassA and -lClassB in the executable compilation.

However, I also found that if I take the B constructor out of the .cpp file and place it in the header, the link error disappears.

So here are my two questions:

  1. Why does the link order matter here? This StackOverflow post claims link order doesn't matter for dynamic libs.
  2. Why does moving the implementation of the empty B constructor into the .cpp file cause link errors?
Are you looking for the answer?
Original Question and Possible Answers can be found on `http://stackoverflow.com`

Question Tags: c++, constructor, linker

Please login or Register to submit your answer




Primary Sidebar

Tags

Advancements best Business strategies commercial convenience economic Finances Cognitive decline Financial growth firm Future Hidden Gems Home hydration Impact Innovations lighting line of work Mental health Must-See New York City office patronage Productivity profession Profitability tips Profit optimization pursuit recreation Revenue enhancement romance sippy cups social station Technological breakthroughs technology toddlers trading transaction Treasures Uncover undertaking Well-being Wonders Work Young onset dementia

Newsletter

Complete the form below, and we'll send you all the latest news.

Footer

Footer Funnies

Who knew that reading the footer could be such a hilarious adventure? As we navigate websites, books, and documents, we often stumble upon the unassuming space at the bottom, only to discover a treasure trove of amusement. In this side-splitting compilation, we present 100 jokes that celebrate the unsung hero of content – the footer. Get ready to chuckle, giggle, and maybe even snort as we dive into the world of footnotes, disclaimers, and hidden comedic gems. Brace yourself for a wild ride through the footer!

Recent

  • Unveiling the Enigma: Almost-Magical Lamp Lights Highway Turns
  • The Impact of Young Onset Dementia on Employment and Finances: Optimizing Post-Diagnostic Approaches
  • 11 Wonders of 2023 Technological Breakthrough – Unveiling the Future
  • Work from Home and Stay Mentally Sane – Achieve Productivity and Well-being
  • Hidden Gems of New York City – Uncover the Must-See Treasures!

Search

Tags

Advancements best Business strategies commercial convenience economic Finances Cognitive decline Financial growth firm Future Hidden Gems Home hydration Impact Innovations lighting line of work Mental health Must-See New York City office patronage Productivity profession Profitability tips Profit optimization pursuit recreation Revenue enhancement romance sippy cups social station Technological breakthroughs technology toddlers trading transaction Treasures Uncover undertaking Well-being Wonders Work Young onset dementia

Copyright © 2023