views:

457

answers:

3

I'm using Ubuntu 10.04 and Qt4.6, and I've created an executable binary file on my own computer through QtCreator.

Now I want to put my executable file on CentOS 5, but it seems that this executable file cannot run on CentOS.

The error message is

bash: ./[filename]: cannot execute binary file

Now I know this comes from 32-bits and 64-bits problem, and successfully create 32-bit exexutable file.

However, this executable file still cannot run on CentOS because of the dynamic linkage problem, it always shows that :

Error while loading shared libraries: libQtGUI.so.4: cannot open shared object file: No such file or directory

I tried to add the "-static" flag on .pro file

QMAKE_CFLAGS_RELEASE    += -Os -static
QMAKE_CPPFLAGS_RELEASE  += -Os -static
QMAKE_CXXFLAGS_RELEASE  += -Os -static
QMAKE_CCFLAGS_RELEASE   += -Os -static

however, looks like that it only generate "static binary" but not "static linked", the dependency still exists.

I also tried to add following line on .pro file:

QMAKE_LFLAGS += static

But this project cannot compile after doing this. I don't have permission to install Qt on Cent OS, how can I compile this project with static linkage so that the executable file can run independently?

Thanks for your help!

+1  A: 

There could be a handful of reasons for your executable not being able to run. However, check the dependencies first with "ldd" to get a clue.

shinkou
I checked the dependency with ldd, but it only shows that :"not a dynamic executable"
Claire Huang
+3  A: 

Check 64-bit vs. 32-bit - file(1) is your friend here. Then check what libraries are missing with ldd(1).

Edit:

Take a look at this SO question Qt static linking and deployment.

Nikolai N Fetissov
Oh I see the problem...My QtCreator is for 64-bit, and the CentOS is 32 bit.... Is there anyway to set it to 32 bit in QtCreator?
Claire Huang
You'll need the gcc option "-m32" and change the -L option to your 32bit library path. Take a look here and see if it's useful -> http://ubuntuforums.org/showthread.php?t=265585
shinkou
Thanks, that's helpful, now I'm trying to figure out how to set the compile parameter, after that this information will be helpful :)
Claire Huang
I've found how to set the parameter and can compile 32-bit file now. There is another problem, it seems there are some dependency on Qt so that it cannot run on a computer without Qt SDK. How can I get rid of this dependency??
Claire Huang
You need to either compile your project *statically* (look for `-static` compiler flag), or pack dynamic libraries your app depends on with the app itself and play with `LD_PRELOAD_PATH` environment variable on the target machine (read `ld.so(8)` man page.)
Nikolai N Fetissov
Thanks! I'm trying to build a static Qt now :). Hope that it will work
Claire Huang
+2  A: 

In general it's always a bad idea to run an executable from one distro on another. Apart from the architectural differences (32 vs 64 bits) you may also run into libraries incompatibilities, ABI changes, and other fun stuff. You can get rid of the libraries problem by compiling a static binary, but this comes with other drawbacks.

You should consider distributions as systems of their own, regardless of the fact they are all based on the Linux kernel, and compile binaries for each of those you want to support. The OpenSUSE Build Factory may help you if your goal is to provide binary packages.

Gnurou
Thanks :) I know it's not a best way to run executable file from different system, however, I don't have enough permission to install Qt on CentOS, and I cannot build an executable file on it, this is the problem ><.
Claire Huang