如何使用通过libc ++ libstdc ++混合编译的链接库 [英] How to use linked libraries compiled with libc++ libstdc++ mixed

查看:443
本文介绍了如何使用通过libc ++ libstdc ++混合编译的链接库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图编写一个脚本,该脚本使用使用clang编译的库和使用G ++编译的另一个库,但出现以下错误:



ld.lld:错误:未定义符号:myFunction()



其中(根据此 C ++中string和char []类型之间的差异)显然是由于std :: string表示不同的意思



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



更多信息



我正在使用 ninja -C out / debug

编译v8_shell

 定义= -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 =铬-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_TY PED_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_V._GENI。 I ../../ include -Igen / include -I ../../ third_party / icu / source / common -I ../../ third_party / icu / source / i18n -I ../../包括
cflags = -fno-strict-aliasing --param = ssp-buff er-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-parameters -Wno-c ++ 11-narrowing-Wno-unneeded-internaldeclaration -Wno-undefined-var-template -Wno忽略的编译指示优化-fno-省略帧指针-g2 -gsplit-dwarf -ggnu-pubnames -gcolumn-info -fsanitize =地址-fsanitize-地址使用后范围检查-fsanitize-blacklist =。 ./../tools/memory/asan/blacklist.txt -fvisibility = hidden -Wheader-卫生-Wstring转换-Wutautologic-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-tautologic-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-隐藏的
label_name = v8_shell
target_out_dir = obj
target_output_name = v8_shell

构建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:链接obj /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,-致命警告-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-禁用新的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 

示例代码段:



tester.cpp

  std :: string myFunction(){
std :: string newstring;
//做某事
返回newstring;
}

shell.cc

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

更新请参阅这些帖子,以进一步记录该文档 C ++与静态lib中的std :: string相关的未定义符号
无法使用clang和libc ++链接libFuzzer.a



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

解决方案

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



键入以下命令

  gn args out / stdc 

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

  is_clang = true 
use_custom_lib_xx_for_host = false
use_custom_libcxx = false
libcxx_abi_unstab le = false

使用



<$ p $构建p> 忍者-C out / stdc


I am trying to write a script that uses a library compiled with clang and another library compiled with G++, I get the following error:

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

Which (according to this Difference between string and char[] types in C++) is apparently due to std::string meaning different things in different versions of G++ and clang.

Does this mean I have to rewrite the Makefile of my target library to use the same version of G++/Clang? Because that seems like an awful amount of effort just to link a prewritten library, and I think I must be missing something here.

More info

I am compiling v8_shell using ninja -C out/debug

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

Link to code is here: https://github.com/v8/v8/blob/master/samples/shell.cc

All I have done is add a test library that returns a std::string and this is called from shell.cc.

This test library is compiled with

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

Which yields undefined symbol errors.

I can get it to compile by forcing libc++ that v8 seems to use, but then I get a whole host of core dumps and other errors at runtime, so I don't think thats a legitimate fix.

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

Example code snippet:

tester.cpp

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

shell.cc

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

Update please see these posts that document it further C++ Undefined symbol related to std::string in static lib Can't link libFuzzer.a using clang with libc++

Theres no real answer apart from use libstdc++ rather than libc++ for everything, but thats not really an option to convert an entire project like v8 to libstdc++

解决方案

This is the solution for compiling v8 with libstdc++.

Type the following command

gn args out/stdc

Add the following arguments to the args file:

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

Build with

 ninja -C out/stdc

这篇关于如何使用通过libc ++ libstdc ++混合编译的链接库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆