小编Rob*_*Rob的帖子

使用O1优化时出现C++链接器错误

在一个已经存在且非常大的项目中,我正在启用gcc编译器优化O1.

没有这个选项,一切都建立,链接和运行良好.启用该选项后,主可执行文件将编译并链接,但在链接其中一个单元测试时会出现链接器错误.

在这两次编译运行之间,我没有更改任何代码或make文件,只有O1优化标志.

我得到的错误是:

Configuration.a(Builder.o): In function `Builder::Create() const':
Builder.cpp:123: undefined reference to `Factory::Instance()'
Run Code Online (Sandbox Code Playgroud)

在链接过程中的某个地方,它会遇到Builder.cpp:cpp文件的这一行(第123行)

pObject = Factory::Instance()->CreateObject();
Run Code Online (Sandbox Code Playgroud)

(除了Instance函数的链接器错误,我也从CreateObject函数中得到一个)

当我看到时Factory,我看到:头文件

class Factory { public:
    static Factory* Instance(); << rest of the file >>
Run Code Online (Sandbox Code Playgroud)

并在cpp文件中

Factory* Factory::sInstance = 0;

Factory* Factory::Instance() {
    // Check if this is the first call
    if (sInstance == 0)
    {
        // Create only instance
        sInstance = new Factory();
    }

    // Address of the instance …
Run Code Online (Sandbox Code Playgroud)

c++ linker gcc compiler-optimization

5
推荐指数
1
解决办法
951
查看次数

标签 统计

c++ ×1

compiler-optimization ×1

gcc ×1

linker ×1