gpt4 book ai didi

clang - C++ 11 代码使用 `clang++` 编译,但不使用 `clang -x c++`

转载 作者:行者123 更新时间:2023-12-04 05:39:39 34 4
gpt4 key购买 nike

基本问题

我有以下代码

#include <iostream>
#include <cstdint>
using namespace std;
int main ()
{
int32_t spam;
spam=5;
cout << "Hello World! We like " << spam << endl;
return 0;
}

当我这样做时,这编译得很好 clang++ -stdlib=libc++ cpptest.cpp .然而,用于 Sublime Text 2 的其他优秀的 SublimeClang 解析器似乎并不理解它。我想这可能是因为它使用了 clang -x c++而不是 clang++ ,并尝试使用 clang -x c++ -std=c++11 -stdlib=libc++ cpptest.cpp 编译我上面的代码片段(或它的各种排列),但它失败了,提示链接器。

所以我的问题是,如何 clang++不同于 clang -x c++ ?是否可以使用 clang -x c++ 编译上述代码段?而不是 clang++ ?

我的机器是运行 Mountain Lion 的 2012 款 MacBook Air。

调试东西

现在回到工作中,我玩了更多这个,并比较了不同版本的详细输出。

调用 clang -x c++
当我跑 clang -v -x c++ -stdlib=libc++ cpptest.cpp我得到
Apple clang version 4.1 (tags/Apple/clang-421.11.66) (based on LLVM 3.1svn)
Target: x86_64-apple-darwin12.2.1
Thread model: posix
"/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.8.0 -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name cpptest.cpp -pic-level 1 -mdisable-fp-elim -relaxed-aliasing -masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 134.9 -v -resource-dir /usr/bin/../lib/clang/4.1 -fmodule-cache-path /var/folders/8m/b4wllzbs67d9zxcj1dd8q6912jclkf/T/clang-module-cache -stdlib=libc++ -fdeprecated-macro -fdebug-compilation-dir /Users/me/Desktop -ferror-limit 19 -fmessage-length 80 -stack-protector 1 -mstackrealign -fblocks -fobjc-runtime-has-arc -fobjc-runtime-has-weak -fobjc-dispatch-method=mixed -fobjc-default-synthesize-properties -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -o /var/folders/8m/b4wllzbs67d9zxcj1dd8q6912jclkf/T/cpptest-1eOyZn.o -x c++ cpptest.cpp
clang -cc1 version 4.1 based upon LLVM 3.1svn default target x86_64-apple-darwin12.2.1
ignoring nonexistent directory "/usr/include/c++/v1"
#include "..." search starts here:
#include <...> search starts here:
/usr/bin/../lib/c++/v1
/usr/local/include
/usr/bin/../lib/clang/4.1/include
/usr/include
/System/Library/Frameworks (framework directory)
/Library/Frameworks (framework directory)
End of search list.
"/usr/bin/ld" -demangle -dynamic -arch x86_64 -macosx_version_min 10.8.0 -o a.out /var/folders/8m/b4wllzbs67d9zxcj1dd8q6912jclkf/T/cpptest-1eOyZn.o -lSystem /usr/bin/../lib/clang/4.1/lib/darwin/libclang_rt.osx.a
Undefined symbols for architecture x86_64:
[snip]
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

调用 clang++
当我打电话时 clang++ -v -stdlib=libc++ cpptest.cpp我得到以下信息。
clang++ -v -stdlib=libc++ cpptest.cpp 
Apple clang version 4.1 (tags/Apple/clang-421.11.66) (based on LLVM 3.1svn)
Target: x86_64-apple-darwin12.2.1
Thread model: posix
"/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.8.0 -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name cpptest.cpp -pic-level 1 -mdisable-fp-elim -relaxed-aliasing -masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 134.9 -v -resource-dir /usr/bin/../lib/clang/4.1 -fmodule-cache-path /var/folders/8m/b4wllzbs67d9zxcj1dd8q6912jclkf/T/clang-module-cache -stdlib=libc++ -fdeprecated-macro -fdebug-compilation-dir /Users/me/Desktop -ferror-limit 19 -fmessage-length 80 -stack-protector 1 -mstackrealign -fblocks -fobjc-runtime-has-arc -fobjc-runtime-has-weak -fobjc-dispatch-method=mixed -fobjc-default-synthesize-properties -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -o /var/folders/8m/b4wllzbs67d9zxcj1dd8q6912jclkf/T/cpptest-E7FTDV.o -x c++ cpptest.cpp
clang -cc1 version 4.1 based upon LLVM 3.1svn default target x86_64-apple-darwin12.2.1
ignoring nonexistent directory "/usr/include/c++/v1"
#include "..." search starts here:
#include <...> search starts here:
/usr/bin/../lib/c++/v1
/usr/local/include
/usr/bin/../lib/clang/4.1/include
/usr/include
/System/Library/Frameworks (framework directory)
/Library/Frameworks (framework directory)
End of search list.
"/usr/bin/ld" -demangle -dynamic -arch x86_64 -macosx_version_min 10.8.0 -o a.out /var/folders/8m/b4wllzbs67d9zxcj1dd8q6912jclkf/T/cpptest-E7FTDV.o -lc++ -lSystem /usr/bin/../lib/clang/4.1/lib/darwin/libclang_rt.osx.a

差异和解决方案?

我能找到的唯一主要区别是,当我使用 -x c++ 运行 clang 时标记 -lc++标志不会添加到以 "/usr/bin/lb" 开头的行的末尾.当我用 clang -v -lc++ -x c++ -stdlib=libc++ cpptest.cpp 编译时,一切正常。我还是不明白 clang++clang -x c++命令应该有所不同,但这似乎使我的示例程序至少可以编译,并且似乎表明命令与链接器执行不同的操作。

解决我的 Sublime Text 2 问题

整个小错误搜寻始于我试图弄清楚如何让 Sublime Text 2 停止提示我拥有的实际工作的代码,现在我想我已经明白了。所需要做的只是按照说明进行操作 here并将包含设置为
/usr/lib/c++/v1
/usr/include/c++/4.2.1
/usr/include/c++/4.2.1/ext

SublimeClang.sublime-settings文件。

最佳答案

你是对的。仅在链接阶段有差异。您可以仅使用 -c 标志运行 clang 进行编译,您将获得相同的结果。

然后你添加 -lc++ 标志,两个样本都是一样的。

关于clang - C++ 11 代码使用 `clang++` 编译,但不使用 `clang -x c++`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13532923/

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