2

I have a program that does not build with modern GCC with the foollowing output:

gcc -I/usr/lib/qt3/include -I/opt/kde3/include/  -fmessage-length=0 -O2 -Wall -D_FORTIFY_SOURCE=2 -fstack-protector -funwind-tables -fasynchronous-unwind-tables -lqt-mt -ldl -L/usr/lib/qt3/lib64 -o autocheck autocheck.cpp
autocheck.cpp: In function 'int main(int, char**)':
autocheck.cpp:64:62: warning: too many arguments for format
autocheck.cpp:79:79: warning: too many arguments for format
/tmp/ccOFReGf.o: In function `main':
autocheck.cpp:(.text+0x244): undefined reference to `dlopen'
autocheck.cpp:(.text+0x2e1): undefined reference to `dlerror'
collect2: ld returned 1 exit status

I searched the Internet for advise but only found a recommendation to add -ldl to the linker. But this does not help here. What should I do?

skaffman
  • 398,947
  • 96
  • 818
  • 769
Anixx
  • 139
  • 1
  • 1
  • 8

1 Answers1

15

Move autocheck.cpp so that it is before the libraries in your command. Libraries are only searched for things that need resolving in files that appear before them. So your command should look like this:

gcc autocheck.cpp -I/usr/lib/qt3/include -I/opt/kde3/include/  -fmessage-length=0 -O2 -Wall -D_FORTIFY_SOURCE=2 -fstack-protector -funwind-tables -fasynchronous-unwind-tables -lqt-mt -ldl -L/usr/lib/qt3/lib64 -o autocheck 
  • @neil-butterworth In my experience, this is only true about static libraries. On my machine `g++ -ldl -o foo foo.cpp` works (foo.cpp does reference dlopen). – n. m. could be an AI May 31 '11 at 06:58
  • How to tell what to configure? –  May 31 '11 at 06:59
  • @n.m I don't know whether libdl is a static or a dynamic library, but in any case this is good practice. –  May 31 '11 at 07:01
  • I added -ldl to LDFLAGS and LIBS environment variables but this does not help: both place -ldl before the cpp file. – Anixx May 31 '11 at 07:30
  • And -lqt-mt somehow works when placed before the file also so I doubt that the reason of the fault is in the placement of the option. – Anixx May 31 '11 at 07:32
  • @Anixx What I am telling you about the search order for libraries on the gcc command line is correct, though it can be modified via a variety of mechanisms. Have you tried running the modified command I provided in my answer from the command line? –  May 31 '11 at 07:36
  • Indeed. This helped: sed -i 's|$(QT_LIB) -L$(QT_LIBDIR) -o autocheck autocheck.cpp|-o autocheck autocheck.cpp $(QT_LIB) -L$(QT_LIBDIR)|g' autocheck/Makefile I just wonder why there is no such problem with other libraries? – Anixx Jun 01 '11 at 18:25