【问题标题】:How to use linked libraries compiled with libc++ libstdc++ mixed如何使用使用 libc++ libstdc++ 混合编译的链接库
【发布时间】:2019-07-10 10:43:00
【问题描述】:

我正在尝试编写一个脚本,该脚本使用一个用 clang 编译的库和另一个用 G++ 编译的库,我收到以下错误:

ld.lld: error: undefined symbol: myFunction()

这(根据Difference between string and char[] types in C++)显然是由于 std::string 在不同版本的 G++ 和 clang 中的含义不同。

这是否意味着我必须重写目标库的 Makefile 才能使用相同版本的 G++/Clang?因为仅仅链接一个预先编写的库似乎需要付出巨大的努力,而且我想我一定在这里遗漏了一些东西。

更多信息

我正在使用 ninja -C out/debug 编译 v8_shell

defines = -DUSE_UDEV -DUSE_AURA=1 -DUSE_GLIB=1 -DUSE_NSS_CERTS=1 -DUSE_X11=1 -DNO_TCMALLOC -DMEMORY_TOOL_REPLACES_ALLOCATOR -DMEMORY_SANITIZER_INITIAL_SIZE -DADDRESS_SANITIZER -DFULL_SAFE_BROWSING -DSAFE_BROWSING_CSD -DSAFE_BROWSING_DB_LOCAL -DCHROMIUM_BUILD -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_GNU_SOURCE -DCR_CLANG_REVISION=\"353250-1\" -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -DCOMPONENT_BUILD -D_LIBCPP_ABI_UNSTABLE -D_LIBCPP_ABI_VERSION=Cr -DCR_LIBCXX_REVISION=349080 -DCR_LIBCXXABI_REVISION=347903 -D_LIBCPP_ENABLE_NODISCARD -DCR_SYSROOT_HASH=e7c53f04bd88d29d075bfd1f62b073aeb69cbe09 -D_DEBUG -DDYNAMIC_ANNOTATIONS_ENABLED=1 -DWTF_USE_DYNAMIC_ANNOTATIONS=1 -DENABLE_DISASSEMBLER -DV8_TYPED_ARRAY_MAX_SIZE_IN_HEAP=64 -DENABLE_GDB_JIT_INTERFACE -DENABLE_MINOR_MC -DOBJECT_PRINT -DVERIFY_HEAP -DV8_TRACE_MAPS -DV8_ENABLE_ALLOCATION_TIMEOUT -DV8_ENABLE_FORCE_SLOW_PATH -DV8_INTL_SUPPORT -DENABLE_HANDLE_ZAPPING -DV8_USE_SNAPSHOT -DV8_USE_EXTERNAL_STARTUP_DATA -DV8_CONCURRENT_MARKING -DV8_CHECK_MICROTASKS_SCOPES_CONSISTENCY -DV8_EMBEDDED_BUILTINS -DV8_ENABLE_CHECKS -DV8_DEPRECATION_WARNINGS -DV8_IMMINENT_DEPRECATION_WARNINGS -DV8_TARGET_ARCH_X64 -DDEBUG -DDISABLE_UNTRUSTED_CODE_MITIGATIONS -DUSING_V8_SHARED -DV8_ENABLE_CHECKS -DV8_DEPRECATION_WARNINGS -DV8_IMMINENT_DEPRECATION_WARNINGS -DU_USING_ICU_NAMESPACE=0 -DU_ENABLE_DYLOAD=0 -DUSE_CHROMIUM_ICU=1 -DICU_UTIL_DATA_IMPL=ICU_UTIL_DATA_FILE -DUCHAR_TYPE=uint16_t -DUSING_V8_BASE_SHARED -DUSING_V8_PLATFORM_SHARED
include_dirs = -I../.. -Igen -I../.. -Igen -I../../include -Igen/include -I../../third_party/icu/source/common -I../../third_party/icu/source/i18n -I../../include
cflags = -fno-strict-aliasing --param=ssp-buffer-size=4 -fstack-protector -Wno-builtin-macro-redefined -D__DATE__= -D__TIME__= -D__TIMESTAMP__= -funwind-tables -fPIC -B../../third_party/binutils/Linux_x64/Release/bin -pthread -fcolor-diagnostics -fmerge-all-constants -Xclang -mllvm -Xclang -instcombine-lower-dbg-declare=0 -no-canonical-prefixes -fcomplete-member-pointers -m64 -march=x86-64 -Wall -Werror -Wextra -Wimplicit-fallthrough -Wthread-safety -Wno-missing-field-initializers -Wno-unused-parameter -Wno-c++11-narrowing -Wno-unneeded-internal-declaration -Wno-undefined-var-template -Wno-ignored-pragma-optimize -fno-omit-frame-pointer -g2 -gsplit-dwarf -ggnu-pubnames -gcolumn-info -fsanitize=address -fsanitize-address-use-after-scope -fsanitize-blacklist=../../tools/memory/asan/blacklist.txt -fvisibility=hidden -Wheader-hygiene -Wstring-conversion -Wtautological-overlap-compare -Wmissing-field-initializers -Wextra-semi -Winconsistent-missing-override -Wunreachable-code -Wshorten-64-to-32 -O2 -fno-ident -fdata-sections -ffunction-sections
cflags_cc = -Wno-undefined-bool-conversion -Wno-tautological-undefined-compare -std=c++14 -fno-exceptions -fno-rtti -nostdinc++ -isystem../../buildtools/third_party/libc++/trunk/include -isystem../../buildtools/third_party/libc++abi/trunk/include --sysroot=../../build/linux/debian_sid_amd64-sysroot -fvisibility-inlines-hidden
label_name = v8_shell
target_out_dir = obj
target_output_name = v8_shell

build obj/v8_shell/shell.o: cxx ../../samples/shell.cc || obj/generate_bytecode_builtins_list.stamp obj/run_torque.stamp obj/v8_dump_build_config.stamp obj/src/inspector/protocol_generated_sources.stamp obj/third_party/icu/icudata.stamp

build ./v8_shell: link obj/v8_shell/shell.o obj/build/config/sanitizers/liboptions_sources.a | ./libv8.so.TOC ./libv8_libbase.so.TOC ./libv8_libplatform.so.TOC ./libicui18n.so.TOC ./libicuuc.so.TOC ./libc++.so.TOC || obj/build/win/default_exe_manifest.stamp obj/v8_dump_build_config.stamp obj/build/config/executable_deps.stamp
  ldflags = -pie -Wl,--fatal-warnings -fPIC -Wl,-z,noexecstack -Wl,-z,relro -fuse-ld=lld -Wl,--color-diagnostics -m64 -Werror -Wl,--gdb-index -rdynamic -nostdlib++ --sysroot=../../build/linux/debian_sid_amd64-sysroot -L../../build/linux/debian_sid_amd64-sysroot/usr/local/lib/x86_64-linux-gnu -Wl,-rpath-link=../../build/linux/debian_sid_amd64-sysroot/usr/local/lib/x86_64-linux-gnu -L../../build/linux/debian_sid_amd64-sysroot/lib/x86_64-linux-gnu -Wl,-rpath-link=../../build/linux/debian_sid_amd64-sysroot/lib/x86_64-linux-gnu -L../../build/linux/debian_sid_amd64-sysroot/usr/lib/x86_64-linux-gnu -Wl,-rpath-link=../../build/linux/debian_sid_amd64-sysroot/usr/lib/x86_64-linux-gnu -fsanitize=address -fsanitize-address-use-after-scope -pie -Wl,-rpath-link=. -Wl,--disable-new-dtags -Wl,-rpath=\$$ORIGIN/. -Wl,-rpath-link=. -Wl,-O2 -Wl,--gc-sections -Wl,-u_sanitizer_options_link_helper -fsanitize=address -fsanitize-address-use-after-scope
  libs =  -L . -ldl -lpthread -lrt
  output_extension =
  output_dir = .
  solibs = ./libtester.so ./libv8.so ./libv8_libbase.so ./libv8_libplatform.so ./libicui18n.so ./libicuuc.so ./libc++.so

代码链接在这里: https://github.com/v8/v8/blob/master/samples/shell.cc

我所做的只是添加一个返回 std::string 的测试库,这是从 shell.cc 调用的。

这个测试库是用

编译的
clang++ -shared -o libtester tester.cpp  -fPIC -L .    -lpthread 

这会产生未定义的符号错误。

我可以通过强制使用 v8 似乎使用的 libc++ 来编译它,但是在运行时我会收到一大堆核心转储和其他错误,所以我认为这不是一个合法的修复。

clang++ -shared -o libtester.so tester.cpp  -fPIC -std=c++11 -L .    -stdlib=libc++ -lpthread 

示例代码sn-p:

tester.cpp

std::string myFunction() {
std::string newstring; 
 // do something
return newstring;
}

shell.cc

std::string test = myFunction();
cout << test;

更新请参阅这些帖子以进一步记录它C++ Undefined symbol related to std::string in static lib Can't link libFuzzer.a using clang with libc++

除了使用 libstdc++ 而不是 libc++ 之外,没有真正的答案,但这并不是将整个项目(如 v8)转换为 libstdc++ 的真正选择

【问题讨论】:

  • 你不应该在问题中包含你的编译命令吗?
  • 我已经添加了编译命令。谢谢
  • 我目前正在阅读这篇文章gernotklingler.com/blog/… 试图提供帮助。
  • 符号myFunctionstd::string无关。 tester 对于 C++ 编译器来说不是一个好的输入文件名。
  • myfunction 被定义为 std::string myFunction()。测试人员只是打错了SO帖子,应该是tester.cpp,

标签: c++ string g++ clang v8


【解决方案1】:

您用于 V8 的构建配方使用 -D_LIBCPP_ABI_UNSTABLE -D_LIBCPP_ABI_VERSION=Cr -DCR_LIBCXX_REVISION=349080 -DCR_LIBCXXABI_REVISION=347903 构建它。根据libc++ ABI stability,这些宏会影响库 ABI。您的单独编译仅使用 -stdlib=libc++,因此它不会启用不兼容的 ABI。

如果您使用不同的方法构建 V8,您可能会获得更好的结果,即使用系统 C++ 标准库及其默认(稳定)ABI。

【讨论】:

  • 这些是用 gn (v8.dev/docs/build-gn) 自动生成的,你知道我是如何选择标准库的吗?我在文档中找不到任何对它的引用。
【解决方案2】:

这是用libstdc++编译v8的解决方案。

输入以下命令

gn args out/stdc

将以下参数添加到 args 文件中:

is_clang = true
use_custom_libcxx_for_host=false
use_custom_libcxx=false
libcxx_abi_unstable=false

构建
 ninja -C out/stdc

【讨论】:

    猜你喜欢
    • 2012-09-14
    • 2012-01-19
    • 2012-05-08
    • 1970-01-01
    • 2013-03-14
    • 2017-11-16
    • 2022-01-16
    • 1970-01-01
    • 2018-04-27
    相关资源
    最近更新 更多