krisy
krisy

Reputation: 1558

Difference between Linux and Windows linker

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 filename.cpp

Upvotes: 3

Views: 1720

Answers (1)

James Anderson
James Anderson

Reputation: 27478

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.

Upvotes: 1

Related Questions