How to compile C++ using MinGW on Windows? - c++

I'm getting the following error
cannot find -lstdc++
collect2.exe: error: ld returned 1 exit status
it's a simple Hello World program.
g++ version returns
g++ (MinGW.org GCC Build-2) 9.2.0
g++ -v <filename.cpp> returns
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/mingw32/9.2.0/lto-wrapper.exe
Target: mingw32
Configured with: ../src/gcc-9.2.0/configure --build=x86_64-pc-linux-gnu --host=mingw32 --target=mingw32 --disable-win32-registry --with-arch=i586 --with-tune=generic --enable-static --enable-shared --enable-threads --enable-languages=c,c++,objc,obj-c++,fortran,ada --with-dwarf2 --disable-sjlj-exceptions --enable-version-specific-runtime-libs --enable-libgomp --disable-libvtv --with-libiconv-prefix=/mingw --with-libintl-prefix=/mingw --enable-libstdcxx-debug --disable-build-format-warnings --prefix=/mingw --with-gmp=/mingw --with-mpfr=/mingw --with-mpc=/mingw --with-isl=/mingw --enable-nls --with-pkgversion='MinGW.org GCC Build-2'
Thread model: win32
gcc version 9.2.0 (MinGW.org GCC Build-2)
COLLECT_GCC_OPTIONS='-v' '-shared-libgcc' '-mtune=generic' '-march=i586'
c:/mingw/bin/../libexec/gcc/mingw32/9.2.0/cc1plus.exe -quiet -v -iprefix c:\mingw\bin\../lib/gcc/mingw32/9.2.0/ hw.cpp -quiet -dumpbase hw.cpp -mtune=generic -march=i586 -auxbase hw -version -o C:\Users\move78~1\AppData\Local\Temp\ccQus9Kp.s
GNU C++14 (MinGW.org GCC Build-2) version 9.2.0 (mingw32)
compiled by GNU C version 9.2.0, GMP version 6.1.2, MPFR version 4.0.2, MPC version 1.1.0, isl version isl-0.21-GMP
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "c:\mingw\bin\../lib/gcc/mingw32/9.2.0/../../../../mingw32/include"
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/9.2.0/include/c++"
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/9.2.0/include/c++/mingw32"
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/9.2.0/include/c++/backward"
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/9.2.0/include"
ignoring duplicate directory "/mingw/lib/gcc/mingw32/9.2.0/../../../../include"
ignoring duplicate directory "c:/mingw/lib/gcc/../../include"
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/9.2.0/include-fixed"
ignoring nonexistent directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/9.2.0/../../../../mingw32/include"
ignoring duplicate directory "/mingw/include"
#include "..." search starts here:
#include <...> search starts here:
c:\mingw\bin\../lib/gcc/mingw32/9.2.0/include/c++
c:\mingw\bin\../lib/gcc/mingw32/9.2.0/include/c++/mingw32
c:\mingw\bin\../lib/gcc/mingw32/9.2.0/include/c++/backward
c:\mingw\bin\../lib/gcc/mingw32/9.2.0/include
c:\mingw\bin\../lib/gcc/mingw32/9.2.0/../../../../include
c:\mingw\bin\../lib/gcc/mingw32/9.2.0/include-fixed
End of search list.
GNU C++14 (MinGW.org GCC Build-2) version 9.2.0 (mingw32)
compiled by GNU C version 9.2.0, GMP version 6.1.2, MPFR version 4.0.2, MPC version 1.1.0, isl version isl-0.21-GMP
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: cb31672760a9c1b42d1ddb86616116cd
COLLECT_GCC_OPTIONS='-v' '-shared-libgcc' '-mtune=generic' '-march=i586'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/as.exe -v -o C:\Users\move78~1\AppData\Local\Temp\cctjBjko.o C:\Users\move78~1\AppData\Local\Temp\ccQus9Kp.s
GNU assembler version 2.32 (mingw32) using BFD version (GNU Binutils) 2.32
COMPILER_PATH=c:/mingw/bin/../libexec/gcc/mingw32/9.2.0/;c:/mingw/bin/../libexec/gcc/;c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/
LIBRARY_PATH=c:/mingw/bin/../lib/gcc/mingw32/9.2.0/;c:/mingw/bin/../lib/gcc/;c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/lib/;c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../
COLLECT_GCC_OPTIONS='-v' '-shared-libgcc' '-mtune=generic' '-march=i586'
c:/mingw/bin/../libexec/gcc/mingw32/9.2.0/collect2.exe -plugin c:/mingw/bin/../libexec/gcc/mingw32/9.2.0/liblto_plugin-0.dll -plugin-opt=c:/mingw/bin/../libexec/gcc/mingw32/9.2.0/lto-wrapper.exe -plugin-opt=-fresolution=C:\Users\move78~1\AppData\Local\Temp\ccxROszG.res -plugin-opt=-pass-through=-lmingw32 -plugin-opt=-pass-through=-lmingwex -plugin-opt=-pass-through=-lmingw32 -plugin-opt=-pass-through=-lmingwex -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lmoldname -plugin-opt=-pass-through=-lmsvcrt -plugin-opt=-pass-through=-ladvapi32 -plugin-opt=-pass-through=-lshell32 -plugin-opt=-pass-through=-luser32 -plugin-opt=-pass-through=-lkernel32 -plugin-opt=-pass-through=-lmingw32 -plugin-opt=-pass-through=-lmingwex -plugin-opt=-pass-through=-lmingw32 -plugin-opt=-pass-through=-lmingwex -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lmoldname -plugin-opt=-pass-through=-lmsvcrt -Bdynamic -u ___register_frame_info -u ___deregister_frame_info c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../crt2.o c:/mingw/bin/../lib/gcc/mingw32/9.2.0/crtbegin.o -Lc:/mingw/bin/../lib/gcc/mingw32/9.2.0 -Lc:/mingw/bin/../lib/gcc -Lc:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/lib -Lc:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../.. C:\Users\move78~1\AppData\Local\Temp\cctjBjko.o -lstdc++ -lmingw32 -lmingwex -lmingw32 -lmingwex -lgcc_s -lgcc -lmoldname -lmsvcrt -ladvapi32 -lshell32 -luser32 -lkernel32 -lmingw32 -lmingwex -lmingw32 -lmingwex -lgcc_s -lgcc -lmoldname -lmsvcrt c:/mingw/bin/../lib/gcc/mingw32/9.2.0/crtend.o
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: cannot find -lstdc++
collect2.exe: error: ld returned 1 exit status

You need 3 things to build:
Your tools (like g++.exe) need to be in your PATH (or specified via their full path when calling them, e.g. C:\mingw\mingw32\bin\g++.exe).
The compiler needs to be able to find the include files. If needed you can add include paths (pointing to the folder, not the actual file) using -I flags (or you can use C_INCLUDE_PATH (for C) and CPLUS_INCLUDE_PATH (for C++) environment variables).
The linker needs be able to find your libraries (like libstdc++.a). If needed you can add library paths (pointing to the folder, not the actual file) using -L flags (or you can use LIBRARY_PATH environment variable).
If you use MSYS2 shell these things should already have been set up.
The standalone build from http://winlibs.com/ looks for relative locations and should just work wherever you extract it on your disk.
You didn't mention which shell you were running your commands from (Command Prompt, PowerShell, MSYS), but it's important the above is respected correctly in whatever shell you are using.
As far as I know http://winlibs.com/ works in MSYS shell, Command Prompt and PowerShell (though parameters starting with - need to be put in quotes when using PowerShell).

Related

G++ not understanding console option

So, I need to compile, assemble, and link a C++ file. But, I need to use an external library with it. So, I'm attempting to do so with G++ using this batch file:
g++ -c WindowTest.cpp -I include\bgfx-master\include\bgfx -I include\glfw-3.3.8.bin.WIN32\include\GLFW --verbose
g++ WindowTest.o -o WindowTest.exe -static -L include\glfw-3.3.8.bin.WIN32\lib-vc2022 -l glfw3.lib --verbose
However, when run, I get this output:
g++ -c WindowTest.cpp -I include\bgfx-master\include\bgfx -I include\glfw-3.3.8.bin.WIN32\include\GLFW --verbose
Using built-in specs.
COLLECT_GCC=g++
Target: mingw32
Configured with: ../src/gcc-6.3.0/configure --build=x86_64-pc-linux-gnu --host=mingw32 --with-gmp=/mingw --with-mpfr=/mingw --with-mpc=/mingw --with-isl=/mingw --prefix=/mingw --disable-win32-registry --target=mingw32 --with-arch=i586 --enable-languages=c,c++,objc,obj-c++,fortran,ada --with-pkgversion='MinGW.org GCC-6.3.0-1' --enable-static --enable-shared --enable-threads --with-dwarf2 --disable-sjlj-exceptions --enable-version-specific-runtime-libs --with-libiconv-prefix=/mingw --with-libintl-prefix=/mingw --enable-libstdcxx-debug --with-tune=generic --enable-libgomp --disable-libvtv --enable-nls
Thread model: win32
gcc version 6.3.0 (MinGW.org GCC-6.3.0-1)
COLLECT_GCC_OPTIONS='-c' '-I' 'include\bgfx-master\include\bgfx' '-I' 'include\glfw-3.3.8.bin.WIN32\include\GLFW' '-v' '-shared-libgcc' '-mtune=generic' '-march=i586'
c:/mingw/bin/../libexec/gcc/mingw32/6.3.0/cc1plus.exe -quiet -v -I include\bgfx-master\include\bgfx -I include\glfw-3.3.8.bin.WIN32\include\GLFW -iprefix c:\mingw\bin\../lib/gcc/mingw32/6.3.0/ WindowTest.cpp -quiet -dumpbase WindowTest.cpp -mtune=generic -march=i586 -auxbase WindowTest -version -o C:\Users\owenr\AppData\Local\Temp\ccnfsknd.s
GNU C++14 (MinGW.org GCC-6.3.0-1) version 6.3.0 (mingw32)
compiled by GNU C version 6.3.0, GMP version 6.1.2, MPFR version 3.1.5, MPC version 1.0.2, isl version 0.15
warning: MPC header version 1.0.2 differs from library version 1.0.3.
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/6.3.0/include/c++"
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/6.3.0/include/c++/mingw32"
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/6.3.0/include/c++/backward"
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/6.3.0/include"
ignoring duplicate directory "/mingw/lib/gcc/mingw32/6.3.0/../../../../include"
ignoring duplicate directory "c:/mingw/lib/gcc/../../include"
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/6.3.0/include-fixed"
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/6.3.0/../../../../mingw32/include"
ignoring duplicate directory "/mingw/include"
#include "..." search starts here:
#include <...> search starts here:
include\bgfx-master\include\bgfx
include\glfw-3.3.8.bin.WIN32\include\GLFW
c:\mingw\bin\../lib/gcc/mingw32/6.3.0/include/c++
c:\mingw\bin\../lib/gcc/mingw32/6.3.0/include/c++/mingw32
c:\mingw\bin\../lib/gcc/mingw32/6.3.0/include/c++/backward
c:\mingw\bin\../lib/gcc/mingw32/6.3.0/include
c:\mingw\bin\../lib/gcc/mingw32/6.3.0/../../../../include
c:\mingw\bin\../lib/gcc/mingw32/6.3.0/include-fixed
c:\mingw\bin\../lib/gcc/mingw32/6.3.0/../../../../mingw32/include
End of search list.
GNU C++14 (MinGW.org GCC-6.3.0-1) version 6.3.0 (mingw32)
compiled by GNU C version 6.3.0, GMP version 6.1.2, MPFR version 3.1.5, MPC version 1.0.2, isl version 0.15
warning: MPC header version 1.0.2 differs from library version 1.0.3.
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: af09a87986453bf79da3565f33c7648f
COLLECT_GCC_OPTIONS='-c' '-I' 'include\bgfx-master\include\bgfx' '-I' 'include\glfw-3.3.8.bin.WIN32\include\GLFW' '-v' '-shared-libgcc' '-mtune=generic' '-march=i586'
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/bin/as.exe -v -I include\bgfx-master\include\bgfx -I include\glfw-3.3.8.bin.WIN32\include\GLFW -o WindowTest.o C:\Users\owenr\AppData\Local\Temp\ccnfsknd.s
GNU assembler version 2.28 (mingw32) using BFD version (GNU Binutils) 2.28
COMPILER_PATH=c:/mingw/bin/../libexec/gcc/mingw32/6.3.0/;c:/mingw/bin/../libexec/gcc/;c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/bin/
LIBRARY_PATH=c:/mingw/bin/../lib/gcc/mingw32/6.3.0/;c:/mingw/bin/../lib/gcc/;c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/lib/;c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../
COLLECT_GCC_OPTIONS='-c' '-I' 'include\bgfx-master\include\bgfx' '-I' 'include\glfw-3.3.8.bin.WIN32\include\GLFW' '-v' '-shared-libgcc' '-mtune=generic' '-march=i586'
$-> g++ WindowTest.o -o WindowTest.exe -static -L include\glfw-3.3.8.bin.WIN32\lib-vc2022 -l glfw3.lib --verbose
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/mingw32/6.3.0/lto-wrapper.exe
Target: mingw32
Configured with: ../src/gcc-6.3.0/configure --build=x86_64-pc-linux-gnu --host=mingw32 --with-gmp=/mingw --with-mpfr=/mingw --with-mpc=/mingw --with-isl=/mingw --prefix=/mingw --disable-win32-registry --target=mingw32 --with-arch=i586 --enable-languages=c,c++,objc,obj-c++,fortran,ada --with-pkgversion='MinGW.org GCC-6.3.0-1' --enable-static --enable-shared --enable-threads --with-dwarf2 --disable-sjlj-exceptions --enable-version-specific-runtime-libs --with-libiconv-prefix=/mingw --with-libintl-prefix=/mingw --enable-libstdcxx-debug --with-tune=generic --enable-libgomp --disable-libvtv --enable-nls
Thread model: win32
gcc version 6.3.0 (MinGW.org GCC-6.3.0-1)
COMPILER_PATH=c:/mingw/bin/../libexec/gcc/mingw32/6.3.0/;c:/mingw/bin/../libexec/gcc/;c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/bin/
LIBRARY_PATH=c:/mingw/bin/../lib/gcc/mingw32/6.3.0/;c:/mingw/bin/../lib/gcc/;c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/lib/;c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../
COLLECT_GCC_OPTIONS='-o' 'WindowTest.exe' '-static' '-Linclude\glfw-3.3.8.bin.WIN32\lib-vc2022' '-v' '-mtune=generic' '-march=i586'
c:/mingw/bin/../libexec/gcc/mingw32/6.3.0/collect2.exe -plugin c:/mingw/bin/../libexec/gcc/mingw32/6.3.0/liblto_plugin-0.dll -plugin-opt=c:/mingw/bin/../libexec/gcc/mingw32/6.3.0/lto-wrapper.exe -plugin-opt=-fresolution=C:\Users\owenr\AppData\Local\Temp\ccyUGRgT.res -plugin-opt=-pass-through=-lmingw32 -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_eh -plugin-opt=-pass-through=-lmoldname -plugin-opt=-pass-through=-lmingwex -plugin-opt=-pass-through=-lmsvcrt -plugin-opt=-pass-through=-ladvapi32 -plugin-opt=-pass-through=-lshell32 -plugin-opt=-pass-through=-luser32 -plugin-opt=-pass-through=-lkernel32 -plugin-opt=-pass-through=-lmingw32 -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_eh -plugin-opt=-pass-through=-lmoldname -plugin-opt=-pass-through=-lmingwex -plugin-opt=-pass-through=-lmsvcrt -Bstatic -o WindowTest.exe c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../crt2.o c:/mingw/bin/../lib/gcc/mingw32/6.3.0/crtbegin.o -Linclude\glfw-3.3.8.bin.WIN32\lib-vc2022 -Lc:/mingw/bin/../lib/gcc/mingw32/6.3.0 -Lc:/mingw/bin/../lib/gcc -Lc:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/lib -Lc:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../.. WindowTest.o -lglfw3.lib -lstdc++ -lmingw32 -lgcc -lgcc_eh -lmoldname -lmingwex -lmsvcrt -ladvapi32 -lshell32 -luser32 -lkernel32 -lmingw32 -lgcc -lgcc_eh -lmoldname -lmingwex -lmsvcrt c:/mingw/bin/../lib/gcc/mingw32/6.3.0/crtend.o
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/bin/ld.exe: cannot find -lglfw3.lib
collect2.exe: error: ld returned 1 exit status
I've tried moving the library file, adding or removing space between the -l option and glfw3.lib, and many other things. It gives this output every time unless I just don't try to include the library file, in which case I get this error:
WindowTest.o:WindowTest.cpp:(.text+0xc): undefined reference to `glfwInit'
collect2.exe: error: ld returned 1 exit status
Edit:
Here's the file system
Main Directory:
include
glfw-3.3.8.bin.WIN32
include
glw3.h (Header file included in WindowTest.cpp)
lib-vc2022
glfw3.lib (Library file I want to include)
WindowTest.cpp
MakeFile.bat (Has g++ commands)

Why does g++ require libstdc++.a? Why is it not the default?

I had this problem, and the solution was to explicitly pass libstdc++.a to g++, like the following:
/usr/local/gcc-10.2.0/bin/g++ -I/usr/local/gcc-10.2.0/include -L/usr/local/gcc-10.2.0/lib64 -Wl,-rpath,/usr/local/gcc-10.2.0/lib64 b.cpp /usr/local/gcc-10.2.0/lib64/libstdc++.a
My question: Why do I need to explicitly pass libstdc++.a? How can I make it automatic so that libstdc++.a is used by default? How can I find the answer to the two questions?
The test code:
#include <sstream>
using namespace std;
int
main ()
{
ostringstream oss;
unsigned long k = 5;
oss << k;
}
Compiled with the following parameters:
/usr/local/gcc-10.2.0/bin/g++ -I/usr/local/gcc-10.2.0/include -L/usr/local/gcc-10.2.0/lib64 -Wl,-rpath,/usr/local/gcc-10.2.0/lib64 -lstdc++ b.cpp
Got the following output:
/tmp/cclRSXGV.o: In function main': b.cpp:(.text+0x35): undefined reference to std::ostream::operator<<(unsigned long)'
collect2: error: ld returned 1 exit status
Using GNU gcc 10.2.0, installed with the following:
../gcc-10.2.0/configure --prefix=/usr/local/gcc-10.2.0 --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-languages=c,c++ --disable-dssi --enable-libgcj-multifile --with-ppl --with-cloog --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux && make install-strip
Already had GNU gcc 4.4.7 installed by default at /usr, and GNU gcc 9.2.0 at /usr/local/gcc-9.2.0
Running /usr/local/gcc-10.2.0/bin/g++ -v b.cpp produces the following:
Using built-in specs.
COLLECT_GCC=/usr/local/gcc-10.2.0/bin/g++
COLLECT_LTO_WRAPPER=/usr/local/gcc-10.2.0/libexec/gcc/x86_64-redhat-linux/10.2.0/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../gcc-10.2.0/configure --prefix=/usr/local/gcc-10.2.0 --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-languages=c,c++ --disable-dssi --enable-libgcj-multifile --with-ppl --with-cloog --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 10.2.0 (GCC)
COLLECT_GCC_OPTIONS='-v' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
/usr/local/gcc-10.2.0/libexec/gcc/x86_64-redhat-linux/10.2.0/cc1plus -quiet -v -D_GNU_SOURCE b.cpp -quiet -dumpbase b.cpp -mtune=generic -march=x86-64 -auxbase b -version -o /tmp/ccJiMH6j.s
GNU C++14 (GCC) version 10.2.0 (x86_64-redhat-linux)
compiled by GNU C version 10.2.0, GMP version 6.1.0, MPFR version 3.1.4, MPC version 1.0.3, isl version isl-0.18-GMP
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "/usr/local/gcc-10.2.0/lib/gcc/x86_64-redhat-linux/10.2.0/../../../../x86_64-redhat-linux/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/local/gcc-10.2.0/lib/gcc/x86_64-redhat-linux/10.2.0/../../../../include/c++/10.2.0
/usr/local/gcc-10.2.0/lib/gcc/x86_64-redhat-linux/10.2.0/../../../../include/c++/10.2.0/x86_64-redhat-linux
/usr/local/gcc-10.2.0/lib/gcc/x86_64-redhat-linux/10.2.0/../../../../include/c++/10.2.0/backward
/usr/local/gcc-10.2.0/lib/gcc/x86_64-redhat-linux/10.2.0/include
/usr/local/include
/usr/local/gcc-10.2.0/include
/usr/local/gcc-10.2.0/lib/gcc/x86_64-redhat-linux/10.2.0/include-fixed
/usr/include
End of search list.
GNU C++14 (GCC) version 10.2.0 (x86_64-redhat-linux)
compiled by GNU C version 10.2.0, GMP version 6.1.0, MPFR version 3.1.4, MPC version 1.0.3, isl version isl-0.18-GMP
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 410dfe626634fcd13dbcedee05209c5e
COLLECT_GCC_OPTIONS='-v' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
as -v --64 -o /tmp/ccUyoUr2.o /tmp/ccJiMH6j.s
GNU assembler version 2.35 (x86_64-pc-linux-gnu) using BFD version (GNU Binutils) 2.35
COMPILER_PATH=/usr/local/gcc-10.2.0/libexec/gcc/x86_64-redhat-linux/10.2.0/:/usr/local/gcc-10.2.0/libexec/gcc/x86_64-redhat-linux/10.2.0/:/usr/local/gcc-10.2.0/libexec/gcc/x86_64-redhat-linux/:/usr/local/gcc-10.2.0/lib/gcc/x86_64-redhat-linux/10.2.0/:/usr/local/gcc-10.2.0/lib/gcc/x86_64-redhat-linux/
LIBRARY_PATH=/usr/local/gcc-10.2.0/lib/gcc/x86_64-redhat-linux/10.2.0/:/usr/local/gcc-10.2.0/lib/gcc/x86_64-redhat-linux/10.2.0/../../../../lib64/:/lib/../lib64/:/usr/lib/../lib64/:/usr/local/gcc-10.2.0/lib/gcc/x86_64-redhat-linux/10.2.0/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
/usr/local/gcc-10.2.0/libexec/gcc/x86_64-redhat-linux/10.2.0/collect2 -plugin /usr/local/gcc-10.2.0/libexec/gcc/x86_64-redhat-linux/10.2.0/liblto_plugin.so -plugin-opt=/usr/local/gcc-10.2.0/libexec/gcc/x86_64-redhat-linux/10.2.0/lto-wrapper -plugin-opt=-fresolution=/tmp/ccfGN6NK.res -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lc -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 /usr/lib/../lib64/crt1.o /usr/lib/../lib64/crti.o /usr/local/gcc-10.2.0/lib/gcc/x86_64-redhat-linux/10.2.0/crtbegin.o -L/usr/local/gcc-10.2.0/lib/gcc/x86_64-redhat-linux/10.2.0 -L/usr/local/gcc-10.2.0/lib/gcc/x86_64-redhat-linux/10.2.0/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/usr/local/gcc-10.2.0/lib/gcc/x86_64-redhat-linux/10.2.0/../../.. /tmp/ccUyoUr2.o -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /usr/local/gcc-10.2.0/lib/gcc/x86_64-redhat-linux/10.2.0/crtend.o /usr/lib/../lib64/crtn.o
/usr/local/bin/ld: /tmp/ccUyoUr2.o: in function `main':
b.cpp:(.text+0xc9): undefined reference to `std::cout'
/usr/local/bin/ld: b.cpp:(.text+0x35): undefined reference to std::ostream::operator<<(unsigned long)'
collect2: error: ld returned 1 exit status
By default g++ links against the dynamic libstdc++ library (libstdc++.so). By passing libstdc++.a explicitly you're linking against the static counterpart. You could achieve the same end result with -static-libstdc++.
Both static and dynamic linking against libstdc++ should work. Your issue could be possibly caused by building or installing GCC incorrectly (e.g. applying an incorrect patch, or mixing include files from different GCC builds).
To investigate, produce object file b.o by adding -c to your g++ command line, and preprocessed source b.ii by adding -E -o b.ii. Use nm -u b.o to get mangled names of undefined symbols (add -C to demangle). You'll see that mangled name of the missing symbol is _ZNSolsEm. Try to grep the missing symbol in the list of symbols defined by libstdc++ (nm -D --defined-only /usr/local/gcc-10.2.0/lib64/libstdc++.so). If it is missing from the library, the issue is that the included files expect the library to provide those template instantiations, while the actual library does not.
If you look in b.ii, you'll find that it declares the corresponding extern template towards the end:
extern template class basic_ostringstream<char>;
If you look into sstream.tcc file from gcc install tree that this line comes from, you'll see it's guarded by #if _GLIBCXX_EXTERN_TEMPLATE. The definition of this macro is in c++config.h, and is set depending on --enable-extern-template option at libstdc++ configuration time. If the library is built with --disable-extern-template, it will not provide those template instantiations.

How to make g++ require a standard `main`?

In particular, how can g++ be made to reject this invalid-as-standard-C++ code:
main(){}
?
I'm using MinGW g++ 6.2.0, but I imagine an answer will apply to all currently used variants of g++.
Example of what I've tried:
[C:\my\temp]
> g++ --version | find "++"
g++ (i686-win32-dwarf-rev1, Built by MinGW-W64 project) 6.2.0
[C:\my\temp]
> type foo.cpp
main() {}
[C:\my\temp]
> dir /b
foo.cpp
[C:\my\temp]
> g++ foo.cpp -Wall -Wextra -Wreturn-type -pedantic
[C:\my\temp]
> dir /b
a.exe
foo.cpp
[C:\my\temp]
> _
Version info with verbose compiler output:
C:\my\temp]
> g++ -v --version
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=C:/Program\ Files\ (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../libexec/gcc/i686-w64-mingw32/6.2.0/lto-wrapper.exe
g++ (i686-win32-dwarf-rev1, Built by MinGW-W64 project) 6.2.0
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Target: i686-w64-mingw32
Configured with: ../../../src/gcc-6.2.0/configure --host=i686-w64-mingw32 --build=i686-w64-mingw32 --target=i686-w64-mingw32 --prefix=/mingw32 --with-sysroot=/c/mingw620/i686-620-win32-dwarf-rt_v5-rev1/mingw32 --enable-shared --enable-static --disable-multilib --enable-languages=c,c++,fortran,lto --enable-libstdcxx-time=yes --enable-threads=win32 --enable-libgomp --enable-libatomic --enable-lto --enable-graphite --enable-checking=release --enable-fully-dynamic-string --enable-version-specific-runtime-libs --enable-libstdcxx-filesystem-ts=yes --disable-sjlj-exceptions --with-dwarf2 --disable-isl-version-check --disable-libstdcxx-pch --disable-libstdcxx-debug --enable-bootstrap --disable-rpath --disable-win32-registry --disable-nls --disable-werror --disable-symvers --with-gnu-as --with-gnu-ld --with-arch=i686 --with-tune=generic --with-libiconv --with-system-zlib --with-gmp=/c/mingw620/prerequisites/i686-w64-mingw32-static --with-mpfr=/c/mingw620/prerequisites/i686-w64-mingw32-static --with-mpc=/c/mingw620/prerequisites/i686-w64-mingw32-static --with-isl=/c/mingw620/prerequisites/i686-w64-mingw32-static --with-pkgversion='i686-win32-dwarf-rev1, Built by MinGW-W64 project' --with-bugurl=http://sourceforge.net/projects/mingw-w64 CFLAGS='-O2 -pipe -I/c/mingw620/i686-620-win32-dwarf-rt_v5-rev1/mingw32/opt/include -I/c/mingw620/prerequisites/i686-zlib-static/include -I/c/mingw620/prerequisites/i686-w64-mingw32-static/include' CXXFLAGS='-O2 -pipe -I/c/mingw620/i686-620-win32-dwarf-rt_v5-rev1/mingw32/opt/include -I/c/mingw620/prerequisites/i686-zlib-static/include -I/c/mingw620/prerequisites/i686-w64-mingw32-static/include' CPPFLAGS= LDFLAGS='-pipe -L/c/mingw620/i686-620-win32-dwarf-rt_v5-rev1/mingw32/opt/lib -L/c/mingw620/prerequisites/i686-zlib-static/lib -L/c/mingw620/prerequisites/i686-w64-mingw32-static/lib -Wl,--large-address-aware'
Thread model: win32
gcc version 6.2.0 (i686-win32-dwarf-rev1, Built by MinGW-W64 project)
COLLECT_GCC_OPTIONS='-v' '--version' '-shared-libgcc' '-mtune=generic' '-march=i686'
C:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../libexec/gcc/i686-w64-mingw32/6.2.0/cc1.exe -quiet -v -iprefix C:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/6.2.0/ -U_REENTRANT help-dummy -quiet -dumpbase help-dummy -mtune=generic -march=i686 -auxbase help-dummy -version --version -o C:\Users\AlfP\AppData\Local\Temp\cc4K0l79.s
GNU C11 (i686-win32-dwarf-rev1, Built by MinGW-W64 project) version 6.2.0 (i686-w64-mingw32)
compiled by GNU C version 6.2.0, GMP version 6.1.0, MPFR version 3.1.4, MPC version 1.0.3, isl version 0.15
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
COLLECT_GCC_OPTIONS='-v' '--version' '-shared-libgcc' '-mtune=generic' '-march=i686'
C:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/6.2.0/../../../../i686-w64-mingw32/bin/as.exe -v --version -o C:\Users\AlfP\AppData\Local\Temp\ccn2QnTg.o C:\Users\AlfP\AppData\Local\Temp\cc4K0l79.s
GNU assembler version 2.25 (i686-w64-mingw32) using BFD version (GNU Binutils) 2.25
GNU assembler (GNU Binutils) 2.25
Copyright (C) 2014 Free Software Foundation, Inc.
This program is free software; you may redistribute it under the terms of
the GNU General Public License version 3 or later.
This program has absolutely no warranty.
This assembler was configured for a target of `i686-w64-mingw32'.
COMPILER_PATH=C:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../libexec/gcc/i686-w64-mingw32/6.2.0/;C:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../libexec/gcc/;C:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/6.2.0/../../../../i686-w64-mingw32/bin/
LIBRARY_PATH=C:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/6.2.0/;C:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/;C:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/6.2.0/../../../../i686-w64-mingw32/lib/../lib/;C:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/6.2.0/../../../../lib/;C:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/6.2.0/../../../../i686-w64-mingw32/lib/;C:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/6.2.0/../../../
COLLECT_GCC_OPTIONS='-v' '--version' '-shared-libgcc' '-mtune=generic' '-march=i686'
C:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../libexec/gcc/i686-w64-mingw32/6.2.0/collect2.exe -plugin C:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../libexec/gcc/i686-w64-mingw32/6.2.0/liblto_plugin-0.dll -plugin-opt=C:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../libexec/gcc/i686-w64-mingw32/6.2.0/lto-wrapper.exe -plugin-opt=-fresolution=C:\Users\AlfP\AppData\Local\Temp\ccXVaxln.res -plugin-opt=-pass-through=-lmingw32 -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lmoldname -plugin-opt=-pass-through=-lmingwex -plugin-opt=-pass-through=-lmsvcrt -plugin-opt=-pass-through=-ladvapi32 -plugin-opt=-pass-through=-lshell32 -plugin-opt=-pass-through=-luser32 -plugin-opt=-pass-through=-lkernel32 -plugin-opt=-pass-through=-liconv -plugin-opt=-pass-through=-lmingw32 -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lmoldname -plugin-opt=-pass-through=-lmingwex -plugin-opt=-pass-through=-lmsvcrt --sysroot=C:/mingw620/i686-620-win32-dwarf-rt_v5-rev1/mingw32 -m i386pe -Bdynamic -u ___register_frame_info -u ___deregister_frame_info --version C:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/6.2.0/../../../../i686-w64-mingw32/lib/../lib/crt2.o C:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/6.2.0/crtbegin.o -LC:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/6.2.0 -LC:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc -LC:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/6.2.0/../../../../i686-w64-mingw32/lib/../lib -LC:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/6.2.0/../../../../lib -LC:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/6.2.0/../../../../i686-w64-mingw32/lib -LC:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/6.2.0/../../.. C:\Users\AlfP\AppData\Local\Temp\ccn2QnTg.o -lmingw32 -lgcc_s -lgcc -lmoldname -lmingwex -lmsvcrt -ladvapi32 -lshell32 -luser32 -lkernel32 -liconv -lmingw32 -lgcc_s -lgcc -lmoldname -lmingwex -lmsvcrt C:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/6.2.0/crtend.o
collect2 version 6.2.0
C:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/6.2.0/../../../../i686-w64-mingw32/bin/ld.exe -plugin C:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../libexec/gcc/i686-w64-mingw32/6.2.0/liblto_plugin-0.dll -plugin-opt=C:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../libexec/gcc/i686-w64-mingw32/6.2.0/lto-wrapper.exe -plugin-opt=-fresolution=C:\Users\AlfP\AppData\Local\Temp\ccXVaxln.res -plugin-opt=-pass-through=-lmingw32 -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lmoldname -plugin-opt=-pass-through=-lmingwex -plugin-opt=-pass-through=-lmsvcrt -plugin-opt=-pass-through=-ladvapi32 -plugin-opt=-pass-through=-lshell32 -plugin-opt=-pass-through=-luser32 -plugin-opt=-pass-through=-lkernel32 -plugin-opt=-pass-through=-liconv -plugin-opt=-pass-through=-lmingw32 -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lmoldname -plugin-opt=-pass-through=-lmingwex -plugin-opt=-pass-through=-lmsvcrt --sysroot=C:/mingw620/i686-620-win32-dwarf-rt_v5-rev1/mingw32 -m i386pe -Bdynamic -u ___register_frame_info -u ___deregister_frame_info --version C:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/6.2.0/../../../../i686-w64-mingw32/lib/../lib/crt2.o C:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/6.2.0/crtbegin.o -LC:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/6.2.0 -LC:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc -LC:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/6.2.0/../../../../i686-w64-mingw32/lib/../lib -LC:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/6.2.0/../../../../lib -LC:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/6.2.0/../../../../i686-w64-mingw32/lib -LC:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/6.2.0/../../.. C:\Users\AlfP\AppData\Local\Temp\ccn2QnTg.o -lmingw32 -lgcc_s -lgcc -lmoldname -lmingwex -lmsvcrt -ladvapi32 -lshell32 -luser32 -lkernel32 -liconv -lmingw32 -lgcc_s -lgcc -lmoldname -lmingwex -lmsvcrt C:/Program Files (x86)/mingw-w64/i686-6.2.0-win32-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/6.2.0/crtend.o
GNU ld (GNU Binutils) 2.25
Copyright (C) 2014 Free Software Foundation, Inc.
This program is free software; you may redistribute it under the terms of
the GNU General Public License version 3 or (at your option) a later version.
This program has absolutely no warranty.
COLLECT_GCC_OPTIONS='-v' '--version' '-shared-libgcc' '-mtune=generic' '-march=i686'
[C:\my\temp]
> _
You can pass -pedantic-errors, which gives the following error message on g++ 6.3:
main.cpp:1:6: error: ISO C++ forbids declaration of 'main' with no type [-Wpedantic]
main(){}
Live demo
Here is the documentation for -pedantic-errors:
Give an error whenever the base standard (see -Wpedantic) requires a diagnostic, in some cases where
there is undefined behavior at compile-time and in some other cases that do not prevent compilation of
programs that are valid according to the standard. This is not equivalent to -Werror=pedantic, since
there are errors enabled by this option and not enabled by the latter and vice versa.
Once you enable warnings (-Wall), g++ will complain about this code. Additionally you can have the warnings be treated as errors (-Werror), so that the compilation fails (a good thing to have).
Example with g++ 5.4.0:
kos#Starbuck ~$ cat bar.cpp
main(){}
kos#Starbuck ~$ g++ bar.cpp
kos#Starbuck ~$ g++ bar.cpp -Wall -Werror
bar.cpp:1:6: error: ISO C++ forbids declaration of ‘main’ with no type [-Werror=return-type]
main(){}
^
cc1plus: all warnings being treated as errors

Why is g++ not producing a binary?

This is my build log when using Eclipse and GCC.
I have tried every suggestion for this issue I have found on the web (and there are lots!) but nothing works for me.
Can anyone see any issue from this build log please:
17:49:24 **** Rebuild of configuration Debug for project test2 ****
Info: Internal Builder is used for build
g++ -O0 -g3 -Wall -c -fmessage-length=0 -v -o "src\\test2.o" "..\\src\\test2.cpp"
Using built-in specs.
COLLECT_GCC=C:\MinGW\bin\g++.exe
Target: mingw32
Configured with: ../gcc-4.8.1/configure --prefix=/mingw --host=mingw32 --build=mingw32 --without-pic --enable-shared --enable-static --with-gnu-ld --enable-lto --enable-libssp --disable-multilib --enable-languages=c,c++,fortran,objc,obj-c++,ada --disable-sjlj-exceptions --with-dwarf2 --disable-win32-registry --enable-libstdcxx-debug --enable-version-specific-runtime-libs --with-gmp=/usr/src/pkg/gmp-5.1.2-1-mingw32-src/bld --with-mpc=/usr/src/pkg/mpc-1.0.1-1-mingw32-src/bld --with-mpfr= --with-system-zlib --with-gnu-as --enable-decimal-float=yes --enable-libgomp --enable-threads --with-libiconv-prefix=/mingw32 --with-libintl-prefix=/mingw --disable-bootstrap LDFLAGS=-s CFLAGS=-D_USE_32BIT_TIME_T
Thread model: win32
gcc version 4.8.1 (GCC)
COLLECT_GCC_OPTIONS='-O0' '-g3' '-Wall' '-c' '-fmessage-length=0' '-v' '-o' 'src\test2.o' '-shared-libgcc' '-mtune=generic' '-march=pentiumpro'
c:/mingw/bin/../libexec/gcc/mingw32/4.8.1/cc1plus.exe -quiet -v -iprefix c:\mingw\bin\../lib/gcc/mingw32/4.8.1/ -dD ..\src\test2.cpp -quiet -dumpbase test2.cpp -mtune=generic -march=pentiumpro -auxbase-strip src\test2.o -g3 -O0 -Wall -version -fmessage-length=0 -o C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\cc2tQ3YT.s
GNU C++ (GCC) version 4.8.1 (mingw32)
compiled by GNU C version 4.8.1, GMP version 5.1.2, MPFR version 3.1.2, MPC version 1.0.1
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/4.8.1/include/c++"
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/4.8.1/include/c++/mingw32"
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/4.8.1/include/c++/backward"
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/4.8.1/include"
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/4.8.1/../../../../include"
ignoring duplicate directory "/mingw/include"
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/4.8.1/include-fixed"
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/4.8.1/../../../../mingw32/include"
ignoring duplicate directory "/mingw/include"
#include "..." search starts here:
#include <...> search starts here:
c:\mingw\bin\../lib/gcc/mingw32/4.8.1/include/c++
c:\mingw\bin\../lib/gcc/mingw32/4.8.1/include/c++/mingw32
c:\mingw\bin\../lib/gcc/mingw32/4.8.1/include/c++/backward
c:\mingw\bin\../lib/gcc/mingw32/4.8.1/include
c:\mingw\bin\../lib/gcc/mingw32/4.8.1/../../../../include
c:\mingw\bin\../lib/gcc/mingw32/4.8.1/include-fixed
c:\mingw\bin\../lib/gcc/mingw32/4.8.1/../../../../mingw32/include
End of search list.
GNU C++ (GCC) version 4.8.1 (mingw32)
compiled by GNU C version 4.8.1, GMP version 5.1.2, MPFR version 3.1.2, MPC version 1.0.1
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 1ebc2a6f92fbd3aadc367a20a63fdf9f
COLLECT_GCC_OPTIONS='-O0' '-g3' '-Wall' '-c' '-fmessage-length=0' '-v' '-o' 'src\test2.o' '-shared-libgcc' '-mtune=generic' '-march=pentiumpro'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/as.exe -v -o src\test2.o C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\cc2tQ3YT.s
GNU assembler version 2.24 (mingw32) using BFD version (GNU Binutils) 2.24
COMPILER_PATH=c:/mingw/bin/../libexec/gcc/mingw32/4.8.1/;c:/mingw/bin/../libexec/gcc/;c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/
LIBRARY_PATH=c:/mingw/bin/../lib/gcc/mingw32/4.8.1/;c:/mingw/bin/../lib/gcc/;c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/lib/;c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../
COLLECT_GCC_OPTIONS='-O0' '-g3' '-Wall' '-c' '-fmessage-length=0' '-v' '-o' 'src\test2.o' '-shared-libgcc' '-mtune=generic' '-march=pentiumpro'
17:49:24 Build Finished (took 313ms)

Can't link BOTAN library with eclipse

I have installed and built the BOTAN cryptographic library.
Everything seems correctly installed, since I have the following entries in /usr/local/lib directory:
-rw-r--r-- 1 root root 7676350 2012-06-03 11:01 libbotan-1.10.a
lrwxrwxrwx 1 root root 20 2012-06-03 11:01 libbotan-1.10.so -> libbotan-1.10.so.0.1
lrwxrwxrwx 1 root root 20 2012-06-03 11:01 libbotan-1.10.so.0 -> libbotan-1.10.so.0.1
-rwxr-xr-x 1 root root 3396354 2012-06-03 11:01 libbotan-1.10.so.0.1
and in the /usr/local/include one too:
drwxr-xr-x 3 root root 4096 2012-06-03 11:01 botan-1.10
And eventually I managed to include the libraries in the main of my program, and write code without error.
However when building my project (on Eclipse Indigo) I get the following output on console, with the red stderr fashion:
error while loading shared libraries: libbotan-1.10.so.0: cannot open shared object file: No such file or directory
The code I'm trying to execute is the following:
try
{
cout << "In try" << endl;
Botan::LibraryInitializer init;
}catch(std::exception& e)
{
cout << "In catch" << endl;
cerr << e.what() << endl;
}
I have the following doubts:
Is it a problem of the Linker? (And if yes, why isn't the linker throwing the error, but the stderr?)
If not, how can I figure out to let my program find the libbotan-1.10.so.0 file which is actually there? (in /usr/local/lib).
Note that my environment variable $LD_LIBRARY_PATH is currently set, and has the following value:
$> echo $LD_LIBRARY_PATH
$> /usr/local/lib/
Compiler Command Line:
Invoking: GCC C++ Compiler
g++ -I/usr/include/c++/4.5.2 -I/usr/local/include/botan-1.10 -O3 -g3 -Wall -c -fmessage-length=0 -std=c++0x -v -MMD -MP -MF"mainOobClient.d" -MT"mainOobClient.d" -o "mainOobClient.o" "../mainOobClient.cpp"
g++ -I/usr/include/c++/4.5.2 -I/usr/local/include/botan-1.10 -O3 -g3 -Wall -c -fmessage-length=0 -std=c++0x -v -MMD -MP -MF"oobClientClass.d" -MT"oobClientClass.d" -o "oobClientClass.o" "../oobClientClass.cpp"
Invoking: GCC C++ Linker
g++ -L/usr/local/lib -o "MTVS_oobClient" ./mainOobClient.o ./oobClientClass.o -lbotan-1.10
Complete Build Output:
Build of configuration Debug for project MTVS_oobClient
make all
Building file: ../mainOobClient.cpp
Invoking: GCC C++ Compiler
g++ -I/usr/include/c++/4.5.2 -I/usr/local/include/botan-1.10 -O3 -g3 -Wall -c -fmessage-length=0 -std=c++0x -v -MMD -MP -MF"mainOobClient.d" -MT"mainOobClient.d" -o "mainOobClient.o" "../mainOobClient.cpp"
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/lto-wrapper
Target: i686-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.5.2-8ubuntu4' --with-bugurl=file:///usr/share/doc/gcc-4.5/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.5 --enable-shared --enable-multiarch --with-multiarch-defaults=i386-linux-gnu --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib/i386-linux-gnu --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.5 --libdir=/usr/lib/i386-linux-gnu --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-plugin --enable-gold --enable-ld=default --with-plugin-ld=ld.gold --enable-objc-gc --enable-targets=all --disable-werror --with-arch-32=i686 --with-tune=generic --enable-checking=release --build=i686-linux-gnu --host=i686-linux-gnu --target=i686-linux-gnu
Thread model: posix
gcc version 4.5.2 (Ubuntu/Linaro 4.5.2-8ubuntu4)
Finished building: ../mainOobClient.cpp
Building file: ../oobClientClass.cpp
Invoking: GCC C++ Compiler
g++ -I/usr/include/c++/4.5.2 -I/usr/local/include/botan-1.10 -O3 -g3 -Wall -c -fmessage-length=0 -std=c++0x -v -MMD -MP -MF"oobClientClass.d" -MT"oobClientClass.d" -o "oobClientClass.o" "../oobClientClass.cpp"
COLLECT_GCC_OPTIONS='-I/usr/include/c++/4.5.2' '-I/usr/local/include/botan-1.10' '-O3' '-g3' '-Wall' '-c' '-fmessage-length=0' '-std=c++0x' '-v' '-MMD' '-MP' '-MFmainOobClient.d' '-MTmainOobClient.d' '-o' 'mainOobClient.o' '-shared-libgcc' '-mtune=generic' '-march=i686'
/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/cc1plus -quiet -v -I/usr/include/c++/4.5.2 -I/usr/local/include/botan-1.10 -MMD mainOobClient.d -MFmainOobClient.d -MP -MTmainOobClient.d -MQ mainOobClient.o -dD -D_GNU_SOURCE ../mainOobClient.cpp -D_FORTIFY_SOURCE=2 -quiet -dumpbase mainOobClient.cpp -mtune=generic -march=i686 -auxbase-strip mainOobClient.o -g3 -O3 -Wall -std=c++0x -version -fmessage-length=0 -fstack-protector -o /tmp/ccLIYQkV.s
Finished building: ../oobClientClass.cpp
GNU C++ (Ubuntu/Linaro 4.5.2-8ubuntu4) version 4.5.2 (i686-linux-gnu)
Building target: MTVS_oobClient
Invoking: GCC C++ Linker
g++ ./mainOobClient.o ./oobClientClass.o -lbotan-1.10 -L/usr/local/lib -o "MTVS_oobClient"
compiled by GNU C version 4.5.2, GMP version 4.3.2, MPFR version 3.0.0-p8, MPC version 0.9
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "/usr/local/include/i386-linux-gnu"
ignoring nonexistent directory "/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/../../../../../i686-linux-gnu/include"
ignoring duplicate directory "/usr/include/c++/4.5.2"
as it is a non-system directory that duplicates a system directory
Finished building target: MTVS_oobClient
#include "..." search starts here:
#include <...> search starts here:
/usr/local/include/botan-1.10
/usr/include/c++/4.5
/usr/include/c++/4.5/i686-linux-gnu
/usr/include/c++/4.5/backward
/usr/local/include
/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/include
/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/include-fixed
/usr/include/i386-linux-gnu
/usr/include
End of search list.
GNU C++ (Ubuntu/Linaro 4.5.2-8ubuntu4) version 4.5.2 (i686-linux-gnu)
compiled by GNU C version 4.5.2, GMP version 4.3.2, MPFR version 3.0.0-p8, MPC version 0.9
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 0c5cb630517b5952f4898dfa56d7e8e5
COLLECT_GCC_OPTIONS='-I/usr/include/c++/4.5.2' '-I/usr/local/include/botan-1.10' '-O3' '-g3' '-Wall' '-c' '-fmessage-length=0' '-std=c++0x' '-v' '-MMD' '-MP' '-MFmainOobClient.d' '-MTmainOobClient.d' '-o' 'mainOobClient.o' '-shared-libgcc' '-mtune=generic' '-march=i686'
as -V -Qy --32 -o mainOobClient.o /tmp/ccLIYQkV.s
GNU assembler version 2.21.0 (i686-linux-gnu) using BFD version (GNU Binutils for Ubuntu) 2.21.0.20110327
COMPILER_PATH=/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/:/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/:/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/:/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/:/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/
LIBRARY_PATH=/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/:/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/../../../:/lib/:/usr/lib/:/usr/lib/i386-linux-gnu/
COLLECT_GCC_OPTIONS='-I/usr/include/c++/4.5.2' '-I/usr/local/include/botan-1.10' '-O3' '-g3' '-Wall' '-c' '-fmessage-length=0' '-std=c++0x' '-v' '-MMD' '-MP' '-MFmainOobClient.d' '-MTmainOobClient.d' '-o' 'mainOobClient.o' '-shared-libgcc' '-mtune=generic' '-march=i686'
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/lto-wrapper
Target: i686-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.5.2-8ubuntu4' --with-bugurl=file:///usr/share/doc/gcc-4.5/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.5 --enable-shared --enable-multiarch --with-multiarch-defaults=i386-linux-gnu --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib/i386-linux-gnu --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.5 --libdir=/usr/lib/i386-linux-gnu --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-plugin --enable-gold --enable-ld=default --with-plugin-ld=ld.gold --enable-objc-gc --enable-targets=all --disable-werror --with-arch-32=i686 --with-tune=generic --enable-checking=release --build=i686-linux-gnu --host=i686-linux-gnu --target=i686-linux-gnu
Thread model: posix
gcc version 4.5.2 (Ubuntu/Linaro 4.5.2-8ubuntu4)
COLLECT_GCC_OPTIONS='-I/usr/include/c++/4.5.2' '-I/usr/local/include/botan-1.10' '-O3' '-g3' '-Wall' '-c' '-fmessage-length=0' '-std=c++0x' '-v' '-MMD' '-MP' '-MFoobClientClass.d' '-MToobClientClass.d' '-o' 'oobClientClass.o' '-shared-libgcc' '-mtune=generic' '-march=i686'
/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/cc1plus -quiet -v -I/usr/include/c++/4.5.2 -I/usr/local/include/botan-1.10 -MMD oobClientClass.d -MFoobClientClass.d -MP -MToobClientClass.d -MQ oobClientClass.o -dD -D_GNU_SOURCE ../oobClientClass.cpp -D_FORTIFY_SOURCE=2 -quiet -dumpbase oobClientClass.cpp -mtune=generic -march=i686 -auxbase-strip oobClientClass.o -g3 -O3 -Wall -std=c++0x -version -fmessage-length=0 -fstack-protector -o /tmp/ccTuqeWw.s
GNU C++ (Ubuntu/Linaro 4.5.2-8ubuntu4) version 4.5.2 (i686-linux-gnu)
compiled by GNU C version 4.5.2, GMP version 4.3.2, MPFR version 3.0.0-p8, MPC version 0.9
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "/usr/local/include/i386-linux-gnu"
ignoring nonexistent directory "/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/../../../../../i686-linux-gnu/include"
ignoring duplicate directory "/usr/include/c++/4.5.2"
as it is a non-system directory that duplicates a system directory
#include "..." search starts here:
#include <...> search starts here:
/usr/local/include/botan-1.10
/usr/include/c++/4.5
/usr/include/c++/4.5/i686-linux-gnu
/usr/include/c++/4.5/backward
/usr/local/include
/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/include
/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/include-fixed
/usr/include/i386-linux-gnu
/usr/include
End of search list.
GNU C++ (Ubuntu/Linaro 4.5.2-8ubuntu4) version 4.5.2 (i686-linux-gnu)
compiled by GNU C version 4.5.2, GMP version 4.3.2, MPFR version 3.0.0-p8, MPC version 0.9
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 0c5cb630517b5952f4898dfa56d7e8e5
../oobClientClass.cpp: In member function ‘void oobClientClass::authenticateTransaction()’:
../oobClientClass.cpp:126:29: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result
../oobClientClass.cpp:164:63: warning: ignoring return value of ‘ssize_t write(int, const void*, size_t)’, declared with attribute warn_unused_result
../oobClientClass.cpp:59:6: warning: ‘confirmationChoice’ may be used uninitialized in this function
COLLECT_GCC_OPTIONS='-I/usr/include/c++/4.5.2' '-I/usr/local/include/botan-1.10' '-O3' '-g3' '-Wall' '-c' '-fmessage-length=0' '-std=c++0x' '-v' '-MMD' '-MP' '-MFoobClientClass.d' '-MToobClientClass.d' '-o' 'oobClientClass.o' '-shared-libgcc' '-mtune=generic' '-march=i686'
as -V -Qy --32 -o oobClientClass.o /tmp/ccTuqeWw.s
GNU assembler version 2.21.0 (i686-linux-gnu) using BFD version (GNU Binutils for Ubuntu) 2.21.0.20110327
COMPILER_PATH=/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/:/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/:/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/:/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/:/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/
LIBRARY_PATH=/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/:/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/../../../:/lib/:/usr/lib/:/usr/lib/i386-linux-gnu/
COLLECT_GCC_OPTIONS='-I/usr/include/c++/4.5.2' '-I/usr/local/include/botan-1.10' '-O3' '-g3' '-Wall' '-c' '-fmessage-length=0' '-std=c++0x' '-v' '-MMD' '-MP' '-MFoobClientClass.d' '-MToobClientClass.d' '-o' 'oobClientClass.o' '-shared-libgcc' '-mtune=generic' '-march=i686'
**** Build Finished ****
The error is coming at run-time. You need to specify the rpath for the binary at build time - Where you have the:
-L/usr/local/lib
You also need:
-Wl,-rpath=/usr/local/lib
This instructs ld to use this path when trying to find the library at run-time. Your LD_LIBRARY_PATH is probably being overridden by the launcher of eclipse, which is why the variable is not being used.
Alternatives:
Make sure that /usr/local/lib is in the /etc/ld.so.conf, and that ldconfig has been run after installing the library.
Link to the .a, using the command line:
g++ -o "MTVS_oobClient" ./mainOobClient.o ./oobClientClass.o -L/usr/local/lib -Wl,-Bstatic -lbotan-1.10 -Wl,-Bdynamic
or using:
g++ -o "MTVS_oobClient" ./mainOobClient.o ./oobClientClass.o /usr/local/lib/libbotan-1.10.a
... both should have the same effect
Since the program build ends correctly, it is clear that it's a run-time problem due to the fact that the program can't find the Shared Library.
I looked for other people with same run-time issue and found out the solution (that I'll briefly sum up) thanks to another SO question.
What I did was to build the dynamically-linked executable, that wishes to be linked against libbotan-1.10.so.0 at execution time. The linking during the build phase was done with command line option -L/usr/local/lib.
There are a few options to make botan (or any other library) available at run-time:
Create a symlink to botan in a directory listed in /etc/ld.so.conf (or /lib or /usr/lib)
Copy botan to a directory listed in /etc/ld.so.conf (or /lib or /usr/lib) (defaults)
Add the directory containing botan to /etc/ld.so.conf
Set LD_LIBRARY_PATH=/usr/local/lib before running your executable.
Recompile the program statically. (It will work, but don't bother.)
After any of the first 3, rerun ldconfig so the linker cache is updated. (You can then run ldconfig -v to verify it's resolvable.)
Note those aren't steps, they're options. You only need to do 1 of them.
Updating the linker cache with ldconfig made it work for me.