Compiling 32-bit code with 64-bit MinGW GCC [duplicate] - c++

I've downloaded MinGW from this link x64-4.8.1-posix-sjlj-rev1 but when I try to build for x86 target I've lots of linkage errors... seems that only x64 lib are installed...
I've need to build for x86 and x64 platforms on windows... Have I to download both x64 and x86 or are some simpler ways?
Edit I'm using eclipse keplero as IDE
I've tryed to build myself a simple hello world program with g++ -m32 -std=c++11 test.cpp -o test32.exe and g++ -m64 -std=c++11 test.cpp -o test64.exe. And all is ok... So the problem was with eclipse... After a little a discovered that I need to use MYSY ( set in PATH ) and set -m32 also in the c++ linkage options...
Now all is fine.
I've also tryed to use NetBeans C++ as IDE... seems a gread IDE!!!

It is not multilib enabled. That's why you are not able to compile 32-bit(x86) program. You can get multilib enabled toolchain from following link:
For 64-bit machine: 64-Bit
For 32-bit machine: 32-Bit

Related

Compile OpenGL with GCC 64-bit

I have a project that compiles with GCC 32-bit. I have been trying to migrate to GCC 64-bit on my native Windows machine (not through a VM). To do this I have downloaded w64devkit from https://github.com/skeeto/w64devkit/releases. My 32-bit install comes from https://sourceforge.net/projects/mingw/.
NOTE: Both installs cannot be active at the same time. Since the 32-bit version makes it into the path before we can add the 64-bit version through the environment variables, I modify the 32-bit MinGW folder name so that when the 64-bit version of GCC tries to use as.exe and ld.exe through the path, it will find the correct versions.
I have also downloaded glm, glew, and glfw for this project, and added folders to the path to get at least the 32-bit version working.
The working command line to compile the 32-bit version is as follows:
g++ -std=c++11 -O3 -ggdb src/main.cpp -o build/main -lopengl32 -L. -I"C:\lib\glm" -I"C:\lib\glew-2.1.0-win32\include" -I"C:\lib\glfw-3.3.8.bin.WIN32\include" "C:\lib\glfw-3.3.8.bin.WIN32\lib\libglfw3dll.a" "C:\lib\glew-2.1.0-win32\lib\Release\Win32\glew32.lib"
The following command line to compile the 64-bit version also works:
g++ -std=c++11 -O0 -ggdb src/main.cpp -o build/main -lopengl32 -L. -I"C:\lib\glm" -I"C:\lib\glew-2.1.0-win32\include" -I"C:\lib\glfw-3.3.8.bin.WIN64\include" "C:\lib\glfw-3.3.8.bin.WIN64\lib-mingw-w64\libglfw3dll.a" "C:\lib\glew-2.1.0-win32\lib\Release\x64\glew32.lib"
However whenever I try to run the executables, the 32-bit version runs and the 64-bit version gives me an error in a pop-up window. "The application was unable to start correctly (0xc000007b). Click OK to close the application."
When I try to run it in one of my debuggers I get a "Error: 32-bit processes are not supported". Another debugger says "During startup program exited with code 0xc000007b".
Clearly something is going horribly wrong, but I don't know what or where.

compile with clang from the command line: compatibility issues mac os X

I am compiling a c++14 project on MacOsX10.10 using cmake, clang++, boost and openCV (static linkage - compilation flags: -Wall -std=c++14 -O3). How can I make sure the program runs out-of-the-box in older MacOsX versions? (and in other mac computers as well?) I've tested the binary on an older macbook running os X 10.7 and it failed. With xcode it's possible to build a program against some particular SDK, can I do something similar from the command line?
P.S. This is a more general question, but the source code for this particular project can be found here: https://github.com/MarinosK/oiko-nomic-threads
You need to make sure that all dependencies are linked statically into your executable. So this not only includes the .a (static libraries or object archives) but also the C++ (and possibly C) standard libraries.
For example:
clang --std=c++14 -stdlib=libstdc++ main.cpp -o main thirdparty.a -static -lstdc++

GCC Fails to Generate Object Files When Compiling

Recently I have noticed that GCC does not generate object (*.o) files when compiling with '-c'. It does not issue any errors or warnings. I have run it with '-verbose' but it shows nothing out of the ordinary.
Running under Windows:
gcc -Wall -c source_file.c
I have also tried compiling with '-verbose' to display detailed information
gcc -Wall -verbose -c source_file.c
It should produce 'source_file.o' but it doesn't. Any idea what's going on?
The issue has been resolved. Reinstalling GCC (mingw) fixed the problem. It is possible the problem came about because I tried to install the 64-bit version of GCC (Mingw-w64) to top of the 32-bit version...

Getting a Botan library test program to compile under Windows 7 (MinGW, Code::Blocks)

I have been trying in vain to get this test program to compile
#include <botan/botan.h>
int main()
{
Botan::LibraryInitializer init;
}
I have downloaded library source from the website. I ran configure.py, which ran fine.
I then attempted to run MinGW-make
This is the error I got
c:\Botan-1.11.7>mingw32-make
g++ -m64 -pthread -fPIC -fvisibility=hidden -std=c++11 -D_REENTRANT -fstack-pro
tector -O3 -momit-leaf-frame-pointer -Wall -Wextra -Wstrict-aliasing -Wstrict-ov
erflow=5 -Wcast-align -Wmissing-declarations -Wpointer-arith -Wcast-qual -Wold-s
tyle-cast -Wzero-as-null-pointer-constant -Ibuild\include -c C:\Botan-1.11.7\src
\lib\algo_base\scan_name.cpp -o build\obj\lib\src_lib_algo_base_scan_name.obj
C:\Botan-1.11.7\src\lib\algo_base\scan_name.cpp:1:0: warning: -fPIC ignored for
target (all code is position independent) [enabled by default]
C:\Botan-1.11.7\src\lib\algo_base\scan_name.cpp:1:0: sorry, unimplemented: 64-bi
t mode not compiled in
mingw32-make: *** [build\obj\lib\src_lib_algo_base_scan_name.obj] Error 1
I searched Google and any available forum I could find. I found prebuilt libraries for windows in a hidden folder on their site. However the package contained lib and dll files and not .a files which MinGW requires. I tries to use a program LIB2A which created a .a file. I have added this file into my code::blocks linker options. I also included the include folder.
When I try to compile I get this error.
C:\botan\include\botan\init.h|41|undefined reference to `_imp___ZN5Botan18LibraryInitializer10initializeERKSs'|
It seems like it cannot see the library with function definitions, but I am lost on where to go from here.
mingw32-make has the -m64 flag set, which means it's trying to build a 64 bit library. To build a 64 bit library, you'll need to get MinGW-w64.
When you ran configure.py, it likely set up your MakeFile to build a 64 bit library. You'll want to check what options are set in its output. This includes the option to build 32 bit or 64 bit. Either way, you'll want some variant of this: python configure.py --os=mingw --cc=gcc
This question had a similar error, and suggested using MinGW-w64, which will allow you to build a 64 bit library: Building 64 bit dll with MinGW 32 bit in Eclipse
In order to build with mingw32-make off the command line, you'll need to also add the MinGW-w64 bin directory to your Windows path, and likely remove the Code::Blocks packaged MinGW from your path to avoid conflicts. You'll still use mingw32-make to build the library with MinGW-w64; even though it's named mingw32-make, it will build 64 bit.
If you decide to build the library 64 bit, you'll need to build your application 64 bit as well, so you'll want to set up MinGW-w64 for Code::Blocks. To set MinGW-w64 up in Code::Blocks, see this question: Setting up MingW and Code::Blocks in Windows 8 64 bit.

How to compile 32bit x86 application in 64bit x86 environment?

How to compile 32bit x86 application in 64bit x86 environment?
Any command for cc/ld/ar, including options? Thanks.
Any links is well appreciated. Thanks.
Note: take c code for example.
To compile and link a C source file with a 64-bit multilib GCC, you can do the following:
gcc -m32 -c somefile.c
gcc -m32 somefile.o -o myprog
Note that all 32-bit libraries need to be installed and useable by the multilib compiler.
ar should work, if built correctly, it is discouraged to call ld directly, because its options are radically different from GCC's. Just link with GCC.
As to why it is "discouraged to call ld directly": If you all gcc to link, it will know exactly where system/runtime libraries are located, and also about any platform-specific options it needs to pass to ld. When calling ld directly, you need to take care of all of that. Here that matters for the options for 32 vs 64-bit, along with proper library directories.
Yes, just use -m32 and make sure that you have all the 32 bit tools and libraries installed (not all x86-64 distros include these by default, so you may need to apt-get or yast or whatever to install these).
$ gcc -m32 -Wall foo.c -o foo
Yes, I needed -D_FILE_OFFSET_BITS=64 sometimes -m32 makes some trouble sometimes so you have to try yourself.
c++ -m32 -D_FILE_OFFSET_BITS=64 foo.c -o foo
But that's for the other way round. Compiling 64bit programs on 32bit boxes.
The -m32 flag is all you need, ie.
gcc -m32 ...
If you get an error, you may need the 32-bit libraries, that might be named similar to glibc-devel.i686. That's the name of the package on Fedora (using yum), other Linux distros should be similar.
On Debian & Ubuntu, you'll need the gcc-multilib and ia32-libs-dev packages.