2013-10-11 35 views
0

我试图运行一个使用__builtin_popcountll函数的程序。 当我编译使用生成文件,如下所示,其与编译命令/标志的源文件中的代码:g ++与Mountain Lion是否支持-msse4.2?

g++ -c -Wall `pkg-config opencv --cflags` -I./include -O3 -fopenmp -msse4.2 src/Utils.cpp -o src/Utils.o 

它编译没有任何错误/警告。但是,当我尝试链接对象(.o)文件以构建可执行文件时,出现undefined symbols错误。

下面是一个命令:

g++ src/BoostDesc.o src/Utils.o src/main.o `pkg-config opencv --libs` -lgomp -o main 

,这是完全错误:

Undefined symbols for architecture x86_64: 
    "___builtin_popcountll", referenced from: 
     __ZN9boostDesc5Utils12matchHammingERKN2cv3MatERKSt6vectorIS2_SaIS2_EERS5_IS5_INS1_6DMatchESaISA_EESaISC_EE.omp_fn.0 in Utils.o 
ld: symbol(s) not found for architecture x86_64 
collect2: ld returned 1 exit status 
make: *** [main] Error 1 

我抬头的手册页在苹果的网站here海湾合作委员会,并建议该标志工程我假设它也适用于g ++。有人可以确认或反驳使用这个内建函数的可能性吗?日Thnx!

g++ --version返回此:

i686-apple-darwin11-llvm-g++-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00) 
+0

你不需要将-msse4.2也传递到链接行吗? – Petesh

+0

nope,代码适用于其他机器(很可能是某些linux)。尽管如此我仍然尝试并仍然给出相同的错误 – Ani

回答

0

这种优化做指定了:-mpopcnt-march=corei7到编辑。

取一个样本:

% cat popcountl.c 
int main(int argc , char** argv) 
{ 
    volatile long long x = 0xf0f0f0f0f0f0f0f0; 

    return __builtin_popcountll(x); 

} 

证明不是在该版本的G ++支持:

~/D/e/popcountl [1]> /Applications/Xcode\ 4.6.3.app/Contents/Developer/usr/bin/g++ -mpopcnt -c popcountl.c 
cc1plus: error: unrecognized command line option "-mpopcnt" 
~/D/e/popcountl [1]> /Applications/Xcode\ 4.6.3.app/Contents/Developer/usr/bin/g++ -march=corei7 -c popcountl.c 
popcountl.c:1: error: bad value (corei7) for -march= switch 
popcountl.c:1: error: bad value (corei7) for -mtune= switch 
~/D/e/popcountl> /Applications/Xcode\ 4.6.3.app/Contents/Developer/usr/bin/g++ --version 
i686-apple-darwin11-llvm-g++-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00) 
Copyright (C) 2007 Free Software Foundation, Inc. 
This is free software; see the source for copying conditions. There is NO 
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 

工程与Xcode5 G ++虽然。

% g++ --version 
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1 
Apple LLVM version 5.0 (clang-500.2.75) (based on LLVM 3.3svn) 
Target: x86_64-apple-darwin12.5.0 
Thread model: posix 
g++ -mpopcnt -c popcountl.cpp 
dhcp-3-127:~% otool -tV popcountl.o               
popcountl.o: 
: 
0000000000000024 popcntq %rax, %rax 

它看起来像标志这种优化不存在于4.6版本的XCode提供的编译器,但目前在5.0版本的XCode编译器。请记住,你实际上在运行带有g ++前端的llvm编译器,所以它实际上并不运行官方的g ++;这可能是在这种情况下无法正常工作的根本原因。

+0

对不起,但你是说Xcode5 g ++的作品还是只有Xcode5 gcc的作品?我在我的机器上安装了Xcode5 – Ani

+0

Xcode 5 g ++的作品 - 它报告为Apple LLVM 5.0版本,而不是“i686-apple-darwin11-llvm-C++ 4.2” - 我会更新答案(我被召集到会议中) – Petesh

+0

记住,你实际上在运行clang ++ frontent的g ++驱动程序时运行llvm,而不是gcc/g ++ – Petesh