Need help - Can't run a simple C++ application

Do you have a question? Post it now! No Registration Necessary

Translate This Thread From English to

Threaded View
Hi,

I'm quite new at Linux but I have been developing for Win32 for many
years. I wrote an application in C++/Win32 a couple of years ago and I
have now been asked to port it to an embedded Linux computer (please
see hardware and software details at the end of this post).

Since Linux is new to me, I first tried compiling on my development
workstation a simple "Hello World" program written in C. The program
runs fine on the target computer.

I then tried compiling a C++ version of the Hello World program (with
only a single class) using g++, but this program does not work on the
target. It complains about certain libraries that were missing.

To find out the dependencies of the program, I ran on the workstation:
# ldd hello
libstdc++.so.5 => /usr/lib/libstdc++.so.5 (0x4001e000)
libm.so.6 => /lib/libm.so.6 (0x400d0000)
libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x400f2000)
libc.so.6 => /lib/libc.so.6 (0x400fb000)
/lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)

And here is the list of libraries already present on the target
computer:
# ls /lib
ld-linux.so.2             libext2fs.so.2          
libstdc++-libc6.2-2.so.3
libc.so.6
libcom_err.so.2           libm.so.6                 libutil.so.1
libcrypt.so.1             libnsl.so.1               libuuid.so.1
libdl.so.2                libnss_files.so.2
libe2p.so.2               libresolv.so.2

I copied the libraries libgcc_s.so.1 and libstdc++.so.5 from the
development workstation to the target computer. I did not copy
libc.so.6, libm.so.6 or ld-linux.so.2 because then already exist on
the target.

Now when I run hello on the target it gives the error:
./hello: /lib/libc.so.6: version `GLIBC_2.3' not found (required by
/lib/libstdc++.so.5)

I tried copying the libc.so.6 library from the workstation to the
target, but this crashed the target so bad and I had to reGhost the
drive. I now know better than to overwrite those libraries.

Can anyone help me understand how to select and install the correct
libraries in order to run a C++ application on the target?

HARDWARE/SOFTWARE DETAILS
-------------------------
Development workstation:
Intel PC with Mandrake Linux 9.1
Compiling with g++ 3.2.2

Target computer:
ICOP 6015
Embedded 386SX Tiny board with 2S/4MB DRAM onboard/GPIO/Ethernet
http://www.icoptech.com/products_detail.asp?ProductID=8
I comes with an OS called X-Linux preinstalled. They told me that it
is a trimmed down version of RedHat Linux.

Thanks.

Eric

Re: Need help - Can't run a simple C++ application
snipped-for-privacy@hotmail.com (Eric) writes:

Quoted text here. Click to load it

Since your workstation is running a more recent glibc, you can't
simply install correct libraries on the target (libc, libm and
ld-linux all form a "package", and must be upgraded together;
you saw what happens when they are not).

Your choices are (in order of increasing difficulty, I think):

- upgrade target to the same version of glibc as that on workstation.
- install a gcc x86 => x86 cross-compiler (details in "info gcc").
- downgrade workstation to the same version of glibc as that on the
  target (you'll have to find a distribution that used the "old" glibc;
  simply downgrading glibc by itself will likely render the
  workstation unusable).

Cheers,
--
In order to understand recursion you must first understand recursion.

Re: Need help - Can't run a simple C++ application
Quoted text here. Click to load it

Or link the program statically  (e.g. with the compiler switch
"-all-static")  and run "strip programname" afterwards. Of course, this is
not a good idea if you have more programs as each will become several 100
kbytes larger.

Greetings
Ernst




Re: Need help - Can't run a simple C++ application
Quoted text here. Click to load it

Ernst,

Eventually I will want to link with shared libraries because I will be
developing other applications for the target. Right now I will try to
link the program statically, this will allow me to start development
right now and give me more time to resolve the shared library issue
later.

I tried the compiler switch you suggested, but it errors:
# g++ -all-static -o hello.out hello.cpp
cc1plus: unrecognized option `-all-static'

So I tried this:
# g++ -static -o hello.out hello.cpp
/usr/bin/ld: cannot find -lstdc++
collect2: ld returned 1 exit status

I don't know why it does not find libstdc++, I think it's there:
# ls /usr/lib/*stdc++*
/usr/lib/libstdc++-3-libc6.2-2-2.10.0.so*  /usr/lib/libstdc++.so.5@
/usr/lib/libstdc++-libc6.1-1.so.2@        
/usr/lib/libstdc++.so.5.0.3*
/usr/lib/libstdc++-libc6.2-2.so.3@

Is it because I only have the shared version of the library and not
the static version? If so, how do I get the static version?

Thanks for the help.

Eric

Re: Need help - Can't run a simple C++ application
Hello Eric,

Quoted text here. Click to load it

-all-static seems to work only after the -o flag. I use KDevelop and added
# the library search path:
myprogram_LDFLAGS = $(all_libraries) -all-static

If I use -all-static on another place (e.g. like in your example) it won't
work either.

Quoted text here. Click to load it

Yes. you need the *.a or *.o files.
*.a is an archive of  *.o files.

Quoted text here. Click to load it

maybe here: http://www.gnu.org/software/gcc/libstdc ++/
If you are using a distribution, it might be on the CD as a separate
package.
Otherwise, I guess, you have to "make" it from the source.
But I have never done this myself. 8-)

Greetings
Ernst

PS: I think configuration of the development system is more complicated than
programming.  8-)



Re: Need help - Can't run a simple C++ application
<snip>
Quoted text here. Click to load it

That's correct.

That depends on which distribution of Linux you are using.  If it's
rpm-based, you should look for the correct rpm (try http://rpmfind.net ).
  If it's deb-based, look for the correct debian package.  If it's
neither, download and build the source.

- Adam

--
Reverse domain name to reply.


Re: Need help - Can't run a simple C++ application
Hello Ernst and Adam,

I finally succeeded in compiling the thing statically.

By reading your replies, I understood that I had to find the static
versions of the libraries. I had a look on rpmfind.net and searched
for stdc++* and saw the library
libstdc++5-static-devel-3.2.2-3mdk.i586.rpm for my Mandrake
distribution. I then took a second look at the CDs that I had used for
the installation and sure enough, it was there, so I installed it and
recompiled. When I saw that the error message had changed and said it
could not find libm, I knew I was making progress. I installed the
static version of glibc, recompiled and voilÓ. I now have a whopping
665KB "Hello World" program! Not very compact, but it's a first step.
I runs fine on the embedded device.

I simply used the following command:
$ g++ -static -o hello.out hello.cpp

I'll post back here later when I'll need to link to the shared
libraries, but in the mean time I can start working on my code.

Thank you all for your help.

Eric

Re: Need help - Can't run a simple C++ application
 I now have a whopping
Quoted text here. Click to load it

Even with the strip command?
strip would remove unused functions.

greetings
ernst




Re: Need help - Can't run a simple C++ application
Quoted text here. Click to load it

I'd be interested to see how strip does that. How does it know that a
function is not used?
46%rom the finely printed manual:

    strip - Discard symbols from object files.

-- =

Josef MF6%llers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
We've slightly trimmed the long signature. Click to see the full one.
Re: Need help - Can't run a simple C++ application

Quoted text here. Click to load it

It discards debugging symbols (typically).  As you can see below,
there are debugging sections of the binary, which strip removes.  You
can also use it to remove specific sections (not commonly used).  This
is a ~reasonably large C++ application compiled using GCC 3.3.2.
strip removes the DWARF2 debugging symbol tables:

[before strip]
-rwxr-xr-x    1 roger    roger     6204796 2003-11-17 15:51 someprog

[objdump -h]
someprog:     file format elf32-i386

Sections:
Idx Name          Size      VMA       LMA       File off  Algn
  0 .interp       00000013  08048114  08048114  00000114  2**0
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  1 .note.ABI-tag 00000020  08048128  08048128  00000128  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  2 .hash         00002950  08048148  08048148  00000148  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  3 .dynsym       000064b0  0804aa98  0804aa98  00002a98  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  4 .dynstr       00010ab2  08050f48  08050f48  00008f48  2**0
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  5 .gnu.version  00000c96  080619fa  080619fa  000199fa  2**1
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  6 .gnu.version_r 00000080  08062690  08062690  0001a690  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  7 .rel.dyn      000001c8  08062710  08062710  0001a710  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  8 .rel.plt      00000f78  080628d8  080628d8  0001a8d8  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  9 .init         00000017  08063850  08063850  0001b850  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
 10 .plt          00001f00  08063868  08063868  0001b868  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
 11 .text         000446b0  08065770  08065770  0001d770  2**4
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
 12 .fini         0000001b  080a9e20  080a9e20  00061e20  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
 13 .rodata       00011a45  080a9e40  080a9e40  00061e40  2**5
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
 14 .eh_frame_hdr 00000fbc  080bb888  080bb888  00073888  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
 15 .data         0000000c  080bd844  080bd844  00074844  2**2
                  CONTENTS, ALLOC, LOAD, DATA
 16 .eh_frame     00004774  080bd850  080bd850  00074850  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
 17 .gcc_except_table 00007dec  080c1fc4  080c1fc4  00078fc4  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
 18 .dynamic      000001a0  080c9db0  080c9db0  00080db0  2**2
                  CONTENTS, ALLOC, LOAD, DATA
 19 .ctors        00000030  080c9f50  080c9f50  00080f50  2**2
                  CONTENTS, ALLOC, LOAD, DATA
 20 .dtors        00000008  080c9f80  080c9f80  00080f80  2**2
                  CONTENTS, ALLOC, LOAD, DATA
 21 .jcr          00000004  080c9f88  080c9f88  00080f88  2**2
                  CONTENTS, ALLOC, LOAD, DATA
 22 .got          000007cc  080c9f8c  080c9f8c  00080f8c  2**2
                  CONTENTS, ALLOC, LOAD, DATA
 23 .bss          000006ec  080ca758  080ca758  00081758  2**3
                  ALLOC
 24 .comment      000002be  00000000  00000000  00081758  2**0
                  CONTENTS, READONLY
 25 .debug_aranges 00000ec0  00000000  00000000  00081a18  2**3
                  CONTENTS, READONLY, DEBUGGING
 26 .debug_pubnames 0000d189  00000000  00000000  000828d8  2**0
                  CONTENTS, READONLY, DEBUGGING
 27 .debug_info   00491b89  00000000  00000000  0008fa61  2**0
                  CONTENTS, READONLY, DEBUGGING
 28 .debug_abbrev 00010f19  00000000  00000000  005215ea  2**0
                  CONTENTS, READONLY, DEBUGGING
 29 .debug_line   0002f652  00000000  00000000  00532503  2**0
                  CONTENTS, READONLY, DEBUGGING
 30 .debug_frame  00005d58  00000000  00000000  00561b58  2**2
                  CONTENTS, READONLY, DEBUGGING
 31 .debug_str    00069d4d  00000000  00000000  005678b0  2**0
                  CONTENTS, READONLY, DEBUGGING
 32 .debug_ranges 00000e48  00000000  00000000  005d15fd  2**0
                  CONTENTS, READONLY, DEBUGGING

[after strip]
-rwxr-xr-x    1 roger    roger      532268 2003-11-18 22:47 someprog

[objdump -h]
someprog:     file format elf32-i386

Sections:
Idx Name          Size      VMA       LMA       File off  Algn
  0 .interp       00000013  08048114  08048114  00000114  2**0
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  1 .note.ABI-tag 00000020  08048128  08048128  00000128  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  2 .hash         00002950  08048148  08048148  00000148  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  3 .dynsym       000064b0  0804aa98  0804aa98  00002a98  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  4 .dynstr       00010ab2  08050f48  08050f48  00008f48  2**0
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  5 .gnu.version  00000c96  080619fa  080619fa  000199fa  2**1
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  6 .gnu.version_r 00000080  08062690  08062690  0001a690  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  7 .rel.dyn      000001c8  08062710  08062710  0001a710  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  8 .rel.plt      00000f78  080628d8  080628d8  0001a8d8  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  9 .init         00000017  08063850  08063850  0001b850  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
 10 .plt          00001f00  08063868  08063868  0001b868  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
 11 .text         000446b0  08065770  08065770  0001d770  2**4
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
 12 .fini         0000001b  080a9e20  080a9e20  00061e20  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
 13 .rodata       00011a45  080a9e40  080a9e40  00061e40  2**5
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
 14 .eh_frame_hdr 00000fbc  080bb888  080bb888  00073888  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
 15 .data         0000000c  080bd844  080bd844  00074844  2**2
                  CONTENTS, ALLOC, LOAD, DATA
 16 .eh_frame     00004774  080bd850  080bd850  00074850  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
 17 .gcc_except_table 00007dec  080c1fc4  080c1fc4  00078fc4  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
 18 .dynamic      000001a0  080c9db0  080c9db0  00080db0  2**2
                  CONTENTS, ALLOC, LOAD, DATA
 19 .ctors        00000030  080c9f50  080c9f50  00080f50  2**2
                  CONTENTS, ALLOC, LOAD, DATA
 20 .dtors        00000008  080c9f80  080c9f80  00080f80  2**2
                  CONTENTS, ALLOC, LOAD, DATA
 21 .jcr          00000004  080c9f88  080c9f88  00080f88  2**2
                  CONTENTS, ALLOC, LOAD, DATA
 22 .got          000007cc  080c9f8c  080c9f8c  00080f8c  2**2
                  CONTENTS, ALLOC, LOAD, DATA
 23 .bss          000006ec  080ca758  080ca758  00081758  2**3
                  ALLOC
 24 .comment      000002be  00000000  00000000  00081758  2**0
                  CONTENTS, READONLY


<OT>Using readelf -w, I can see the details of the Debian build daemon
that built my glibc--a potential security concern?

     DW_AT_comp_dir    : /build/buildd/glibc-2.3.2.ds1/build-tree/glibc-2.3.2/csu
     DW_AT_producer    : GNU AS 2.14.90.0.7
     DW_AT_language    : 32769  (MIPS assembler)
                                 ^^^^^^^^^^^^^^Huh, I'm using ix86!
</OT>


--
Roger Leigh

                Printing on GNU/Linux?  http://gimp-print.sourceforge.net/
We've slightly trimmed the long signature. Click to see the full one.
Re: Need help - Can't run a simple C++ application
Quoted text here. Click to load it

How does this contradict what I wrote? As you write yourself: it just
removes debugging information.
All sections which contain CODE are left untouched:
2E%init, .plt, .text, .fini,
so no unused functions have been removed.

-- =

Josef MF6%llers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
We've slightly trimmed the long signature. Click to see the full one.
Re: Need help - Can't run a simple C++ application
Quoted text here. Click to load it

AFAIK, unused functions are already removed by the linker.

-Michael

Re: Need help - Can't run a simple C++ application
Quoted text here. Click to load it

They could even be removed by the code generator itself (an unused
static function does not need code to be generated).
As for the linker, IMHO this would violate the task of the linker. It
would re-arrange code within a linkable object. But then, it could do
this, because it knows that there is no reference to the function label.

-- =

Josef MF6%llers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
We've slightly trimmed the long signature. Click to see the full one.
Re: Need help - Can't run a simple C++ application
Quoted text here. Click to load it
OK, I think I was wrong. according to the man page unused relocation
information and debugging symbols are removed. In my case this is 3/4 of the
file size.

But why cannot functions be removed (what is unused relocation information)?
I could imagine that a segment, where no jump goes to, could be removed.

Greetings
Ernst




Re: Need help - Can't run a simple C++ application
Quoted text here. Click to load it

(I'm not a C++ expert, so I give this example in C):
static void f();
main()
{
    void (*p)() 3D% &f;
    (*f)();
}
void f(){ printf("Hello, world!\n"); }

No jump to f, but f is not unused. You could make this arbitrarily
complex.

As for the "unused relocation information": if you're referring to the
"--strip-unneeded" option, the relocation table contains reference to
symbols which must be relocated during linking. Those entries in the
symbol table which are not referenced in the relocation table can be
removed. A quick test on a simple program reveals that gcc2_compiled is
one of such symbols.

Josef
-- =

Josef MF6%llers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
                        -- T.  Pratchett

Re: Need help - Can't run a simple C++ application
Quoted text here. Click to load it

The linker removes functions that are not _ referenced _.

Re: Need help - Can't run a simple C++ application
On Wed, 19 Nov 2003 11:11:47 +0100, Michael Schnell

Quoted text here. Click to load it

Any sensible linker with some kind of library utility would not
include any library routines that are not referenced by the main
program or modules needed by the main program.

However, if the execution environment supports dynamic linking (such
as xxx.dll or xxx.so files), it would be quite dangerous to remove any
globally available symbol (variable or function) from the final image,
if the module has been specified on the linker command line, since the
linker can not know, if this symbol will ever be referenced by any
dynamically loaded function during run time.

Paul
 

Re: Need help - Can't run a simple C++ application
Quoted text here. Click to load it

Up to now I never considered dynamic linking and so I don't know what
goes on there (but I did watch the GCC linker working for statically
linked projects in embedded environments). I suppose you need to tell
the linker about the dynamic option.

Does dynamic linking include a jump table to access the referenced
functions or do the calls go directly in the midst of the code ?

-Michael

Re: Need help - Can't run a simple C++ application
Ernst,

Thanks for reminding me about using strip. It works, it reduced the
file size from 664,759 bytes to 420,024 bytes for my "Hello World"
test program.

I then tried it on my project executable and the results were more
notable, it reduced the file size from 3,753,183 to 852,616 bytes.

Regards,

Eric

Re: Need help - Can't run a simple C++ application
This is because you lib versions differ. You can't mix glibc-2.1 with
glibc-2.3 for example - you need to compile against the target libs
(this is why cross toolchains are used even if your target cpu is the
same as the one on your host). If you don't want to add the missing libs
on your target you can link statically to them and dynamically to the
ones that are present there.

Eric wrote:
Quoted text here. Click to load it


--
Alexander Popov                    ProSyst Bulgaria Inc.
RTOS Leader                        48 Vladajska Str.
We've slightly trimmed the long signature. Click to see the full one.

Site Timeline