view C++/doc/Dynlib/Home.md @ 343:8846b46fc435

Directory: fix for Unix
author David Demelier <markand@malikania.fr>
date Mon, 30 Mar 2015 13:01:18 +0200
parents 0b576ee64d45
children
line wrap: on
line source

# DynLib

This class provides routines to load shared libraries in a portable manner.

## Operating system support

| System  | Support  | Remarks                             |
|---------|----------|-------------------------------------|
| Linux   | Complete | Linker needs -ldl linker flags      |
| FreeBSD | Complete |                                     |
| Windows | Complete | Immediately policy is not available |

## API Reference

### Macros constants

- [DYNLIB_EXPORT](macro/DYNLIB_EXPORT.md)

### Classes

- [DynLib](class/Dynlib.md)

## Tutorial

This is the library code to be compiled as a shared module.

**Library.cpp**

````cpp
#include <iostream>
#include <string>

#include "DynLib.h"

extern "C" {

DYNLIB_EXPORT void a_sayHello(const std::string &name)
{
	std::cout << "Hello " << name << "!" << std::endl;
}

}
````

And this is the code that load the shared module.

**Main.cpp**

````cpp
#include <iostream>

#include "DynLib.h"

using HelloFunc = void (*)(const std::string &);

int main(void)
{
	try {
		DynLib library("./liba.so");

		HelloFunc hello = library.sym<HelloFunc>("a_sayHello");
		hello("Test");
	} catch (const std::exception &error) {
		std::cerr << error.what() << std::endl;
	}

	return 0;
}
````