gpt4 book ai didi

c++ - 如何在不调用链接器的情况下将 .cpp 文件编译为目标文件

转载 作者:可可西里 更新时间:2023-11-01 17:37:08 26 4
gpt4 key购买 nike

我正在尝试从构建过程中单独编译单个 .cpp 文件。

如果我进行完整构建,那么编译步骤会输出到创建项目时配置的目录。

但是,如果我只是请求编译,我最终会将目标文件转到与源代码相同的目录,更糟糕的是,它继续将目标文件链接到可执行文件,而实际上它应该在进行编译。

请注意,我正在为 C++11 使用 clang++ 进行编译,但我认为这对为什么它第二次调用 Clang++.exe 进行链接没有任何要求没有任何限制。

编辑1

构建时会这样做

-------------- Build: Debug in GOTW (compiler: GNU GCC Compiler)---------------

clang++.exe -Wall -fexceptions -g -std=c++11 -stdlib=libstdc++ -c
C:\Work\test\2010\C++11\CLang\GOTW\gotw.cpp -o obj\Debug\GOTW.o
clang++.exe -o bin\Debug\GOTW.exe obj\Debug\GOTW.o
Output size is 203.50 KB
Process terminated with status 0 (0 minutes, 11 seconds)
0 errors, 6 warnings (0 minutes, 11 seconds)

然而,当执行Compile Current File 时,它会这样做:

clang++.exe -std=c++11 -stdlib=libstdc++    -c GOTW.cpp -o GOTW.o
1 warning generated.
clang++.exe -o GOTW.exe GOTW.o

我不明白为什么它输出第二步,以及如何让它像构建那样使用 obj 和 bin 目录

最佳答案

Use the -c option.

-c

Run all of the above, plus the assembler, generating a target ".o" object file.

关于c++ - 如何在不调用链接器的情况下将 .cpp 文件编译为目标文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16697737/

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