Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 979
krisy Avatar asked Mar 19 '26 04:03

krisy


1 Answers

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.

like image 52
James Anderson Avatar answered Mar 21 '26 19:03

James Anderson