简体   繁体   中英

Makefile linking issue with stacked classes

I am trying to write a makefile for a piece of code that implements multiple classes that depend upon other classes. In order to perform this I thought I could isolate my code using object files and then compile everything into an executable but I keep running into the same error:

ld: symbol(s) not found for architecture x86_64

clang: error: linker command failed with exit code 1 (use -v to see invocation)

I have performed some tests to try and figure out the problem but I have been ultimately stumped. My code is separated across three source code files, two header files, and one makefile.

Here is the declaration of class B:

#ifndef B_H
#define B_H

class B
{
private:
    int _b;

public:
    B(int b);
    ~B();

    int getB();
};

#endif

Here is the source code for class B:

#include "b.h"

B::B(int b)
{
    _b = b;
}

B::~B()
{

}

int B::getB()
{
    return _b;
}

Here is the declaration of class C:

#ifndef C_H
#define C_H

#include "b.h"

class C
{
private:
    int _a;
    B* _b;

public:
    C(int a, int b);
    ~C();

    int add();
};

#endif

Here is the source code for class C:

#include "c.h"
#include "b.h"

C::C(int a, int b)
{
    _a = a;
    _b = new B(b);
}

C::~C()
{
    delete _b;
}

int C::add()
{
    return _a + _b->getB();
}

Here is the source code for the executable:

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

using namespace std;

int main(int argc, char const *argv[])
{
    C adder = C(3, 5);
    cout << adder.add() << endl;
}

Here is the makefile:

OPTS = -Wall

test: test.cpp c.o
    g++ -o test test.cpp c.o

c.o: c.cpp c.h b.o
    g++ -c c.cpp b.o

b.o: b.cpp b.h
    g++ -c b.cpp

Here is the output:

g++ -c b.cpp
g++ -c c.cpp b.o
clang: warning: b.o: 'linker' input unused
g++ -o test test.cpp c.o
Undefined symbols for architecture x86_64:
  "B::getB()", referenced from:
      C::add() in c.o
  "B::B(int)", referenced from:
      C::C(int, int) in c.o
  "B::~B()", referenced from:
      C::~C() in c.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [test] Error 1

The code when executed should print out 8 having passed the int values 3 and 5 to class C. Which then passes the 5 to class B. The value 5 is then accessed from class B and summed with the private variable in class C before being sent to the stdout.

The file compiles normally when using the following command, which leads me to believe the error is in the linker:

g++ -o test test.cpp c.cpp b.cpp

If you have any suggestions I would be glad to hear them. Thanks

Your problem is here:

g++ -c c.cpp b.o
clang: warning: b.o: 'linker' input unused

The compiler will not link when you use -c , just compile the source file(s) to respective object file(s).

The fix is to update your makefile line here:

test: test.cpp c.o b.o
    g++ -o test test.cpp c.o b.o

In other words, add the object file bo to both lines.

And then remove the same from here (and add the bh as a dependency)

c.o: c.cpp c.h b.h
    g++ -c c.cpp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM