Question

There is my problem:

All the code is in C++11.

Part 1

I've build a library (named socket). somwhere in the code, I declare a function:

namespace ntw {
    extern int dispatch(int id,Socket& request));
}

This function is user defined (most of the case, simply a big switch)) and by this way the body of it, is not define in the socket lib.

I use this function in server.cpp (which is part of the socket lib).

All is fine under linux, it build the .so perfectly. But under windows it create a

undefined reference to ntw::dispatch(int,Socket&)

So the .dll is not build.

Part2

I create the main program that use the socket lib.

And in this code, I make :

namespace ntw {
    int dispatch(int id,Socket& request)){
       /// some code
       return ...;
    }
}

finaly

So now, that i want is: the user define int dispatch(int id,Socket& request)) have to be call by the socket libary.

Under Ubuntu, all is fine (coppilation, adn run), but under windows .... it's the hell.

What can I do to make it work under windows xp and over ?


linux

Ubuntu 12.04, gcc 4.8.1

windows

windows xp, mingw 4.8.1

code

github: https://github.com/Krozark/cpp-Socket

It use cmake (if someone want to try).

Was it helpful?

Solution

What you are attempting won't work on Windows which has a quite different linking model from Linux. You need run time binding. I think you want the function to be provided by the host executable. In which case you need the host to export it with either a .def file or __declspec(dllexport). And then run time binding like this:

HMODULE hMod = GetModuleHandle(NULL); // gets host executable module handle
void *fn = GetProcAddress(hMod, FunctionName);

You can then cast fn to an appropriately declared function pointer before calling the function.

This is probably a reasonable approximation to how your Linux code operates. But it's not a very natural way to operate on Windows. More normal would be for the host to register callback functions or interfaces with the library. Once the host has informed the library of its callbacks, the library can use them.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top