简体   繁体   English

Linux共享库c ++

[英]Linux Shared Libraries c++

I have a Shared Library wise.so. 我有一个明智的共享图书馆。 How I can use it in my programm? 我如何在我的程序中使用它? Do I need to include headers of that library? 我是否需要包含该库的标题?

I work with Eclipce under Linux. 我在Linux下使用Eclipce。 I have set a path to the library using -L and -l. 我使用-L和-l设置了库的路径。 But my function is not visible in the program. 但我的功能在程序中不可见。

Could you explain me how does Shared Library work? 你能解释一下共享图书馆是如何运作的吗?

Regards. 问候。

EDIT: 编辑:

I get the following error: 我收到以下错误:

int main() {
    char* path = "/export/home/pdmazubi3/workspace/proj1/src/pic.jpg";
    CEDD_Descriptor::CEDD ced; // undefined reference to `CEDD_Descriptor::CEDD::CEDD[in-charge]()'
    ced.execute(path);
}

Header: 标题:

class CEDD
    {
        public:
            CEDD(double Th0, double Th1, double Th2, double Th3,bool CompactDescriptor);
            CEDD();
            ~CEDD(void);

            double T0;
            double T1;
            double T2;
            double T3;
            bool Compact;

            double* execute(char* path);

        private:
            int cedd_segnum;                //number of segments
            int* cedd_partitionSize;        //number of pixels in each segment
    };

You need to include the header file in your application and link against it. 您需要在应用程序中包含头文件并链接它。

Have a look at how to use libraries in shared libraries and Linux howto . 看看如何在共享库Linux howto中使用库。

If the header file is not in the same directory as your application (which it usually isn't) then you need to tell compiler where to look for it, you use -I/path/to/include to include path to include directory that contains the header file. 如果头文件与您的应用程序不在同一目录中(通常不是),那么您需要告诉编译器在哪里查找它,使用-I/path/to/include来包含包含目录的路径包含头文件。

In linking step you need to point to the library. 在链接步骤中,您需要指向库。 The general usage is to use -L/path/to/lib is path to directory containing your library followed by -l<libname> where <libname> is the name of library without lib eg if you have libboost_serialization-d-1_34_1.so you would use -lboost_serialization-d-1_34_1 一般用法是使用-L/path/to/lib是包含库的目录的路径,后跟-l<libname> ,其中<libname>是没有lib的库的名称,例如,如果你有libboost_serialization-d-1_34_1.so你会使用-lboost_serialization-d-1_34_1

Examples: 例子:

g++ -I/sw/include -Wall -g -I/usr/local/include/boost-1_36/ -c main.cpp -o main.o
g++ -L/sw/lib -lboost_serialization-d-1_34_1 -o x main.o 

Have you also modified the Include path (the -I option) so it knows where to look for the headers for the library? 您是否还修改了包含路径(-I选项),以便它知道在哪里查找库的标头? If you haven't done this then the compiler will complain about being unable to find functions/classes/structures/etc. 如果你还没有这样做,那么编译器会抱怨无法找到函数/类/结构/等。

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

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