开发者

Difference between Linux and Windows linker

开发者 https://www.devze.com 2023-02-27 00:49 出处:网络
What is the difference in linking on various operating system? For example the following code produces a linker error on Windows (compiled both with Vs2010 and gcc), but compiles successfully on Linu

What is the difference in linking on various operating system?

For example the following code produces a linker error on Windows (compiled both with Vs2010 and gcc), but compiles successfully on Linux (Ubuntu,gcc):

extern int foo

int main() {
    foo=1;
}

Gcc command:

gcc -shared fil开发者_StackOverflow中文版ename.cpp


If you are trying to compile it as a windows shared library you need something like (code stolen from Wikipedia!) :-

#include <windows.h>


// DLL entry function (called on load, unload, ...)
BOOL APIENTRY DllMain(HANDLE hModule, DWORD dwReason, LPVOID lpReserved)
{
    return TRUE;
}

// Exported function - adds two numbers
extern "C" __declspec(dllexport) double AddNumbers(double a, double b)
{
    return a + b;
}

Windows shared modules (DLLs) require a DllMain entry point (executed the first time the module is loaded) and function names need to be exported via the declspec gobledygook before they can be used by another program.

0

精彩评论

暂无评论...
验证码 换一张
取 消