Linker C++;循环依赖库的链接器顺序

Linker C++;循环依赖库的链接器顺序,linker,g++,cyclic,Linker,G++,Cyclic,我在两个单独的cpp文件中有两个类1和2。内容如下: #include "One.h" #include "Two.h" namespace Sample { One::One() {} void One::foo1() { Two t; t.foo(); } void One::foo() { } } g++ main.cpp -o main -L. -lOne -lTwo 还有2.cpp是 #include "Two.h

我在两个单独的cpp文件中有两个类1和2。内容如下:

#include "One.h"
#include "Two.h"
namespace Sample 
{
  One::One() {}
  void One::foo1() {   
    Two t;
    t.foo();
  }   
  void One::foo()     {     }   
}
g++ main.cpp -o main -L. -lOne -lTwo
还有2.cpp是

#include "Two.h"
#include "One.h"
namespace Sample {
Two::Two()    {    }   
void Two::foo1() {
    One t;
    t.foo();
}   
void Two::foo()  {}   
}   
我使用

g++ -c One.cpp; ar cr libOne.a One.o; ranlib libOne.a
g++ -c Two.cpp; ar cr libTwo.a One.o; ranlib libTwo.a
对于main,我调用两个类各自的
foo1
函数,并按如下方式编译main

#include "One.h"
#include "Two.h"
namespace Sample 
{
  One::One() {}
  void One::foo1() {   
    Two t;
    t.foo();
  }   
  void One::foo()     {     }   
}
g++ main.cpp -o main -L. -lOne -lTwo
即使我更改了1和2的顺序,代码也会编译

我希望代码不会编译,除非我重复1和2的顺序,因为存在循环依赖关系

原因可能是什么

我正在用g++版本4.8.2运行Ubuntu14.04

我希望代码不会编译

首先,您的代码应该编译,但可能无法链接。这些是不同的

其次,您没有显示
main.cpp
的内容。如果它引用了
第一类的任何方法
,那么您的代码应该链接良好

要了解原因,请阅读或阅读