简体   繁体   English

g ++:静态链接不起作用

[英]g++: Static linking not working

I'm trying to link the standard library in my project statically. 我正在尝试在项目中静态链接标准库。 I've tried several variations of static link options from various threads on SO and other sites ( http://www.trilithium.com/johan/2005/06/static-libstdc/ ), but none of them have yielded the desired results. 我已经尝试过SO和其他网站( http://www.trilithium.com/johan/2005/06/static-libstdc/ )上的各种线程的静态链接选项的几种变体,但没有一个产生预期的结果。

Here's my current flag setup: 这是我当前的标志设置:

g++ -Wall -g -static-libgcc -static-libstdc++ -Iinclude -std=c++11

I've also tried: 我也尝试过:

g++ -Wall -g -Wl,-Bstatic -lstdc++ -std=c++11

With either of these variations, the output of ldd still is: 无论使用哪种变体, ldd的输出仍然是:

linux-gate.so.1 => (0xb77b1000)
libstdc++.so.6 => /lib/libstdc++.so.6 (0x4e085000)
libm.so.6 => /lib/libm.so.6 (0x4daf0000)
libgcc_s.so.1 -> /lib/libgcc_s.s0.1 (0x4dd1f000)
libc.so.6 => /lib/libc.so.6 (0x4d90a000)
/lib/ld-linux.so.2 (0x4d8e7000)

Running file also tells me that it's a dynamically linked binary. 运行file还告诉我这是一个动态链接的二进制文件。 I've tried this on multiple distros, including Fedora 18, Ubuntu 12.04, and CentOS, but to no avail. 我已经在多个发行版(包括Fedora 18,Ubuntu 12.04和CentOS)上进行了尝试,但无济于事。

g++ -Wall -g -static-libgcc -static-libstdc++ -std=c++0x hello.cpp

with this output from ldd 来自ldd的输出

linux-vdso.so.1 =>  (0x00007fffef928000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fbe12439000)
/lib64/ld-linux-x86-64.so.2 (0x00007fbe12822000)

does the job under my Ubuntu 12.04, also I doubt that you are using the gcc/g++ version supported under Ubuntu 12.04 because it's g++ 4.6 and g++ 4.6 doesn't offer the -std=c++11 flag. 可以在我的Ubuntu 12.04下完成这项工作,我也怀疑您使用的是Ubuntu 12.04支持的gcc / g ++版本,因为它是g ++ 4.6,而g ++ 4.6不提供-std=c++11标志。

g++ -std=c++0x -static hello.cpp

this also works as expected compiling everything into a single static object. 这也可以按预期工作,将所有内容编译到单个静态对象中。

the source of my hello.cpp 我的hello.cpp的来源

#include <iostream>

int main(int argc, char* argv[])
{
  [](void){std::cout << "hello" << std::endl;}();
  return(0);
}

I think that you are playing with different compilers or with compilers that are not supported by your platform, document yourself about your compiler version, this flags work, the problem is probably your installation or the settings in your compiler. 我认为您正在使用不同的编译器或平台不支持的编译器,将自己的编译器版本记录下来,此标记有效,问题可能出在您的安装或编译器中的设置。

I've tried to link -static-libgcc -static-libstdc++ with others static libraries: protobuf, pthread, rt and every time I had a different problem. 我试图将-static-libgcc -static-libstdc ++与其他静态库链接:protobuf,pthread,rt以及每次遇到其他问题时。

The solution I found was to use an external tool Ermine and solves all my problems. 我找到的解决方案是使用外部工具Ermine并解决我的所有问题。 based on: Statifier 基于: 量词

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM