gpt4 book ai didi

c++ - 如何使用libc++用clang编译Google Benchmark

转载 作者:行者123 更新时间:2023-12-01 14:42:11 28 4
gpt4 key购买 nike

我想使用libc++用clang编译Google Benchmark文档中给出的示例:

#include <benchmark/benchmark.h>

static void BM_StringCreation(benchmark::State& state) {
for (auto _ : state)
std::string empty_string;
}
// Register the function as a benchmark
BENCHMARK(BM_StringCreation);

// Define another benchmark
static void BM_StringCopy(benchmark::State& state) {
std::string x = "hello";
for (auto _ : state)
std::string copy(x);
}
BENCHMARK(BM_StringCopy);

BENCHMARK_MAIN();
我使用以下命令:
clang++ -stdlib=libc++ example.cpp -lbenchmark -lpthread -o example
而且我有很多链接器错误:
[...]
clang: error: linker command failed with exit code 1 (use -v to see invocation)
完整的输出可用 here(超过1500行)。如果使用 -v选项,则可以看到:
clang version 7.0.1-8 (tags/RELEASE_701/final)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/8
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/8
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/8
Candidate multilib: .;@m64
Selected multilib: .;@m64
"/usr/lib/llvm-7/bin/clang" -cc1 -triple x86_64-pc-linux-gnu -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name example.cpp -mrelocation-model static -mthread-model posix -mdisable-fp-elim -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -v -resource-dir /usr/lib/llvm-7/lib/clang/7.0.1 -internal-isystem /usr/lib/llvm-7/bin/../include/c++/v1 -internal-isystem /usr/include/clang/7.0.1/include/ -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-7/lib/clang/7.0.1/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fdeprecated-macro -fdebug-compilation-dir /root -ferror-limit 19 -fmessage-length 0 -fobjc-runtime=gcc -fcxx-exceptions -fexceptions -fdiagnostics-show-option -o /tmp/example-9660e7.o -x c++ example.cpp -faddrsig
clang -cc1 version 7.0.1 based upon LLVM 7.0.1 default target x86_64-pc-linux-gnu
ignoring nonexistent directory "/include"
ignoring duplicate directory "/usr/include/clang/7.0.1/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/lib/llvm-7/bin/../include/c++/v1
/usr/include/clang/7.0.1/include
/usr/local/include
/usr/include/x86_64-linux-gnu
/usr/include
End of search list.
"/usr/bin/ld" --hash-style=both --build-id --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o example /usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../x86_64-linux-gnu/crt1.o /usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../x86_64-linux-gnu/crti.o /usr/bin/../lib/gcc/x86_64-linux-gnu/8/crtbegin.o -L/usr/bin/../lib/gcc/x86_64-linux-gnu/8 -L/usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../x86_64-linux-gnu -L/lib/x86_64-linux-gnu -L/lib/../lib64 -L/usr/lib/x86_64-linux-gnu -L/usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../.. -L/usr/lib/llvm-7/bin/../lib -L/lib -L/usr/lib /tmp/example-9660e7.o -lbenchmark -lpthread -lc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /usr/bin/../lib/gcc/x86_64-linux-gnu/8/crtend.o /usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../x86_64-linux-gnu/crtn.o
[...]
clang: error: linker command failed with exit code 1 (use -v to see invocation)
我不知道问题是否来自我的c或Google Benchmark安装。这是我安装两个脚本的脚本:
#!/bin/sh
apt-get -y install clang libc++-dev libc++abi-dev git cmake
git clone https://github.com/google/benchmark.git
git clone https://github.com/google/googletest.git benchmark/googletest
pushd benchmark
cmake -E make_directory "build"
cmake -E chdir "build" cmake -DCMAKE_BUILD_TYPE=Release -DLLVMAR_EXECUTABLE=/usr/bin/llvm-ar-7 -DLLVMNM_EXECUTABLE=/usr/bin/llvm-nm-7 -DLLVMRANLIB_EXECUTABLE=/usr/bin/llvm-ranlib-7 ../
cmake --build "build" --config Release --target install
popd
在第二个cmake命令期间,我收到以下消息:
CMake Warning:
Manually-specified variables were not used by the project:

LLVMAR_EXECUTABLE
LLVMNM_EXECUTABLE
LLVMRANLIB_EXECUTABLE
这是否意味着Google Benchmark未使用libc++构建,并且无法将其链接到我的基准测试?
如何解决此问题?

这是我的系统信息:
$ uname -a
Linux 1b3149ded49c 4.9.0-8-amd64 #1 SMP Debian 4.9.144-3.1 (2019-02-19) x86_64 GNU/Linux

最佳答案

在该项目的GitHub存储库中是一个travis file。与cmake命令对应的行是:

cmake -DCMAKE_C_COMPILER=${C_COMPILER} -DCMAKE_CXX_COMPILER=${COMPILER} -DCMAKE_BUILD_TYPE=${BUILD_TYPE} -DCMAKE_C_FLAGS="${EXTRA_FLAGS}" -DCMAKE_CXX_FLAGS="${EXTRA_FLAGS} ${EXTRA_CXX_FLAGS}" -DBENCHMARK_DOWNLOAD_DEPENDENCIES=ON -DBENCHMARK_BUILD_32_BITS=${BUILD_32_BITS} ${EXTRA_OPTIONS} ..
我要做的就是用我想要的替换travis变量:
cmake -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_FLAGS="-stdlib=libc++" -DBENCHMARK_DOWNLOAD_DEPENDENCIES=ON ../
安装后,我可以使用clang和libc++编译基准测试:
clang++ -stdlib=libc++ example.cpp -lbenchmark -lpthread -o example

关于c++ - 如何使用libc++用clang编译Google Benchmark,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63612465/

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