Advertisement

DLLs in linux.

Started by April 05, 2007 09:55 AM
16 comments, last by taby 17 years, 7 months ago
hi, I have Ubuntu installed on my machine and I was wondering whether there is such a thing as a DLL for linux. Or are all the libraries are static ones. Thanks.
They exist, but they are called shared objects and end with the .so extension.
Advertisement
Thanks will look into it.
One more question.

In windows when you compile a dll you get a lib file which you
use to link with your programs, So the linker can know the symbols and everything.
From what i understood in linux you link directly to the "DLL".
If that is the case how do you export C++ class?
bump
On Linux you either export all symbols or none, to build a shared object you pass the -shared flag to the linker, which will export all symbols.

Then to link a second program against your .so, you use the -L and -l flags as you would with static libraries.
Advertisement
Which also handles classes?
Quote: Original post by CableGuy
Which also handles classes?


Yes, a class method is in reality a function that takes a pointer (the "this" pointer) to the particular instance of the class (object) on witch it is being called, the class itself is declared on a header file, the compiler and linker take care of all the dirty work for you.

Note that due to Name Mangling and differences between compiler ABI's, you may not be able to link an application with compiler A against a C++ shared library compiled with compiler B, this is the same as with Windows DLLs, and thats why COM and CORBA came to be.
Enjoy [smile]

// polygon.hpp:#ifndef POLYGON_HPP#define POLYGON_HPPclass polygon {protected:    double side_length_;public:    polygon()        : side_length_(0) {}    void set_side_length(double side_length) {        side_length_ = side_length;    }    virtual double area() const { }};// the types of the class factoriestypedef polygon* create_t();typedef void destroy_t(polygon*);#endif


//main.cc:#include "polygon.hpp"#include <iostream>#include <dlfcn.h>int main() {    using std::cout;    using std::cerr;    // load the triangle library    void* triangle = dlopen("./triangle.so", RTLD_LAZY);    if (!triangle) {        cerr << "Cannot load library: " << dlerror() << '\n';        return 1;    }    // load the symbols    create_t* create_triangle = (create_t*) dlsym(triangle, "create");    destroy_t* destroy_triangle = (destroy_t*) dlsym(triangle, "destroy");    if (!create_triangle || !destroy_triangle) {        cerr << "Cannot load symbols: " << dlerror() << '\n';        return 1;    }    // create an instance of the class    polygon* poly = create_triangle();    // use the class    poly->set_side_length(7);        cout << "The area is: " << poly->area() << '\n';    // destroy the class    destroy_triangle(poly);    // unload the triangle library    dlclose(triangle);}


// triangle.cc:#include "polygon.hpp"#include <cmath>class triangle : public polygon {public:    virtual double area() const {        return side_length_ * side_length_ * sqrt(3) / 2;    }};// the class factoriesextern "C" polygon* create() {    return new triangle;}extern "C" void destroy(polygon* p) {    delete p;}


# Compiler/Linker/dynamic linkerCC = g++LD = g++# flags to compile object files that can be used in a dynamic libraryCFLAGS = -fPIC# on some platforms, use '-fpic' instead.# Flags to create a dynamic library.DYNLINKFLAGS = -fPIC -shared -nostdlib -rdynamic# on some platforms, use '-G' instead.# files removalRM = /bin/rm -f# program's object filesPROG_OBJS = main.o# program's executablePROG = prog# libraries to link with the program during compile time.LIBS = -ldl# shared library filesLIB_FILES = triangle.so# shared libraries object filesLIB_OBJS = triangle.oprog: prog.o	g++ $(CFLAGS) $(LIBS) -o prog main.o	prog.o: main.cc	g++ $(CFLAGS) $(LIBS) -c main.cc# top-level ruleall: $(LIB_FILES) $(PROG)	$(PROG): $(PROG_OBJS)	$(LD) $(LDFLAGS) $(PROG_OBJS) $(LIBS) -o $(PROG)# compile C source files into object files.%.o: %.cc	$(CC) $(CFLAGS) -c $<# create our librarystriangle.so: triangle.o	$(LD) $(DYNLINKFLAGS) $< -o $@# clean everythingclean:	$(RM) $(PROG_OBJS) $(PROG) $(LIB_OBJS) $(LIB_FILES)# clean the program and its object files, don't touch the library.cleanprog:	$(RM) $(PROG_OBJS) $(PROG)# clean the library and its object files onlycleanlib:	$(RM) $(LIB_OBJS) $(LIB_FILES)# clean the library's object files onlycleanlibobjs:	$(RM) $(LIB_OBJS)


To build it type "make" and then "make triangle.so".
Thanks, got it working.

This topic is closed to new replies.

Advertisement