Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I deploy a C++11 program (with dependencies) on CentOS 6, whose GCC is C++03?

GCC is great with ABI-compatibility as long as you use the same C++ standard [1].

But it strikes me that if a shared library compiled by GCC 4.3 in C++03 mode exposes, say, a std::string, this is going to be a different std::string than that understood by an executable compiled by GCC 4.8 in C++11 mode.

The reason I ask is that I am planning to deploy a program compiled by GCC 4.8 in C++11 mode on CentOS 6, whose maximum packaged GCC is 4.3... and some of the shared libraries (be they third-party C++ libraries or more system-level stuff) will presumably therefore all be C++03. But if that were the case, we'd never be able to deploy any C++11 programs on older Linux distributions, which seems unlikely.

Am I naive to think there might be a problem here? And, if there is, how can I resolve it?

like image 274
Lightness Races in Orbit Avatar asked Aug 04 '16 14:08

Lightness Races in Orbit


People also ask

Does GCC support C ++ 11?

GCC provides experimental support for the 2011 ISO C++ standard. This support can be enabled with the -std=c++11 or -std=gnu++11 compiler options; the former disables GNU extensions. As of GCC 4.8.

Which version of C++ does GCC support?

GCC supports the original ISO C++ standard published in 1998, and the 2011, 2014, 2017 and mostly 2020 revisions. The original ISO C++ standard was published as the ISO standard (ISO/IEC 14882:1998) and amended by a Technical Corrigenda published in 2003 (ISO/IEC 14882:2003).

How to install GCC and development tools on CentOS 7?

Command to install GCC and Development Tools on a CentOS / RHEL 7 server. Type the following yum command as root user: # yum group install "Development Tools". OR. $ sudo yum group install "Development Tools". If above command failed, try: # yum groupinstall "Development Tools". Sample outputs:

Can I install libstdc++-4 on CentOS 6?

Can I install libstdc++-4.8.5-28.el7.i686 on CentOS 6.10 if not any other posibilities ? Build your program on CentOS 6 if that's where you want it to run. You cannot get the newer glibc etc onto CentOS 6 and if you could, it would be way way simpler just to update to CentOS 7 in the first place.

What version of C++ do I need to compile my source code?

Compile your source code on 6.10. You can choose from five devtoolsets offering gcc 4.8.2, 4.9.2, 5.3.1, 6.3.1, and 7.3.1, respectively. That should be enough C++11. The "devtoolsets" come from Software Collections (SCL). That pulls in the tools to do a build.

Is there a warranty for installing GNU/C++ on CentOS 7?

There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Create a file called foo.c as follows: $ ./foo Hello World! You learned how to install GNU c, c++ compilers and releated tools on a CentOS or Red Hat Enterprise Linux version 7.x.


2 Answers

There is a wonderful page on this matter: https://gcc.gnu.org/wiki/Cxx11AbiCompatibility

In short, C++11 in gcc is mostly ABI compatible with c++98, but there are a couple of mismatches. Page above lists all of those.

To alleviate the issue I can suggest following approach:

  • Clearly identify all your dependencies which are C++ libraries. You usually do not have too many of them - boost comes to mind first, do you have anything else?
  • Than you check if the symbols your app needs are in the list of broken ABI (see above). If they are not, you are in the clear.
  • If they are, you recompile the lib, and either distribute it as shared lib together with your app (playing with Rpath flags to make sure your app loads your version) or link statically against it.

Just in case, you might as well link statically against libstdc++.

like image 138
SergeyA Avatar answered Oct 12 '22 03:10

SergeyA


Actually, you can distribute a program compiled with a newer g++ compiler on a vanilla CentOS 6 platform. There are several ways to do this: The easiest is to use the DevToolset 3, which will give you g++ 4.9.2 (the dev toolset 2 will give you gcc 4.8.2). Then, just compile your application with this g++. When distributing your software, you need to make sure to also ship the libstdc++.so that is being shipped with g++ 4.9. Either set the LD_LIBRARY_PATH so it gets picked up on startup, or set the RPATH to tell your executable where to look first for libraries.

Essentially, you can do this also with newer compilers, but then you first need to compile the compiler itself. If you don't want to compile a compiler first, go with a respective dev toolset and you should be fine.

Yes, you can also try to statically link libstdc++.a. Search for the option -static-libstdc++:

When the g++ program is used to link a C++ program, it normally automatically links against libstdc++. If libstdc++ is available as a shared library, and the -static option is not used, then this links against the shared version of libstdc++. That is normally fine. However, it is sometimes useful to freeze the version of libstdc++ used by the program without going all the way to a fully static link. The -static-libstdc++ option directs the g++ driver to link libstdc++ statically, without necessarily linking other libraries statically.

But if you statically link, you will not get any security updates etc. Granted, you will not get the updates, if you ship libstdc++.so on your own as well, but incremental updates maybe easier.

And with respect to running your application: The rule of thumb is: Compile on the oldest platform you need to support, then your binaries (with self-shipped libstdc++ and other required libs) will likely work also on newer versions. That is, if you compile on CentoOS 6, and it works, then you can expect it to also work on CentOS 7. On a related subject, this is exactly the reason why for instance AppImage and related solutions recommend to build on an old system.

like image 33
dhaumann Avatar answered Oct 12 '22 05:10

dhaumann