gpt4 book ai didi

c++ - -static -lxyz和-l:libxyz.a之间的区别

转载 作者:行者123 更新时间:2023-12-03 07:22:32 26 4
gpt4 key购买 nike

对不起,我的英语不好。
我正在使用可视代码,我的项目是c++,-L./libs/curl/lib仅包含libcurl.a,即静态lib
当我使用此链接选项构建项目时,一切正常:

LDFLAGS = -static -L./libs/curl/lib -lcurl -lssl -lcrypto -lsqlite3 -lpthread -ldl -lz
但是,当我使用此链接选项时:
LDFLAGS = -L./libs/curl/lib -l:libcurl.a -l:libssl.a -l:libcrypto.a -l:libsqlite3.a -l:libpthread.a -l:libdl.a -l:libz.a
我收到此错误:
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/libpthread.a(pthread_create.o): In function `allocate_stack':
/build/glibc-2ORdQG/glibc-2.27/nptl/allocatestack.c:526: undefined reference to `_dl_stack_flags'
/build/glibc-2ORdQG/glibc-2.27/nptl/allocatestack.c:652: undefined reference to `_dl_stack_flags'
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/libpthread.a(elision-lock.o): In function `do_set_elision_enable':
/build/glibc-2ORdQG/glibc-2.27/nptl/../sysdeps/unix/sysv/linux/x86/elision-conf.c:67: undefined reference to `_dl_x86_cpu_features'
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/libpthread.a(nptl-init.o): In function `__pthread_initialize_minimal_internal':
/build/glibc-2ORdQG/glibc-2.27/nptl/nptl-init.c:294: undefined reference to `_dl_cpuclock_offset'
/build/glibc-2ORdQG/glibc-2.27/nptl/nptl-init.c:429: undefined reference to `_dl_pagesize'
/build/glibc-2ORdQG/glibc-2.27/nptl/nptl-init.c:438: undefined reference to `_dl_pagesize'
/build/glibc-2ORdQG/glibc-2.27/nptl/nptl-init.c:454: undefined reference to `_dl_init_static_tls'
/build/glibc-2ORdQG/glibc-2.27/nptl/nptl-init.c:456: undefined reference to `_dl_wait_lookup_done'
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/libpthread.a(nptl-init.o): In function `__pthread_get_minstack':
/build/glibc-2ORdQG/glibc-2.27/nptl/nptl-init.c:475: undefined reference to `_dl_pagesize'
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/libdl.a(dlopen.o): In function `dlopen':
(.text+0x5): undefined reference to `__dlopen'
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/libdl.a(dlclose.o): In function `dlclose':
(.text+0x1): undefined reference to `__dlclose'
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/libdl.a(dlsym.o): In function `dlsym':
(.text+0x5): undefined reference to `__dlsym'
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/libdl.a(dlerror.o): In function `dlerror':
(.text+0x1): undefined reference to `__dlerror'
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/libdl.a(dladdr.o): In function `dladdr':
(.text+0x1): undefined reference to `__dladdr'
所以我的问题是:
  • -static -lxyz和-l:libxyz.a链接选项之间有什么区别?
  • -static是否具有隐式链接以添加其他库?
  • 最佳答案

    默认情况下,gcc或g++将链接到某些标准库。这些库的列表取决于体系结构,但是您可以找到libc libgcc和其他一些库。要查看用于链接的库列表,可以添加-v选项。您将看到传递给collect2的库的列表。由于您要针对x86_64进行编译,因此列表可能是(libc,libgcc和libgcc_s)。
    使用static选项时,将强制链接器使用所有库的静态版本,这些库是通过-l选项提供的,而默认库是。
    在第二种情况下,您为链接器提供了库的静态版本,但是对于其他库,将采用共享版本。来自https://gcc.gnu.org/onlinedocs/gcc/Link-Options.html#Link-OptionsIf both static and shared libraries are found, the linker gives preference to linking with the shared library unless the -static option is used.
    最后一件事。如果使用static选项,则crt文件也将有所不同。您也可以使用-v选项查看它。
    如果您想精确控制应该使用哪个库,可以使用nostdlib选项。使用此选项时,编译器在链接时不要使用标准的系统启动文件或库。没有启动文件,只有指定的库才传递给链接器,并且指定系统库链接的选项(例如-static-libgcc或-shared-libgcc)将被忽略。

    关于c++ - -static -lxyz和-l:libxyz.a之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64681796/

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