2

my program compiled and worked fine with:

g++ main.cpp exm1.cpp exm2.cpp -o main.o

i want to compile this app and run anywhere
how can i?
i try this code

g++ -g -Wall -I/MyApp/lib -static-libgcc -static-libstdc++ -static main.cpp exm1.cpp exm2.cpp -o main.o

but not work
in lib folder has 2 files:

  • exm1.h
  • exm2.h

    main.cpp included:
#include <fstream>
#include <iostream>
#include <string>
#include <streambuf>
#include <stdlib.h>
#include "lib/exm1.h"
#include "lib/exm2.h"

my linux is kali, and i want run this app on CentOS 6
please help me,thanks

nima
  • 177
  • 1
  • 1
  • 6

3 Answers3

1

As C. bear said, use the -m32 flag so the program is able to run on 64- and 32- bit systems. In doing this, You'll also have to install the 32bit stdlibs. I'm not sure how to do this on kali, give it a google. Another thing you can do to avoid having users install external libraries (if you use them at any point) is to statically link you executable. Use the -static flag to do this. However, because all your app's dependencies (including the stdlibs) will be in a single file, it will get pretty big. On the other hand, loading times will be better, because your app doesn't have to run the dynamic linker and wait for it to link your external libraries as you call them.

Lennon McLean
  • 308
  • 3
  • 12
0

So from comments one reason could be you executable is 64-bit and centOS a 32-bit sytem. Try compilation with -m32 flag.

c.bear
  • 1,197
  • 8
  • 21
  • `/usr/include/c++/4.9/iosfwd:38:28: fatal error: bits/c++config.h: No such file or directory #include ^ compilation terminated. ` – nima Sep 04 '15 at 14:47
  • 1
    http://stackoverflow.com/questions/4643197/missing-include-bits-cconfig-h-when-cross-compiling-64-bit-program-on-32-bit seems a frequent issue – c.bear Sep 04 '15 at 14:52
0

how to compile g++ app and run anywhere?

It is not easily possible. For example, an ELF executable produced by GCC on a recent Debian/x86-64 computer won't run on a RaspberryPi (with a linux for ARM 32 bits, e.g. some Raspbian)

Read more about execve(2), elf(5) and about fat binaries

Be aware that the GCC compiler suite can be built (from its source code) as a cross-compiler (but you'll need to rebuild GCC for every different target system).

I recommend to invoke GCC as g++ -Wall -Wextra -g during the debugging phase (and use GDB). Once your software is debugged, compile it with g++ -Wall -Wextra -O2 to get an optimized binary (or even compile and link it with g++ -Wall -Wextra -O3 -flto)

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547