Post

C++简单笔记

  1. -fPIC编译选项

1.1 什么是 -fPIC?

1
> 位置无关代码(Position Independent Code, PIC):码可以在内存中的任何位置运行,而不需要修改代码中的地址。共享库(.so 文件)需要位置无关代码,因为它们可以被加载到进程内存的任何位置。

1.2 为什么需要 -fPIC?

1
> 共享库会被多个程序共享,加载到内存的不同位置。如果代码不是位置无关的,共享库将无法正常工作。

1.3 windows不需要-fPIC

1
> 在 Windows 上编译动态链接库(DLL)时,通常不需要显式指定类似 -fPIC 的选项,因为 Windows 的 DLL 默认支持重定位。

{% include embed/bilibili.html id=’BV1X7411F744’ %}

This post is licensed under CC BY 4.0 by the author.