我正在尝试检测用于编译源代码的编译器.我可以很容易地找到预定义的宏来检查MSVC或GCC(例如参见http://predef.sourceforge.net/),但我找不到任何宏来检查clang.
有人知道clang是否定义了一个宏__CLANG__,以便知道当前正在编译我的代码的是什么?
我正在考虑auto在一些跨平台项目(Windows + Mac)中使用一些C++ 11功能(例如).在Windows上,Visual Studio支持即将推出的C++ 11标准的部分内容,这些内容允许我简化部分代码库,因此我很自然地会对开始使用这些功能感兴趣.
但据我所知,目前的XCode版本(3.2.4 + GCC 4.2)根本不支持任何C++ 11功能.我可以以某种方式升级GCC版本或CLang版本吗?或者,我是否应该咬紧牙关,等待Apple将来某个时候打包新版本?
今天我在 C++ 中捕获异常时遇到了一个奇怪的行为,有人可以向我解释一下吗?代码片段
#include <iostream>
#include <string>
#include <exception>
int main() {
try {
std::stod("notanumber");
} catch (const std::invalid_argument&) {
std::cerr << "std::invalid_argument" << std::endl;
} catch (const std::out_of_range&) {
std::cerr << "std::out_of_range" << std::endl;
} catch (const std::exception&) {
std::cerr << "Caught by ancestor" << std::endl;
} catch (...) {
auto ptr = std::current_exception();
auto type = __cxxabiv1::__cxa_current_exception_type();
std::cerr << type->name() << std::endl;
std::cerr << "..." << std::endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
写入输出
St16invalid_argument
...
Run Code Online (Sandbox Code Playgroud)
环境详情
C++ 14, disabled …Run Code Online (Sandbox Code Playgroud) 可能重复:
我可以在Xcode中使用C++ 11吗?
似乎xcode 4包含旧版本的clang和gcc.我可以升级gcc或clang并将它们与xcode 4一起使用吗?我想用gcc> = 4.6或最新的clang.
我的主要目标是在mac osx lion上编写C++时可以使用C++ 11中的新功能,因此如果需要,dcodeing xcode也是一个选项.
我有什么选择来实现这一目标?
在OS X Snow Leopard(10.6)中,我使用以下几行~/.bashrc来强制使用clang而不是标准gcc进行编译:
# Set Clang as the default compiler for the system
export CC=clang
export CFLAGS=-Qunused-arguments
export CPPFLAGS=-Qunused-arguments
Run Code Online (Sandbox Code Playgroud)
我也(偶尔)必须使用以下行来使用llvm-gcc,当clang无法编译某些东西时(PostgreSQL很长一段时间都犯了这个错误):
# Set LLVM GCC as the default compiler for the system
export CPP='llvm-gcc-4.2'
export CC='llvm-gcc-4.2'
export CXX='llvm-g++'
Run Code Online (Sandbox Code Playgroud)
在OS X Lion(10.7)上,这些行仍然需要吗?llvm-gcc(或clang)是系统的默认编译器吗?或者这些线仍然需要住在我的~/.bashrc?
这是我所说的代码部分.
try {
std::cerr << "first try" << std::endl;
po::store(po::parse_config_file(ifs, _configFileOptions, false), vm);
} catch(...) {
std::cerr << "second try" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
只是为了寻找细节,我使用boost program_options来解析配置文件.由于我在文件中添加了一个无法识别的选项,因此引发了异常.
Clang没有捕获此异常存在问题.基本上我只在输出中看到
first try
libc++abi.dylib: terminating with uncaught exception of type boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::program_options::unknown_option> >: unrecognised option 'TestFrequency'
Abort trap: 6
Run Code Online (Sandbox Code Playgroud)
这是我的铿锵版:
c++ --version
Apple LLVM version 7.3.0 (clang-703.0.31)
Target: x86_64-apple-darwin15.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
Run Code Online (Sandbox Code Playgroud)
编辑:当没有异常时,解析和一切正常.
我正在建立一个共享库f-no-rtti.在内部,此库会抛出std:invalid_argument并捕获std::exception,但从catch不输入该子句.
以下代码重现了该问题(g ++ 4.2,Mac OS X 10.6):
// library.cpp: exports f(), compiled with -fno-rtti
#include <stdexcept>
#include <iostream>
extern "C" {
void f() {
try {
throw std::invalid_argument("std::exception handler");
} catch( std::exception& e) {
std::cout << e.what() << "\n";
} catch(...) {
std::cout << "... handler\n";
}
}
}
Run Code Online (Sandbox Code Playgroud)
// main.cpp: the main executable, dynamically loads the library
#include <dlfcn.h>
typedef void(*fPtr)();
int main() {
void* handle = dlopen( "./libexception_problem.dylib", RTLD_LAZY ); …Run Code Online (Sandbox Code Playgroud) 较旧的主题提到,如果您以注册的iOS开发人员身份登录,则可以在https://developer.apple.com/downloads/index.action?name=Xcode上的Xcode下载中找到Xcode for Snow Leopard的下载-但我看不出来.
对此主题的评论有直接链接到下载,但点击该链接会重定向到"拒绝访问"错误页面:
我在尝试编译此代码时遇到问题:
int *array_aleatorio = new int[8]{0, 1, 2, 3, 4, 5, 6, 7};
Run Code Online (Sandbox Code Playgroud)
这是出现的错误:
main.cpp:315:38:错误:预期';' 在声明结束时
int*array_aleatorio = new int [8] {0,1,2,3,4,5,6,7};
我的一个队友告诉我这可能是因为我没有使用正确的编译器.他正在使用C++11并且有效.我试图安装它没有任何成功.
我正在使用OSX 10.6.8和Netbeans 7.3
我正在使用clang++.

我已经试过这本通过加入解-std=c++11 -stdlib=libc++ -Weverything线项目的编制属性,但它不工作:

这是当我尝试通过添加上面的行来执行它时出现的错误.
clang ++ -std = c ++ 11 -stdlib = libc ++ -Weverything -c -g -std = c ++ 11
-stdlib = libc ++ -Weverything -MMD -MP -MF build/Debug/GNU-MacOSX/main.od -o build/Debug/GNU-MacOSX/main.o
main.cpp clang:warning:编译期间未使用的参数:
' - std = c ++ …