- Force gcc to compile 32 bit programs on 64 bit platform
- 3 Answers 3
- GCC to compile compile 64 bit
- 1 Answer 1
- compiling a simple single-source program
- compiling a simple multi-source program
- building more complex programs
- How to compile a C++ program as 64-bit on 64-bit machine?
- 3 Answers 3
- How to compile for Windows on Linux with gcc/g++?
- 7 Answers 7
Force gcc to compile 32 bit programs on 64 bit platform
I’ve got a proprietary program that I’m trying to use on a 64 bit system. When I launch the setup it works ok, but after it tries to update itself and compile some modules and it fails to load them. I’m suspecting it’s because it’s using gcc and gcc tries to compile them for a 64 bit system and therefore this program cannot use these modules. Is there any way (some environmental variables or something like that) to force gcc to do everything for a 32 bit platform. Would a 32 bit chroot work?
3 Answers 3
You need to make GCC use the -m32 flag.
You could try writing a simple shell script to your $PATH and call it gcc (make sure you don’t overwrite the original gcc, and make sure the new script comes earlier in $PATH , and that it uses the full path to GCC.
I think the code you need is just something like /bin/gcc -m32 $* depending on your shell (the $* is there to include all arguments, although it might be something else – very important!)
You’ll also need the 32bit C library, as well as 32 bit versions of whatever external libraries the program links against in some cases.
You may get a 32-bit binary by applying Alan Pearce’s method, but you may also get errors as follows:
fatal error: bits/predefs.h: No such file or directory
If this is the case and if you have apt-get, just install gcc-multilib
sudo apt-get install gcc-multilib
For any code that you compile directly using gcc / g++ , you will need to add -m32 option to the compilation command line, simply edit your CFLAGS , CXXFLAGS and LDFLAGS variables in your Makefile .
For any 3rd party code you might be using you must make sure when you build it to configure it for cross compilation. Run ./configure —help and see which option are available. In most cases you can provide your CFLAGS , CXXFLAGS and LDFLAGS variables to the configure script. You also might need to add —build and —host to the configure script so you end up with something like
./configure CFLAGS=-m32 CXXFLAGS=-m32 LDFLAGS=-m32 --build=x86_64-pc-linux-gnu --host=i686-pc-linux-gnu
If compilation fails this probably means that you need to install some 32 bit development packages on your 64 bit machine
GCC to compile compile 64 bit
I’m trying to compile C++ with G++ in 64 bit. I’m using sublime text 3. This is my build system file :
< "cmd": ["g++ ", "-m64", "-std=c++0x", "$file", "-o", "$/$"], "file_regex": "^(..[^:]*):(8+):?(9+). (.*)$", "working_dir": "$", "selector": "source.c, source.c++, source.cxx, source.cpp", "variants": [ < "name": "Run", "cmd": ["bash", "-c", "g++ -m64 -std=c++0x '$' -o '$/$.exe' && xterm -e bash -c '\"$/$.exe\" ; read'"] > ] >
I’m sure this has something to do with it, but I’m not sure how to fix it, (Pretty new to this), I’ve tried installing multilib 64 but nothing has changed. Any help appreciated. Thank you 🙂
Which operating system? What compiler and version? Also, try to build on the command line, outside of your editor. At first type compilation commands in your terminal. Be sure to compile with g++ -Wall -g -std=c++11 . Perhaps upgrade your GCC to at least GCC 6. So first learn to compile in your terminal, and later configure your IDE to compile the way you want it to.
On Linux and Unix, the right commands to understand if an executable is 32 or 64 bits are file and ldd ; the g++ —print-multi-lib gives info about compiler configuration
1 Answer 1
Your sublime text 3 IDE is confusing and spoiling you and you misconfigured it.
I hope that you are using some POSIX operating system, e.g. Linux.
Learn first to compile on the command line (in some terminal, outside of your editor or IDE). You absolutely need to understand how to invoke the GCC compiler (with commands in a terminal). Here are a few hints.
Be sure to have a recent GCC compiler, at least GCC 6 if you want C++11 or better. Perhaps you need to upgrade your compiler.
compiling a simple single-source program
To compile a single-source C++ program in single.cc into tinyprog executable, use
g++ -std=c++11 -Wall -g single.cc -o tinyprog
FYI, -std=c++11 sets your C++ standard, -Wall ask for almost all warnings (and you might even add -Wextra to get more of them), -g is for debug information (in DWARF) and -o tinyprog sets the output executable. Do ls -l tinyprog , ldd tinyprog , file tinyprog to understand more about your executable.
compiling a simple multi-source program
To compile a small multi-source C++ program, e.g. made of first.cc and second.cc into a smallprog executable
- compile each of these into object files
g++ -std=c++11 -Wall -g -c first.cc -o first.o g++ -std=c++11 -Wall -g -c second.cc -o second.o
g++ -std=c++11 -Wall -g first.o second.o -o smallprog
building more complex programs
If you are using extra libraries, you may need to add some extra -I includedir option at compile time, and some extra -L libdir and -l libname options at link time. You might want to use pkg-config to help you. You could, if curious, add -v after g++ to be shown the actual steps done by g++
This just queries how your g++ compiler has been configured. You could also try g++ -v alone.
Once you are able to compile by (one or several) commands, consider using some build automation tool. To build a single program using GNU make , you could take inspiration from this.
Once you are fluent with compiling thru commands (either directly or thru some build automation tool like make or ninja or thru your script), read the documentation of your editor or IDE to configure it suitably.
How to compile a C++ program as 64-bit on 64-bit machine?
Perhaps a very trivial question: I need to compile a program as 64-bit (earlier makefile written to compile it as 32-bit). I saw the option -m32 appearing in command line parameters with each file compilation. So, I modified the makefile to get rid of -m32 in OPTFLAG , but again when the program compiles, I still see -m32 showing up and binaries are still 32-bit. Does this m32 come from somewhere else as well?
What are you trying to compile? Perhaps you can reconfigure it with proper flags if it uses autotools instead of tweaking Makefile. If it is not your own software, you’d be better off asking corresponding community on how to properly build their software. Just from gcc point of view you need its proper version and -m64 gcc.gnu.org/onlinedocs/gcc/… .
make doesn’t predefine -m32, nor does any other system component (unless there are any relevant distro-specific modifications, but there usually aren’t), if that’s what you’re asking.
the «CPU you selected» message means there is an option like -march=i686 in the makefiles, which is not valid for 64-bit compilation, try removing that too, or adding -march=generic after it on the command line
3 Answers 3
-m32 can only be coming from somewhere in your makefiles, you’ll have to track it down (use a recursive grep) and remove it.
When I am able to force -m64, I get «CPU you selected does not support x86-64 instruction set».Any clues?. uname -a gives x86_64
That error means there is an option like -march=i686 in the makefiles, which is not valid for 64-bit compilation, try removing that too.
If you can’t remove it (try harder!) then adding -march=x86-64 after it on the command line will specify a generic 64-bit CPU type.
How to compile for Windows on Linux with gcc/g++?
So I was wondering if it is possible to have g++ make static compiled Windows executables that contains everything needed? I don’t have Windows, so it would be really cool, if I could do that on Linux 🙂
@AndiDog, «First dose for free», right. Anyway, setting up automated build process on Windows machine, while you have a completed and working one for Linux, is unnecessary.
@el.pescado, building and testing are completely different tasks. Windows is unnecessary for the former.
7 Answers 7
mingw32 exists as a package for Linux. You can cross-compile and -link Windows applications with it. There’s a tutorial here at the Code::Blocks forum. Mind that the command changes to x86_64-w64-mingw32-gcc-win32 , for example.
Ubuntu, for example, has MinGW in its repositories:
$ apt-cache search mingw [. ] g++-mingw-w64 - GNU C++ compiler for MinGW-w64 gcc-mingw-w64 - GNU C compiler for MinGW-w64 mingw-w64 - Development environment targeting 32- and 64-bit Windows [. ]
If you use debian, mingw32 is already in the repository, together with few precompiled libraries too.
Well, there’s a cross-compilation environment at nongnu.org/mingw-cross-env. It includes freeglut, for example. But I haven’t used this, so don’t ask me about it 😉
Does the «32» in mingw32 mean that I can only produce a 32-bit binary? Is there a solution to produce a 64-bit binary as well?
bluenote10: there are variants that build for x64, such as «x86_64-w64-mingw32-g++». The base «mingw32» may or may not be capable, but it’s easy enough to install/use the variants by name. ar2015: Does it not support C++11 at all or are you talking about a problem you had with it? I’m working on getting a project to build with mingw as we speak and this would be good information to know. Other threads indicate that it does support c++11 (e.g. stackoverflow.com/questions/16136142/…). Of course I’d be happy to help, but a separate post would be best for that.
Suggested method gave me error on Ubuntu 16.04: E: Unable to locate package mingw32
To install this package on Ubuntu please use following:
sudo apt-get install mingw-w64
After install you can use it:
For 64-bit use: x86_64-w64-mingw32-g++
For 32-bit use: i686-w64-mingw32-g++
I tried. It just throws all sort of errors regarding DWORD, LPVOID, WORD, BYTE and so on. Isn’t doing anything cross-compilation
@RichardMcFriendOluwamuyiwa Looks like you missed some headers to compile it on linux. It could generate binaries for running on Win machine. Please update your sources.
I made it work by including the windows.h before the winbase.h header. VS code was automatically rearranging the includes and leading to error so I had to force the order by empty comment lines
One option of compiling for Windows in Linux is via mingw. I found a very helpful tutorial here.
To install mingw32 on Debian based systems, run the following command:
sudo apt-get install mingw32
To compile your code, you can use something like:
i586-mingw32msvc-g++ -o myApp.exe myApp.cpp
You’ll sometimes want to test the new Windows application directly in Linux. You can use wine for that, although you should always keep in mind that wine could have bugs. This means that you might not be sure that a bug is in wine, your program, or both, so only use wine for general testing.
To install wine, run:
sudo apt-get install wine