Click here to Skip to main content
Click here to Skip to main content

Win32/Linux Dynamic Library Loader Class

, 12 Oct 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
A class to dynamically load DLLs and SO files and call functions from them
Sample Image - BCLoadLib.jpg


This class allows you to load a Library (a .dll or .so) and get function addresses from the lib on Windows or Linux.


I have been looking around for simple class like this for some of my cross platform programs and couldn't find one, so I made one.

Using the Code

The first thing you should know (one thing that held me up for a bit) is that on Linux you must add -ldl to the compile command so that it can link dlopen, dlsym and dlclose.
The best way to explain the code would be an example...

#include <iostream>
using namespace std;

#include "BCLoadLib.h"

typedef int (*func)(int);

int main()
        BCLoadLib* dll = new BCLoadLib("lib");
        cout << "Loaded.\n";
        func f = (func)dll->getFunc("bob");
        cout << f(7) << "\n";
        delete dll;
        cout << "Unloaded.\n";
    }catch(char* e){
        cout << e << "\n";
    return 0;

The first line of interest is:

typedef int (*func)(int);

This is a function pointer, you must define these yourself for every function you want to use from your DLL.
The code for this function in the lib is, if this doesn't make sense, you should look up function pointers.

extern "C" __declspec(dllexport) int bob(int i){
        return i;

Your functions must either be...

extern "C" __declspec(dllexport)


extern "C"

and have a .def file. You cannot load a C++ function with this lib.

This class uses throw/catch for error handling. If it can't open the library (file not there or error in file), or if it can't find the function you requested with getFunc, it will throw an error.

The next important line is where we create the class.

BCLoadLib* dll = new BCLoadLib("lib");

This actually loads the lib, windows supports just sending the name, but on Linux, we have to add ./, this way your program doesn't have to know if we are on Windows or Linux.

The next two important lines get a function address and call that function:

func f = (func)dll->getFunc("bob");
cout << f(7) << "\n";

Remember that func is our typedef for the function, so we create a new func f and set the address to the address from the DLL, typecasting the void* to our function type.


Well, I hope that helps some of you out there. Please tell me anything that might help or make the code better (or any spelling mistakes :P).


  • October 12, 2006: Fixed some spelling mistakes. This code is rather old, I would recommend looking at the class (it's small) and integrating it into your own code.
  • April 17, 2006: Fixed memory leak and possible memory access error in the Linux loading code.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
United States United States
No Biography provided

Comments and Discussions

Questionif library does mysteriously fails on loading in linux Pinmembercoder_of_salvation30-Aug-11 12:20 
Questionbugfix for mingw Pinmember_sqz_19-Aug-11 5:14 
GeneralFailure to Load dll Pinmemberstephen.cossgrove17-Oct-06 17:40 
AnswerRe: Failure to Load dll PinmemberBigcheesegs18-Oct-06 8:27 
GeneralBCLoadLib* dll = new BCLoadLib("lib"); PinmemberSceptic Mole12-Oct-06 8:51 
GeneralFailure to Load Function Pinmembercossy7412-Oct-06 5:11 
AnswerRe: Failure to Load Function PinmemberBigcheesegs12-Oct-06 5:36 
QuestionHow can I hide the export information of my DLL PinmemberSimon.W18-Apr-06 6:19 

How can I hide the export information of my DLL, such as function name or class name?
I am seeking...
For what?
Why did you ask me for what? I don't know!
AnswerRe: How can I hide the export information of my DLL PinmemberBigcheesegs18-Apr-06 9:43 
AnswerRe: How can I hide the export information of my DLL PinmemberHokei1-May-06 10:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150327.1 | Last Updated 12 Oct 2006
Article Copyright 2006 by Bigcheesegs
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid