使用 Clang 创建静态和共享库

Ale*_*ara 8 c++ linker clang static-libraries dynamic-library

使用under和commmand line创建静态库和动态库,然后将其链接到可执行文件的最小方法是什么?ClangLinuxWindows

假设项目包含一个main.cpp具有该功能的文件mainlib_header.h下一个文件/include/project_name和一个lib_source.c or lib_source.cpp/src

谢谢

use*_*240 13

对于静态库和动态库,首先单独编译源文件:

clang -c -o lib_source.o lib_source.c -fPIC
Run Code Online (Sandbox Code Playgroud)

对于 Linux 上的静态库,将所有 .o 文件归档在一起:

ar r library.a lib_source.o
Run Code Online (Sandbox Code Playgroud)

对于共享库,使用标志链接-shared

clang -shared -o library.so lib_source.o
Run Code Online (Sandbox Code Playgroud)

  • 您需要为 shlib 提及“-fPIC”。 (3认同)