0

I am building an application in C on linux platform. In my program, I read XML document using libxml2 using the API xmlReadFile().

I link the program as below (line from makefile):

 $(CC) -m32 -g -o app.out -Wl,-Bstatic $(other_libs) -lmysqlclient -lxml2 -Wl,-Bdynamic -lpthread -lm -lrt -lz -lbz2 $(extra_ldflags)

With these settings everything works file. The API call to xmlReadFile() works fine.

If I move -lmysqlclient after -lxml2 the program generates below error in xmlReadFile().

File.xml:1: parser error : Document is empty

^
File.xml:1: parser error : Start tag expected, '<' not found

^

I actually want to link to mysql client dynamically (because I have libmysqlclient_r.so but not libmysqlclient_r.a and I want to use libmysqlclient_r.so because my application is multithreaded). Even if I keep the link order same and specify dynamic linking of mysql client library as below, I get the above mentioned error.

 $(CC) -m32 -g -o app.out -Wl,-Bstatic $(other_libs) -Wl,-Bdynamic -lmysqlclient -Wl,-Bstatic -lxml2 -Wl,-Bdynamic -lpthread -lm -lrt -lz -lbz2 $(extra_ldflags)

How do I resolve the above mentioned error?

I thank you in advance for your help.

geekowl
  • 331
  • 1
  • 6
  • 17
  • Are you sure that your XML document is valid? – evotopid Jul 03 '12 at 13:14
  • Yes. It is valid. The program reads the document perfectly before I change the link settings. The XML file is encoded in ascii character set. I specify the encoding in xmlReadFile() API. I tried other encodings but I get the same error. – geekowl Jul 03 '12 at 13:18
  • ok, sorry than I can't help you. I don't know nearly anything about linking... :D – evotopid Jul 03 '12 at 13:19
  • Thanks for looking at the post. – geekowl Jul 03 '12 at 13:20
  • 3
    This seems so weird to me that my guess would be that your program is buggy (invoking undefined behaviour ?); and whether it works or not depends (randomly) on details of the linking process. I would try starting without the mysqlclient library, and run the resulting libxml2 only program through e.g. valgrind. – user422005 Jul 03 '12 at 13:35
  • If I remove mysql code and remove -lmysqlclient, the program works fine. – geekowl Jul 03 '12 at 13:46
  • If I remove mysql code and put -lmysqlclient after -lxml2, I get the error! – geekowl Jul 03 '12 at 13:51
  • Well - have you verified a 100% correct Valgrind run in the case of no mysql? – user422005 Jul 03 '12 at 13:53
  • @geekowl Probably you have a bug in that code, that trashes memory that's important in one case, and trashes memory that's not important in the other case. – nos Jul 03 '12 at 13:53
  • I did valgrind on the program with mysql code removed and no -lmysqlclient. valgrind did not show any errors at runtime. I got some leak at exit but that is because I have not freed all resources on exit. If I add -lmysqlclient after -lxml2, the program crashes and valgrind prints out lot of lines. – geekowl Jul 03 '12 at 14:13
  • I have mysql-connector-c-6.0.2.tar.gz. When I compile it, it generates libmysqlclient.a but it does not generate libmysqlclient_r.a. It generates libmysqlclient_r.so. Do you know how I can generate libmysqlclient_r.a? – geekowl Jul 03 '12 at 14:21
  • I found out that I do not need to link to mysql client library dynamically. I can use libmysqlclient.a in multi-threaded application by following proper steps (mentioned here [link](http://dev.mysql.com/doc/refman/5.1/en/threaded-clients.html)). So I really do not need to change the link order of libmysqlclient and libxml2. Thank you all for your valuable suggestions. They were really helpful. – geekowl Jul 03 '12 at 21:21

0 Answers0