Why does g++ on Windows statically link the standard library by default? - c++

I have a C++ program which makes use of some C++ standard libraries such as iostream, vector, sstream and so on, and is only about 120 lines of code. By building it on Debian I get a 25KB executable, while when I build it on Windows 11 (both of them with just "g++ main.cpp") I normally get a 3026KB .exe. If instead I type "--shared-libstdc++" alongside g++ on Windows, I get a smaller .exe, merely 290KB.
My question is, why does compiling and linking in a Linux environment automatically do this while on Windows I have to specify it? Is there a drawback to dynamically linking the standard C++ library on Windows that G++ is aware of?
On Windows, I have: gcc version 10.3.0 (tdm64-1)
On Debian, I have: gcc version 11.3.0 (Debian 11.3.0-3)

Related

New version of g++ with an older version of libstdc++

I am running Linux CentOS 7.3 which comes with g++ 4.8.5. I would like to use g++ 7.1.0 to compile some C++ software. But the C++ program compiled with g++ 7.1.0 is using the headers from the standard library that comes with g++ 7.1.0 and the libstdc++.so which also comes with it which make things really painful.
Is there a way to use g++ 7.1.0 and still use the standard library (both headers and libstdc++.so) from g++ 4.8.5 installed with CentOS?
Is there a way to use g++ 7.1.0 and still use the standard library (both headers and libstdc++.so) from g++ 4.8.5 installed with CentOS?
Don't do that (the ABI of libstdc++ from GCC 4.8 & GCC 7 are likely to be different). Instead consider perhaps linking the C++ standard library (from GCC 7.1) statically (and other libraries dynamically, notably those in C including libc.so, not C++).
BTW, how did you get  g++-7.1? You could consider compiling GCC 7 (from its source code) on your CentOS 7 (or get some packaged version of it), then you'll have the right libstdc++
Read more about shared libraries, e.g. read Drepper's paper How To Write Shared Libraries and learn more about the -rpath option passed to ld (often using -Wl,-rpath to g++).
The libstdc++ ABI changed between gcc4 and gcc5 so that's not going to work. If you are using gcc7 you should install libstdc++ 7. You can have both versions installed at the same time
Developer Toolset is designed for this scenario, but it is currently at GCC version 6:
https://www.softwarecollections.org/en/scls/rhscl/devtoolset-6/
Its C++ compiler has been configured in such a way that the programs it compiles are linked dynamically against the system libstdc++, using a backwards-compatible ABI, and only the library code for new C++ features not yet supported by the system library is linked statically. This gives maximum compatibility and allows compiled applications to run without DTS.

Distribute C++ binaries across linux

I would like to distribute C++ binary that is created by using Eclipse ++ IDE, with the following settings:
Cross GCC Compiler: gcc -std=c++17
Cross G++ Compiler: g++ -std=c++17
Tool Chain settings:
Cross GCC Compiler
Cross G++ Compiler
Cross GCC Linker
Cross G++ Linker
Cross GCC Archiver
Cross GCC Assembler
I am compiling with boost::serilization and have connect that in Linker. The binary is created on Ubuntu 16.04. I tried to run the binary on Ubuntu 14.04, and it give the error:
error while loading shared libraries: libboost_serialization.so.1.58.0: cannot open shared object file: No such file or directory.
I have also installed libboost on the system by using
sudo apt-get install libboost-program-options-dev
But, it gives me the same error.
I think with the above Eclipse IDE setting, it should work in most of the Linux platforms. Is my presumption of its working on most of the Linux platforms wrong??.
When you build your program you link it with specific versions of the shared libraries. On older releases of e.g. Ubuntu those newer versions of the libraries doesn't exist.
If you want to copy the executable between different systems you need to link statically. That means all libraries are linked into the executable so it doesn't rely on shared libraries. It will make the executable bigger of course.

Relationship between gcc, g++, cygwin, and wingw?

I know for my class, I had to install cygwin to get my Netbeans IDE running, but I see options during setup for both g++ and gcc and I am not sure if they are the same thing or not, and where does wingw? is it another compiler, and if so why choose on over the other?
g++ and gcc are the gnu C++ and C compiler respectively. They're really the same compiler with different flags though.
MinGW is "Minimalist Gnu for Windows". It's a port of the gnu compiler to run on Windows.
Cygwin is another port of the gnu compiler (and various other utilities) to Windows. More accurately (IMO, anyway), it's leaving the compiler/programs running on POSIX, and porting a POSIX layer to run on Windows.
As to choosing between them: if you're running Linux, you probably want ot just get a package of gcc/g++ for the distro you're using.
If you're running Windows, it'll depend on your intent. Cygwin works well for porting existing Linux/POSIX code to Windows. If, however, you plan to write code, and just want a compiler, I'd go for MinGW instead.
One other note: the MinGW at MinGW.org hasn't been updated in years. If you decide to go with MinGW, I'd advise getting it from nuwen.net instead (it's updated quite regularly).
gcc will compile: .c/.cpp files as C and C++ respectively.
g++ will compile: .c/.cpp files but they will all be treated as
C++ files.
Also if you use g++ to link the object files it automatically
links in the std C++ libraries (gcc does not do this).
gcc compiling C files has less predefined macros.
For wingw, did you mean mingw? Because MinGW is for compatibility with Windows. MinGW uses GCC/G++, and MinGW is not a compiler, it's basically a stripped version of Cygwin that uses MS libs wherever possible.
Depends upon what you are building;
gcc is for C programs.
g++ is for C++ programs.
I've not heard of wingw.

SFML 2.1 Code::Blocks Static linking not working,

I am new to the idea of linking libraries in c++, and am trying to get SFML to work with C++. I am on Windows 7 64-bit and am using Code Blocks. I have downloaded the 32-bit mingw GCC SFML DW2 version of SFML. I followed this tutorial: http://www.sfml-dev.org/tutorials/2.0/start-cb.php to set up code blocks. I am staticly linking the libraries. When I press the "Build" button, I get whole host of errors, with undefined reference to '__Unwind_Resume' and '__gxx_personality_v0'. I have looked up this issue, and have only found command line fixes, using g++ instead of gcc. I cannot do this within the codeblocks UI. Can someone give me a hand?
The version of the compiler shipped with code::blocks is the 32-bit 4.7 TDM-SJLJ one, so you need the version of SFML compiled with the same compiler (GCC 4.7 TDM (SJLJ) - 32 bits)
If you want to use the 64-bit version of the library you need to install the 64-bit version of the compiler, available here
change the linker program in settings->compiler->toolchains to g++.exe or mingw-g++.exe

Minimal GCC C++ Compiler

I'm trying to minimize the resources required to compile and run c++ code with GCC. I downloaded DevC++'s portable version but I'm looking more towards what files do I need to ONLY run gcc via command prompt and compile against all standard libraries. I'm not interested in Windows applications, only command prompt.
Is there an already stripped version of GCC out there? And if not, would anyone be able to lend me a hand
*I know of CygWin, and MinGW already, I'm looking for the bare minimals to using the cpp compiler for GCC. Like Tiny C where the entire functioning compiler and libraries is under 200 kilobytes, I'm looking to emulate that workflow with a cpp compiler.
MinGW
MinGW, a contraction of "Minimalist GNU for Windows", is a minimalist development environment for native Microsoft Windows applications.
Primarily intended for use by developers working on the native MS-Windows platform, [...] MinGW includes:
A port of the GNU Compiler Collection (GCC), including C, C++, ADA and Fortran compilers;
GNU Binutils for Windows (assembler, linker, archive manager)
A graphical and a command-line installer for MinGW and MSYS deployment on MS-Windows