简体   繁体   English

子库中对函数的未定义引用

[英]Undefined references to functions in sub-library

First of all, I use c++11 on linux with g++4.7 for all parts of my problem. 首先,对于问题的所有部分,我在Linux上使用g ++ 4.7的c ++ 11。

The setup of my problem: I created a shared library (lets call it "libA") that I use in different programs. 我的问题的设置:我创建了一个在其他程序中使用的共享库(称为“ libA”)。 Within this library is a part that was not exposed in the interface since it's not relevant for the programs. 该库中的某个部分未在界面中公开,因为它与程序无关。 Now, however, I'd like to use this previously hidden part in another library ("libB") directly. 但是,现在,我想直接在另一个库(“ libB”)中使用此先前隐藏的部分。

My plan was therefore to create a new library from the hidden part of libA. 因此,我的计划是从libA的隐藏部分创建一个新的库。 This would then be "libSub". 这将是“ libSub”。 libsub is then included into both libA and libB. 然后,libsub被同时包含在libA和libB中。 Both compile without errors. 两者都编译没有错误。 But when I now try to compile a program that depends on libA, I get lots of errors saying that there are undefined references to functions from libSub. 但是,当我现在尝试编译依赖于libA的程序时,出现很多错误,提示存在来自libSub的函数的未定义引用。

In order to make the structure a bit clearer: 为了使结构更清晰:

// Sub.hpp
class Sub{
    private:
        // private variables
    public:
        // interface functions
};

// A.hpp
class Sub; //forward declaring the sub-class
class A{
    private:
       std::shared_ptr<Sub> s;
       // more private variables
    public:
        // some interface functions
};

// A.cpp
#include <Sub.hpp> // include the declaration of the Sub class
// definitions of the member functions of A

// program.cpp
#include A.hpp
a=A();

The libraries are placed in local folders since I wanted to avoid installing them to the general lib-folders. 这些库放置在本地文件夹中,因为我想避免将它们安装到常规的lib文件夹中。 I guess that installing them all to the global lib-folder would solve the problem. 我想将它们全部安装到全局lib-folder可以解决问题。

And the question is: Is there a way to get rid of the errors and still use local folders? 问题是:是否有办法摆脱错误并仍然使用本地文件夹? And if so, how? 如果是这样,怎么办?

Have you tried to compile only this alone? 您是否尝试过仅单独编译?

// A.hpp
class Sub; //forward declaring the sub-class
class A{
    private:
       Sub s;
       // more private variables
    public:
        // some interface functions
};

Here Sub is an incomplete type and A will not compile because you need to have the class defined. Sub是一个不完整的类型, A将无法编译,因为您需要定义该类。 Look at this to see how you can use forward declarations. 查看此内容以了解如何使用前向声明。

I assume that you need to start by including the header files of your libSub in your other libraries. 我假设您首先需要在其他库中包含libSub的头文件。 Do that with a small example and come back if you have more problems. 举一个小例子,如果有更多问题,请回来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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