Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. I have set a path to the library using -L and -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
    };
like image 632
user101375 Avatar asked Jun 08 '09 10:06

user101375


People also ask

What is shared library in C?

Shared libraries (also called dynamic libraries) are linked into the program in two stages. First, during compile time, the linker verifies that all the symbols (again, functions, variables and the like) required by the program, are either linked into the program, or in one of its shared libraries.

What is shared libraries in Linux?

Shared libraries are the most common way to manage dependencies on Linux systems. These shared resources are loaded into memory before the application starts, and when several processes require the same library, it will be loaded only once on the system. This feature saves on memory usage by the application.

Where are shared libraries in Linux?

In Linux, shared libraries are stored in /lib* or /usr/lib*. Different Linux distributions or even versions of the same distribution might package different libraries, making a program compiled for a particular distribution or version not correctly run on another.

What is a shared library in Linux?

Understanding Shared Libraries in Linux. In programming, a library is an assortment of pre-compiled pieces of code that can be reused in a program. Libraries simplify life for programmers, in that they provide reusable functions, routines, classes, data structures and so on (written by a another programmer), which they can use in their programs.

Where is the shared library located at runtime?

Because it’s impossible to know where the shared library code will be, this flag allows the code to be located at any virtual address at runtime. The -shared flag creates the shared library (shared libraries have the prefix lib and suffix .so [for s hared o bject].

How to find a shared library in a directory using-L?

The option -L is hint to the compiler to look in the directory followed by the option for any shared libraries (during link time only). The command generates an executable named as “ sample “. If you invoke the executable, the dynamic linker will not be able to find the required shared library.

How to get a list of shared library dependencies in Linux?

To get a list of all shared library dependencies for a binary file, you can use the ldd utility. The output of ldd is in the form: This command shows all shared library dependencies for the ls command.


1 Answers

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.

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.

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 e.g. if you have libboost_serialization-d-1_34_1.so you would use -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 
like image 195
stefanB Avatar answered Sep 30 '22 09:09

stefanB