dll and shared object differences

Hello,
I'm writing a cross-platform disk IO library and I decided to make it a separate module. On Windows this is a DLL and on Linux this is called a shared object (I think). Now I'm asking if there are any significant differences between them - is there something like DllMain function for shared objects and can I link them with other programs by using import libraries?

Thanks for help.
The most important difference I've noticed: DLLs only expose functions that have been explicitly exported, whereas shared objects expose all functionality.

Oh, and loaded shared objects can be shared across processes to save memory (hence the "shared" part), while DLLs can't.
So this code will be portable?
1
2
3
4
5
6
7
8
9
10
11
#if define(WIN32) ||defined (_WIN32)
  #define DLLEXPORT extern "C" __declspec(dllexport)
#else
  #define DLLEXPORT
#endif

// ...
DLLEXPORT void function()
{
    cout <<"Hello world!";
}

(I know that gcc compiling options differ for shared objects and DLLs)

Oh, and loaded shared objects can be shared across processes to save memory (hence the "shared" part), while DLLs can't.

What handles this "shared" part? The OS or the programmer?
Yes, it's portable, but regardless of the platform, you should use C linkage for functions you want to use from the outside.

The OS handles sharing SOs.
Oh and the last thing: what functions are used to dynamically call functions from shared objects?
Take a look at this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
class LibraryLoader{
	void *lib;
public:
	enum reason{
		NO_ERROR=0,
		LIBRARY_NOT_FOUND,
		FUNCTION_NOT_FOUND
	} error;
	LibraryLoader(const char *libName);
	~LibraryLoader();
	void *getFunction(const char *functionName);
};

#if /*windows*/
#include <windows.h>
#elif /*unix*/
#include <dlfcn.h>
#endif

#ifdef NO_ERROR
#undef NO_ERROR
#endif

LibraryLoader::LibraryLoader(const char *libName){
#if /*windows*/
	std::string filename=libName;
	filename.append(".dll");
	this->lib=LoadLibraryA(filename.c_str());
#elif /*unix*/
	std::string filename="lib";
	filename.append(libName);
	filename.append(".so");
	this->lib=dlopen(filename.c_str(),RTLD_LAZY);
#endif
	this->error=reason((!this->lib)?LIBRARY_NOT_FOUND:NO_ERROR);
}

LibraryLoader::~LibraryLoader(){
	if (!this->lib)
		return;
#if /*windows*/
	FreeLibrary((HMODULE)this->lib);
#elif /*unix*/
	dlclose(this->lib);
#endif
}

void *LibraryLoader::getFunction(const char *functionName){
	if (!this->lib)
		return 0;
	void *ret=
#if /*windows*/
		(void *)GetProcAddress((HMODULE)this->lib,functionName);
#elif /*unix*/
		dlsym(this->lib,functionName);
#endif
	this->error=reason((!ret)?FUNCTION_NOT_FOUND:NO_ERROR);
	return ret;
}
Thanks!
Topic archived. No new replies allowed.