C++ -static-lxyz和-l:libxyz.a之间的差异

C++ -static-lxyz和-l:libxyz.a之间的差异,c++,linux,C++,Linux,对不起,我的英语不好。 我使用的是视觉代码,我的项目是C++,-L/LIBS/CURL/LIB只包含LIBCURL.A,即静态库 当我使用此链接选项构建项目时,一切正常: 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:libcryp

对不起,我的英语不好。 我使用的是视觉代码,我的项目是C++,-L/LIBS/CURL/LIB只包含LIBCURL.A,即静态库 当我使用此链接选项构建项目时,一切正常:

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是否有添加其他lib的隐式链接

  • 默认情况下,gcc或g++将链接到一些标准库。这些库的列表依赖于体系结构,但是您可以找到libc libgcc和其他一些库。要查看用于链接的库列表,可以添加
    -v
    选项。您将看到传递给collect2的库列表。因为您是为x86_64编译的,所以列表可能是(libc、libgcc和libgcc_s)

    使用static选项时,将强制链接器使用所有库的静态版本,包括使用-l选项提供的库和默认库

    在第二种情况下,为链接器提供库的静态版本,但对于其他库,将采用共享版本
    如果同时找到静态库和共享库,则链接器会优先选择与共享库链接,除非使用-static选项。
    from

    最后一件事。如果使用静态选项,crt文件也会有所不同。您也可以使用
    -v
    选项查看它


    如果您想精确控制应该使用哪个库,可以使用选项
    nostlib
    。使用此选项时,编译器在链接时不使用标准系统启动文件或库。没有启动文件,只有您指定的库被传递到链接器,指定系统库链接的选项(如-static libgcc或-shared libgcc)被忽略。

    如果您希望构建使用这些库的可执行程序,你可以试试这个:
    LDLIBS=-lsqlite3-lcurl-lssl-lcrypto-lz-ldl-lpthread
    但是我想要静态链接,对于没有interner的pc,只在案例1```-lcurl-lssl-lcrypto-lsqlite3-lpthread-ldl-lz-lstdc++-lm--start group-lgcc-lgcc_eh-lc-end group``上使用LANoutput,然后我修复makefile,比如,我得到的输出是:``-l:libcurl.a-l:libssl.a-l:libcrypto.a-l:libsqlite3.a-l:libpthread.a-l:libz.a-l:libm.a--start group-l:libgcc.a-l:libgcc_eh.a-l:libc.a--end group-Bstatic-lstdc++-bddynamic-lm lgcc_s-lgcc-lc lgcc-lgcc_-s-lgcc``它仍然使用动态链接,所以有没有办法强迫g++静态链接而不使用它吗-静态的-静态强制所有库静态链接,我不想这样。我更新了我的答案,如果你想准确地控制你使用的是什么,我猜Nostlib就是你想要的。