Jump to content
  • Advertisement
Sign in to follow this  
CableGuy

DLLs in linux.

This topic is 4082 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Enjoy [smile]


// polygon.hpp:

#ifndef POLYGON_HPP
#define POLYGON_HPP

class 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 factories
typedef 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 factories

extern "C" polygon* create() {
return new triangle;
}

extern "C" void destroy(polygon* p) {
delete p;
}




# Compiler/Linker/dynamic linker
CC = g++
LD = g++

# flags to compile object files that can be used in a dynamic library
CFLAGS = -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 removal
RM = /bin/rm -f

# program's object files
PROG_OBJS = main.o

# program's executable
PROG = prog

# libraries to link with the program during compile time.
LIBS = -ldl

# shared library files
LIB_FILES = triangle.so

# shared libraries object files
LIB_OBJS = triangle.o

prog: prog.o
g++ $(CFLAGS) $(LIBS) -o prog main.o

prog.o: main.cc
g++ $(CFLAGS) $(LIBS) -c main.cc

# top-level rule
all: $(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 librarys
triangle.so: triangle.o
$(LD) $(DYNLINKFLAGS) $< -o $@



# clean everything
clean:
$(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 only
cleanlib:
$(RM) $(LIB_OBJS) $(LIB_FILES)

# clean the library's object files only
cleanlibobjs:
$(RM) $(LIB_OBJS)



To build it type "make" and then "make triangle.so".

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!