C++ Dynamically Loaded Libraries & Automake (invalid ELF library error)
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
C++ Dynamically Loaded Libraries & Automake (invalid ELF library error)
Hi, I'm in big big troubles using dynamically loaded libraries under Linux. I've created a source tree using the acmkdir tool, then I decided to structure my project as follows:
src/ /* source code of main program */
src/libLine/ /* source code of dyn-loaded library */
The program I'm going to develop should use wxWidgets and their wrapper for OpenGL to draw geometric primitives as stated in a XML file or instructed through a Unix socket. I thought to put all OpenGL-related stuff in separate classes loaded at runtime (so that, if anyone wants to provide another geometric primitive, it just has to develop one accordingly to the known interface and put the library file in the proper directory).
I wanted to load the library using dlopen() and the other tools, but I was not sure of compiling options, so I've made a test program and tried them. The 'reconf' script complained about the fact that the library wasn't give a ".la" name, so I've changed it and changed accordingly the names in Makefile.am, then I've compiled the whole thing.
It should be pointed out that several options were tried before the ones that you can see, I've searched around the web for hours and experimented different values, but either refuses to compile (and the 'reconf' script complains about wrong parameters) or compiles successfully but, when the executable is run, it prompts out the error:
roby@archimedes:~/3DVisor/src$ ./3DVisor
./libLine/libLine.la: invalid ELF header
I'm literally going insane with it! Thanks in advance to anyone who is willing to help me.
You need to use -ldl instead of -dlopen. Also, you never need to supply the library and compilation flags when using automake, so you can essentially clear *_CXXFLAGS and *_LDFLAGS since their options won't port to all versions of cc. Try those things and see where you get.
ta0kira
I've exported the LD_LIBRARY_PATH variable (otherwise I could only obtain roby@archimedes:~$ 3DVisor
libLine.la: cannot open shared object file: No such file or directory)
but the error remains the same: roby@archimedes:~$ 3DVisor
/usr/local/lib/libLine.la: invalid ELF header
Using strace I obtain the following
Code:
roby@archimedes:~$ strace 3DVisor
execve("/usr/local/bin/3DVisor", ["3DVisor"], [/* 38 vars */]) = 0
brk(0) = 0x602000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x2b1685e20000
uname({sys="Linux", node="archimedes", ...}) = 0
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x2b1685e21000
access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
open("/usr/local/lib/tls/x86_64/libdl.so.2", O_RDONLY) = -1 ENOENT (No such file or directory)
stat("/usr/local/lib/tls/x86_64", 0x7fff24ca3720) = -1 ENOENT (No such file or directory)
open("/usr/local/lib/tls/libdl.so.2", O_RDONLY) = -1 ENOENT (No such file or directory)
stat("/usr/local/lib/tls", 0x7fff24ca3720) = -1 ENOENT (No such file or directory)
open("/usr/local/lib/x86_64/libdl.so.2", O_RDONLY) = -1 ENOENT (No such file or directory)
stat("/usr/local/lib/x86_64", 0x7fff24ca3720) = -1 ENOENT (No such file or directory)
open("/usr/local/lib/libdl.so.2", O_RDONLY) = -1 ENOENT (No such file or directory)
stat("/usr/local/lib", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
open("/etc/ld.so.cache", O_RDONLY) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=151601, ...}) = 0
mmap(NULL, 151601, PROT_READ, MAP_PRIVATE, 3, 0) = 0x2b1685e23000
close(3) = 0
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
open("/lib/libdl.so.2", O_RDONLY) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0 \16\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0644, st_size=14624, ...}) = 0
mmap(NULL, 2109728, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x2b1686021000
mprotect(0x2b1686023000, 2097152, PROT_NONE) = 0
mmap(0x2b1686223000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x2000) = 0x2b1686223000
close(3) = 0
open("/usr/local/lib/libstdc++.so.6", O_RDONLY) = -1 ENOENT (No such file or directory)
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
open("/usr/lib/libstdc++.so.6", O_RDONLY) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0 I\5\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0644, st_size=1014808, ...}) = 0
mmap(NULL, 3186696, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x2b1686225000
mprotect(0x2b1686315000, 2093056, PROT_NONE) = 0
mmap(0x2b1686514000, 36864, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0xef000) = 0x2b1686514000
mmap(0x2b168651d000, 73736, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x2b168651d000
close(3) = 0
open("/usr/local/lib/libm.so.6", O_RDONLY) = -1 ENOENT (No such file or directory)
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
open("/lib/libm.so.6", O_RDONLY) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\320>\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0644, st_size=526568, ...}) = 0
mmap(NULL, 2621672, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x2b1686530000
mprotect(0x2b16865b0000, 2093056, PROT_NONE) = 0
mmap(0x2b16867af000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x7f000) = 0x2b16867af000
close(3) = 0
open("/usr/local/lib/libgcc_s.so.1", O_RDONLY) = -1 ENOENT (No such file or directory)
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
open("/lib/libgcc_s.so.1", O_RDONLY) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\240!\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0644, st_size=56072, ...}) = 0
mmap(NULL, 2151816, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x2b16867b1000
mprotect(0x2b16867be000, 2097152, PROT_NONE) = 0
mmap(0x2b16869be000, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0xd000) = 0x2b16869be000
close(3) = 0
open("/usr/local/lib/libc.so.6", O_RDONLY) = -1 ENOENT (No such file or directory)
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
open("/lib/libc.so.6", O_RDONLY) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0`\334\1\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=1408312, ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x2b16869bf000
mmap(NULL, 3514744, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x2b16869c0000
mprotect(0x2b1686b12000, 2093056, PROT_NONE) = 0
mmap(0x2b1686d11000, 20480, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x151000) = 0x2b1686d11000
mmap(0x2b1686d16000, 16760, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x2b1686d16000
close(3) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x2b1686d1b000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x2b1686d1c000
arch_prctl(ARCH_SET_FS, 0x2b1686d1bb10) = 0
mprotect(0x2b1686d11000, 12288, PROT_READ) = 0
mprotect(0x2b1686514000, 24576, PROT_READ) = 0
munmap(0x2b1685e23000, 151601) = 0
open("/usr/local/lib/lib3DVisline.la", O_RDONLY) = 3
read(3, "# lib3DVisline.la - a libtool li"..., 832) = 832
close(3) = 0
brk(0) = 0x602000
brk(0x623000) = 0x623000
write(2, "/usr/local/lib/lib3DVisline.la: "..., 50/usr/local/lib/lib3DVisline.la: invalid ELF header) = 50
write(2, "\n", 1
) = 1
exit_group(-1) = ?
Process 11153 detached
I've experimented a bit more. It seems like it wants to be linked against a .so file, but those are generated only during the "make install" phase, and even in that case it always requires a ".la" library. I've used readelf to try to figure out what happens, it returns me an error: readelf: Error: Unable to read in 0x6e65 bytes of section headers
readelf: Error: Not an ELF file - it has the wrong magic bytes at the start
when run on the ".la" file, but it returns as expected when run on the ".so" file extracted from it. Being close to desperation, I tried to compile/link it by hand, but no way, it requests me the dam*ed ".la" file just to complain that it is not an ELF file...
No, you need to link to the .la files since automake is meant to decide between static and shared versions (because some systems don't have shared libs.) You need to add ./libLine/libLine.la (or ./libLine/lib3DVisline.la, whichever) back into 3DVisor_LDADD. Also, add this line to src/Makefile.am:
I had already fixed the SUBDIRS issue because I found it in one of the tutorials I read trying to fix the problem. The problem however remains even with those changes. I've uploaded the source tree on the web, it can be downloaded from here: Source tree
Thank you very much!
I finally solved the issue, it was just a wrong library name left in main.cc while trying to get right compile options
I'd like to give a big thank to ta0kira for the help!
I have a similar problem. I need to specify which .so libraries to use for the linking process. In which file do I need to include this information (makefile.am ??). And what exactly do I need to write (I saw a lot of things in your makefile.am sample) ?
I have a similar problem. I need to specify which .so libraries to use for the linking process. In which file do I need to include this information (makefile.am ??). And what exactly do I need to write (I saw a lot of things in your makefile.am sample) ?
Thanks
Normand Bedard (normand.bedardATgmail.com)
For dynamical linking you have to specify in the Makefile.am file of your application the "-rdynamic" flag in this way
Code:
programname_CXXFLAGS = -rdynamic
Then, in the linking part of the Makefile.am file, you must specify where the library to link against to is located
If you use Libtool, you have to set the corresponding flags in the configure.ac file in your sources' root directory, and you must also tell Autotools that you're going to use dl_open()
Code:
AC_LIBTOOL_DLOPEN
AC_PROG_LIBTOOL
AC_PROG_RANLIB
If you want I can send you the entire (working) version of my project, so that you can simply do some cut&paste to solve your problem...
Hope this helps!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.