简体   繁体   English

如何使用C ++标准库进行静态链接?

[英]How to make static linking with C++ standard library?

i am has an object file(a.obj) and i am need to get executable(a.exe) file via linker call from command line. 我有一个目标文件(a.obj),我需要从命令行通过链接器调用获取可执行文件(a.exe)。 I am received a.obj file from this program: 我从该程序收到a.obj文件:

#include "stdio.h"
int main(){
    puts("Hello world");
}

and i am used clang compiler for generating a.obj file with follow up arguments: "clang.exe -c a.cpp". 我用clang编译器生成带有后续参数的“ .obj”文件:“ clang.exe -c a.cpp”。

My problem is using the "puts" method, which is defined in the standard library(may be libvcruntime.lib) and I don't know which arguments to use for linked to the standard library. 我的问题是使用“ puts”方法,该方法在标准库(可能是libvcruntime.lib)中定义,我不知道要使用哪些参数链接到标准库。

My linker this is Microsoft link.exe and me also available the lld linker, from llvm project(it is more preferables). 我的链接器是Microsoft link.exe,我也可以从llvm项目获得lld链接器(更可取)。

My global target - this is to get executable file from llvm ir and call lld linker from code but theis other history :) 我的全局目标-这是从llvm ir获取可执行文件并从代码中调用lld链接器,但这是其他历史记录:)

Visual Studio 视觉工作室

Specify /MT(d) instead of /MD(d) in project config. 在项目配置中指定/MT(d)而不是/MD(d) docs docs

clang

-static-libstdc++ -static-libgcc. -static-libstdc ++ -static-libgcc。 docs docs

If you're building for Windows with Clang, and you want to use Visual C++'s standard libraries, I suggest you use clang-cl , which is a driver that converts a Visual C++ cl command line options into clang's native options. 如果要使用Clang为Windows进行构建,并且要使用Visual C ++的标准库,建议您使用clang-cl ,该驱动程序将Visual C ++ cl命令行选项转换为clang的本机选项。

You said you're writing: 您说您正在写:

clang -c a.cpp

The -c option asks the compiler to just produce and object file and stop (rather than sending the object file to the linker). -c选项要求编译器只生成目标文件并停止(而不是将目标文件发送到链接器)。 It sounds like you want clang to call the linker, so you should omit the -c . 听起来您想让clang调用链接程序,所以您应该省略-c

To use the static version of the standard library, specify /MT (or /MTd if you want the debug version of the standard library). 要使用标准库的静态版本,请指定/MT (如果需要标准库的调试版本,则指定/MTd )。

Putting it all together, this should work for you: 放在一起,这应该为您工作:

clang-cl /MT a.cpp

clang-cl will translate the /MT to the equivalent option(s) for clang and then run clang. clang-cl会将/MT转换为clang的等效选项,然后运行clang。 When clang finishes compiling the object file, it'll then automatically call lld (the LLVM linker) with options compatible with the ones used for compiling, which should result in a working executable file. 当clang完成目标文件的编译后,它将使用与用于编译的文件兼容的选项自动调用lld(LLVM链接器),这将导致可执行文件运行。

For a while, when using clang to compile for Windows, you needed to use Microsoft's LINK instead of lld. 有一阵子,当使用clang为Windows编译时,您需要使用Microsoft的LINK而不是lld。 But recent versions can use lld, and, in fact, will use lld by default. 但是最新版本可以使用lld,事实上,默认情况下将使用lld。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM