0

My question is quite simple: Can we compile statically when using the libxml2? Actually, I spent an all day searching how it was possible to do that, but I clearly failed. When I run the following command line:

gcc -o exec `xml2-config --cflags` io1.c `xml2-config --libs` -static

I get a bunch of errors:

/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/libxml2.a(nanohttp.o) : dans la fonction « xmlNanoHTTPConnectHost » :
(.text+0xb3b): avertissement : Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/bin/ld: (.text+0x9ba): avertissement : Using 'gethostbyname' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/bin/ld : /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/libxml2.a(xpath.o) : dans la fonction « xmlXPathStringEvalNumber.part.0 » :
(.text+0x1f71) : référence indéfinie vers « pow »
/usr/bin/ld : (.text+0x1ffb) : référence indéfinie vers « pow »
...

When I use cmake it is stranger because I can compile but the executable does not work.

find_package(LibXml2 REQUIRED)
include_directories(${LIBXML2_INCLUDE_DIRS})

# create an executable
add_executable(conspiler ${SRCS} ${HEADERS})

target_compile_options(conspiler PUBLIC -std=c++11 -Wall -lz -O3 -lgmpxx -lgmp)
target_link_libraries (conspiler -lz -O3 -lgmpxx -lgmp libpatoh.a ${LIBXML2_LIBRARIES} -static)

install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/conspiler
DESTINATION bin
RENAME ${CMAKE_PROJECT_NAME}-conspiler)

I get (in french, but that means the executable cannot be found oO):

bash: ./conspiler: Aucun fichier ou dossier de ce type

Best regards, JM

JML
  • 31
  • 4
  • Does this answer your question? [Telling gcc directly to link a library statically](https://stackoverflow.com/questions/6578484/telling-gcc-directly-to-link-a-library-statically) – nwellnhof Jun 08 '20 at 20:46
  • Not exactly, I do not have problems when I create static library in general. With the library libxml it seems it is not possible to do that. I think it is because the library libxml2 is not static, but I really need to construct a static executable. I tried to follow this post: but that does not help, actually I do not understand everything ... – JML Jun 09 '20 at 06:51
  • There's `libxml2.a` in your error messages, so you do have a static version of libxml2. What happens if you compile with `-Wl,-Bstatic $(xml2-config --libs) -Wl,-Bdynamic`? – nwellnhof Jun 09 '20 at 09:48
  • I tested `gcc -o exec $(xml2-config --cflags) io1.c -Wl,-Bstatic $(xml2-config --libs) -Wl,-Bdynamic` and I got almost the same error. – JML Jun 09 '20 at 13:40
  • It is very strange, I use make VERBOSE=1 to print out the command line and I got something like `/usr/bin/c++ -O3 -DNDEBUG -rdynamic CMakeFiles/conspiler.dir/src/parser/XCSP3Manager.cc.o -o conspiler -L/home/jm/Works/ConSPiler/3rdparty/patoh -Wl,-rpath,/home/jm/Works/ConSPiler/3rdparty/patoh -lz -O3 -lgmpxx -lgmp -Wl,-Bstatic -lpatoh -Wl,-Bdynamic -Wno-class-memaccess -lxml2 -static`(I remove a lot of .o files to shorten the command line). And when I run conspiler I get: `bash: ./conspiler: No file or directory of this type`. I compiled and install libxml2 locally and that does not help. – JML Jun 09 '20 at 20:45
  • Don't add `-static`, only the `-Wl,-B` switches. – nwellnhof Jun 10 '20 at 12:53
  • If this doesn't work, please post the output of `xml2-config --libs` and `xml2-config --cflags`. If you're using a libxml2 distro package, tell us the exact OS and package version. If you built libxml2 from source, tell us the exact library version. – nwellnhof Jun 10 '20 at 15:59
  • I removed `-static` and that compile. But when I test on another compute that does not work (`./exec: /lib/x86_64-linux-gnu/libm.so.6: version 'GLIBC_2.29' not found (required by ./exec)`). `xml2-config --libs --cflags` returns `-L/home/jm/Works/ConSPiler/3rdparty/xmlinst/lib -lxml2 -lz -lm -I/home/jm/Works/ConSPiler/3rdparty/xmlinst/include/libxml2`. – JML Jun 11 '20 at 13:13

0 Answers0