windows - Linux 和 Windows 链接器之间的区别

标签 windows linux gcc linker shared-libraries

不同操作系统的链接有什么区别?

例如,以下代码在 Windows 上产生链接器错误(使用 Vs2010 和 gcc 编译),但在 Linux(Ubuntu、gcc)上编译成功:

extern int foo

int main() {
    foo=1;
}

Gcc 命令:

gcc -shared filename.cpp

最佳答案

如果您试图将其编译为 Windows 共享库,您需要类似(从维基百科窃取的代码!):-

#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 共享模块 (DLL) 需要一个 DllMain 入口点(在第一次加载模块时执行)并且函数名称需要通过 declspec gobledygook 导出,然后才能被另一个程序使用。

关于windows - Linux 和 Windows 链接器之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5727871/

相关文章:

linux - pcap 如何同时处理多个协议(protocol)

c++ - Ubuntu系统中不同内容的同名 header

适用于 68HC12/68HC12X 的 GCC 工具链

windows - 如何从给定标签的 clearcase 中提取整个代码?

windows - 如何使用Taskkill为批处理文件创建输入提示

linux - 2g 在 sed 中无法跳过第一次出现的匹配项

c++ - gcc 一起构建对象和依赖文件

windows - 如何在 Chrome 中启用 ActiveX?

c++ - .dll 、 .lib 、 .h 文件有什么区别?

linux - Linux 何时以及如何将 VGA 内存保存到 RAM?它在 pm-suspend 脚本中吗?或者在 echo mem >/sys/power/state 之后的内核中