gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 09:35:36 26 4
gpt4 key购买 nike

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

例如,以下代码在 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com