0

I am trying to include pugixml.hpp in my code using the following include notation :

#include "pugixml-1.7/src/pugixml.hpp" 

Now the weird thing is, this runs in Codeblocks fine, but when I am trying to run it via terminal (I have all the source codes in one single file) using g++ :

g++-5 -v -std=c++11 -O3 -Wall -pedantic -fopenmp -pthread main.cpp -o main.o 

It throws out a bunch of undefined reference to pugi::xml.. (whichever the api I was using). I have tried everything, by keeping the pugixml.hpp file in the main directory, by adding bunch of flags like -iquote or -c to point to that directory.. but nothing is working. What else I should try? I am including the output till directory search here :

Using built-in specs. COLLECT_GCC=g++-5 COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/5/lto-wrapper Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Ubuntu 5.2.1-23ubuntu1~12.04' --with-bugurl=file:///usr/share/doc/gcc-5/README.Bugs --enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-5 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=gcc4-compatible --disable-libstdcxx-dual-abi --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-5-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-5-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-5-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64 --enable-multilib --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu Thread model: posix gcc version 5.2.1 20151031 (Ubuntu 5.2.1-23ubuntu1~12.04) COLLECT_GCC_OPTIONS='-v' '-std=c++11' '-O3' '-Wall' '-Wpedantic' '-fopenmp' '-pthread' '-o' 'main.o' '-shared-libgcc' '-mtune=generic' '-march=x86-64' '-pthread' /usr/lib/gcc/x86_64-linux-gnu/5/cc1plus -quiet -v -imultiarch x86_64-linux-gnu -D_GNU_SOURCE -D_REENTRANT main.cpp -quiet -dumpbase main.cpp -mtune=generic -march=x86-64 -auxbase main -O3 -Wall -Wpedantic -std=c++11 -version -fopenmp -fstack-protector -Wformat-security -o /tmp/ccXfMWIT.s GNU C++11 (Ubuntu 5.2.1-23ubuntu1~12.04) version 5.2.1 20151031 (x86_64-linux-gnu) compiled by GNU C version 5.2.1 20151031, GMP version 5.0.2, MPFR version 3.1.0-p3, MPC version 0.9 GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072 ignoring duplicate directory "/usr/include/x86_64-linux-gnu/c++/5" ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu" ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/5/../../../../x86_64-linux-gnu/include" #include "..." search starts here: #include <...> search starts here: /usr/include/c++/5 /usr/include/x86_64-linux-gnu/c++/5 /usr/include/c++/5/backward /usr/lib/gcc/x86_64-linux-gnu/5/include /usr/local/include /usr/lib/gcc/x86_64-linux-gnu/5/include-fixed /usr/include/x86_64-linux-gnu /usr/include End of search list. 
7
  • 1
    Link required libraries. Commented Apr 13, 2016 at 16:18
  • i have linked all required libraries using the "#include". pugixml takes only two files, pugixml.hpp and pugixmlconfig.hpp Commented Apr 13, 2016 at 16:23
  • You may need to provide the include path on the command line. (ie. g++ -I/path/to/includes ...). Commented Apr 13, 2016 at 16:23
  • @callyalater do you mean by using -c flag? tried it too, didn't work :( Commented Apr 13, 2016 at 16:24
  • @KoustuvSinha Are you sure? I also see pubixml.cpp in the repository. (Note: Do not use #include for "linking" cpp files) Commented Apr 13, 2016 at 16:25

2 Answers 2

2

You need to link against the compiled pugixml library. Including the pugixml.hpp is simply telling the preprocessor to get the interface, however the compiler (more specifically the linker) needs to find the appropriate compiled pugixml code for it to be usable from your source code.

Sign up to request clarification or add additional context in comments.

3 Comments

do u mean i need to link pugixml.o in the code compilation? which flag does that?
@KoustuvSinha This website might help out.
@Koustav Sinha: Yes. I haven't used gcc from the command line directly in a long time, but I believe you do that with the -o. In cmake it would be target_link_libraries().
0

As others have pointed out, you need to link the library. Based on your compiler output, you seem to be running Ubuntu. The easiest way to install the library on Ubuntu is by installing the libpugixml-dev package. This will install pugixml.hpp under /usr/include, and libpugixml.so under /usr/lib.

sudo apt install libpugixml-dev 

One of the standard ways to use installed libraries is by including the output of pkg-config on the compiler command line:

g++ -O3 -Wall -Wextra -pedantic -fopenmp -pthread $(pkg-config --libs --cflags pugixml) -o main main.cpp 

Alternatively, you can add the -lpugixml flag yourself to link against libpugixml.so, and rely on the headers being automatically found under /usr/include.

g++ -O3 -Wall -Wextra -pedantic -fopenmp -pthread -lpugixml -o main main.cpp 

For all of the above to work, you will need to change your include line to not include the installation path, only pugixml.hpp:

#include "pugixml.hpp" 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.