我刚下载了CLang源代码,使用CMake创建了一个Visual C++ 10 IDE工作区,并构建了Visual C++ 10.0(express)中的所有内容.
现在我在hello world上遇到了一堆链接器错误:
d:\dev\test> type con >foo.cpp
#include <iostream>
using namespace std;
int main() { cout << "Hello, cling-clong world!" << endl; }
^Z
d:\dev\test> clang++ foo.cpp
foo-839435.o : error LNK2019: unresolved external symbol __ZSt4cout referenced in function _main
foo-839435.o : error LNK2019: unresolved external symbol __ZdlPv referenced in function __ZNSt14error_categoryD0Ev
foo-839435.o : error LNK2019: unresolved external symbol __ZSt18uncaught_exceptionv referenced in function __ZNSo6sentry
D2Ev
foo-839435.o : error LNK2019: unresolved external symbol ___cxa_rethrow referenced in function … 我正在编写一个C++库,我想让我的API抛出无效参数的异常,但是在编译代码时依赖于assert -fno-exceptions.
如果允许我使用异常处理,有没有办法在编译时检测?请注意,我正在编写一个只有头的库,所以我没有configure阶段,我无法访问构建系统,只需在命令行上定义一个宏(我不想增加负担给用户).
由于标准没有"-fno-exceptions"的任何概念,当然解决方案可能依赖于编译器.在这种情况下,我对使用g ++和clang ++的解决方案感兴趣,其他编译器对于这个项目并不重要.
非常感谢你
在键入时,clang --version我们获得了有关它所构建的LLVM版本的信息:
Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn)
Run Code Online (Sandbox Code Playgroud)
但是现在使用Xcode 7我们只得到以下内容:
Apple LLVM version 7.0.0 (clang-700.0.72)
Run Code Online (Sandbox Code Playgroud) 我对Rcpp有一些奇怪的麻烦 - 它使用了不可预测的C++编译器.这个问题有点类似于这个问题.
我在OSX上,我有2个编译器 - 默认clang和openmp clang-omp支持.我也有以下~/.R/Makevars文件(我设置clang-omp为默认编译器):
CC = clang-omp
CXX = clang-omp ++
CFLAGS + = -O3 -Wall -pipe -pedantic -std = gnu99
CXXFLAGS + = -O3 -Wall -pipe -Wno-unused -pedantic -fopenmp
问题是,我正在开发的软件包编译而clang++不是clang-omp++.我也尝试过(作为实验来解决问题)更改包src/Makevars和设置CXX=clang-omp++以及修改后的$R_HOME/etc/Makeconf CXX条目CXX = clang-omp++.没有运气 - 它仍然可以编译clang++.不知道为什么会这样.
这里也是小的可重现的(来自控制台R和来自Rstudio)的例子(不知道它是否与上面的问题有关).假设2个非常相似的cpp函数:
1.
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector timesTwo(NumericVector x) {
return x * 2;
} …Run Code Online (Sandbox Code Playgroud) 这是一个使用valarrays的简单c ++程序:
#include <iostream>
#include <valarray>
int main() {
using ratios_t = std::valarray<float>;
ratios_t a{0.5, 1, 2};
const auto& res ( ratios_t::value_type(256) / a );
for(const auto& r : ratios_t{res})
std::cout << r << " " << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果我编译并运行它像这样:
g++ -O0 main.cpp && ./a.out
Run Code Online (Sandbox Code Playgroud)
输出如预期:
512 256 128
Run Code Online (Sandbox Code Playgroud)
但是,如果我编译并运行它:
g++ -O3 main.cpp && ./a.out
Run Code Online (Sandbox Code Playgroud)
输出是:
0 0 0
Run Code Online (Sandbox Code Playgroud)
如果我使用-O1优化参数,也会发生相同
GCC版本是(最新的Archlinux):
$ g++ --version
g++ (GCC) 6.1.1 20160707
Run Code Online (Sandbox Code Playgroud)
但是,如果我尝试使用clang,两者都可以
clang++ -std=gnu++14 -O0 main.cpp && ./a.out
Run Code Online (Sandbox Code Playgroud)
和 …
以下代码在gcc 4.9.3和clang 3.7.1上编译并运行得很好
// std::unique_ptr
#include <memory>
// Template class for template-template arguments
template <typename Real>
struct Bar {};
// Base class
template <typename T,template <typename> class XX>
struct Base {};
// Derived class that operates only on Bar
template <typename Real>
struct Derived : public Base <Real,Bar> {};
// Holds the unique_ptr
template <typename T,template <typename> class XX>
struct Foo {
std::unique_ptr <Base <T,XX>> foo;
};
// Create an alias template
template <typename Real>
using Buz = Bar <Real>; …Run Code Online (Sandbox Code Playgroud) 采取以下代码:
#include <array>
int main() {
auto [a, unused] = [] {
return std::array<int, 2>{42, 24};
}();
[&] {
return a * 15;
}();
(void) unused;
}
Run Code Online (Sandbox Code Playgroud)
不用担心unused变量,我只想对数组进行解构。
使用最新的GCC和MSVC可以很好地编译代码,但是Clang 拒绝编译。更改lambda的捕获列表不会更改任何内容。有趣的是,a在捕获列表中显式设置会引发错误a is not a variable。
我很确定这是一个错误,但是我想问一下,主要是因为AFAIK a是对数组第一个元素的引用(main在这种情况下,生存期扩展到了范围),而不仅仅是一个int,请纠正我错误。
你可以从其他的问题看,问题是,a与unused不变量(是的,锵是正确的!),但参考的名字,不能用lambda表达式被捕获。不过,如果您确实需要这样的东西,可以为lambda使用局部变量,如下所示:
#include <array>
int main() {
auto [a, unused] = [] {
return std::array<int, 2>{42, 24};
}();
[&, a = a] …Run Code Online (Sandbox Code Playgroud) 为什么我不能enable_if在以下上下文中使用?
我想检测我的模板化对象是否具有成员函数 notify_exit
template <typename Queue>
class MyQueue
{
public:
auto notify_exit() -> typename std::enable_if<
has_member_function_notify_exit<Queue, void>::value,
void
>::type;
Queue queue_a;
};
Run Code Online (Sandbox Code Playgroud)
初始化为:
MyQueue<std::queue<int>> queue_a;
Run Code Online (Sandbox Code Playgroud)
我不断收到(clang 6):
example.cpp:33:17: error: failed requirement 'has_member_function_notify_exit<queue<int, deque<int, allocator<int> > >, void>::value';
'enable_if' cannot be used to disable this declaration
has_member_function_notify_exit<Queue, void>::value,
Run Code Online (Sandbox Code Playgroud)
或(g++ 5.4):
In instantiation of 'class MyQueue<std::queue<int> >':
33:35: required from here
22:14: error: no type named 'type' in 'struct std::enable_if<false, void>'
Run Code Online (Sandbox Code Playgroud)
我尝试了很多不同的东西,但不知道为什么我不能enable_if用来禁用这个功能。这不正是enable_if为了什么吗?
两个编译器为此代码示例生成不同的结果.Clang生成两种不同的类型.G ++使用相同的类型fu和fi.哪一个符合标准?
#include <iostream>
template< auto IVAL>
struct foo {
decltype(IVAL) x = -IVAL;
};
int main()
{
foo<10u> fu;
foo<10> fi;
std::cout << fi.x << " " << fu.x << '\n';
return 0;
}
Run Code Online (Sandbox Code Playgroud)
g ++ - 7.3输出:
4294967286 4294967286
clang-6.0输出:
-10 4294967286