C++ 强制定义共享库中的所有函数

C++ 强制定义共享库中的所有函数,c++,dll,shared-libraries,C++,Dll,Shared Libraries,我想写一个共享库,如果我忘记实现一些函数,我想得到一个编译器/链接器错误 考虑以下情况: 测试h class Test { public: Test(); }; test.cpp #include "test.h" main.cpp #include "test.h" int main() { new Test(); } 如果我使用以下命令创建库gcc-c-fpic test.cpp&&g++-shared-o libtes

我想写一个共享库,如果我忘记实现一些函数,我想得到一个编译器/链接器错误

考虑以下情况:

测试h

class Test {
    public:
    Test();
};
test.cpp

#include "test.h"
main.cpp

#include "test.h"

int main() {
    new Test();
}
如果我使用以下命令创建库gcc-c-fpic test.cpp&&g++-shared-o libtest.so-Wl,--no undefined-Wl,--no allow shlib undefined test.o没有错误消息,但库已损坏。有没有办法强制创建一个未损坏的库


编辑:添加附加标志,但不更改结果这些代码已被修改:

测试h:

class Test {
    public:
    Test();
};

test.cpp:

#include "test.h"

Test::Test(){}  // you must implement the constructor
您必须实现构造函数,否则将出现错误“未定义对`Test::Test()'的引用”

main.cpp:

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

using namespace std;

int main(void)
{
        Test* t = new Test(); // you must define a pointer

        cout << "test* was created: " << t << endl;

        delete t;
        t = nullptr;

        return 0;
}
最后,我们在引用test.so共享库的同时编译main.cpp文件,并通过以下命令获得exe输出:

g++ -g main.cpp test.so -o test.exe

那没有帮助。我将不得不编写测试实现,而不是编写函数。如果您有一些单元测试,如果您没有实现,它们将失败。这怎么没用?因为我的目标是,如果我忘了什么,就会被人记住。但是,如果我必须记住编写测试或编写实现,对我来说没有什么区别。有什么区别吗?如果你不测试你的函数,即使你确实记得实现它们,你怎么知道它们没有“损坏”(如buggy)呢?我想你不理解我的问题(或者我不理解你的答案),但我知道如何修复代码,但我想知道的是,如何配置编译,使编译器拒绝在没有实现的ctor的情况下构建库。
g++ -g main.cpp test.so -o test.exe