【发布时间】:2016-10-01 14:11:37
【问题描述】:
如果我注入一个依赖项,让它成为A类的实例,在一个共享库中定义的B类中,共享库是否需要A类的目标文件进行链接? (意思是库编译后的g++联动阶段,而不是运行时的OS联动)
实际上我在 linux 上试过了,但没有。所有平台都是这样吗? (在这种情况下忽略符号可见性,除非必要)
啊。
#ifndef SOME_HEADERA_H
#define SOME_HEADERA_H
struct A{
void whoop();
};
#endif
a.cpp
#include <iostream>
#include "a.h"
void A::whoop (){
std::cout << "A whooped."<< std::endl;
}
b.h
#ifndef SOME_HEADERB_H
#define SOME_HEADERB_H
class A;
struct B {
void whoopUsingA(A* a);
};
#endif
b.cpp
#include "b.h"
#include "a.h"
#include <iostream>
void B::whoopUsingA (A* a){
std::cout << "B whoops A."<< std::endl;
a->whoop();
}
main.cpp
#include "a.h"
#include "b.h"
int main (int argc, const char* argv[]) {
A a;
B b;
b.whoopUsingA(&a);
return 0;
}
【问题讨论】: