linux g++编译外部hpp文件上未定义的引用

00jrzges  于 2023-05-22  发布在  Linux
关注(0)|答案(2)|浏览(117)

我尝试使用以下include表示法在代码中包含pugixml.hpp

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

现在奇怪的是,这在代码块中运行得很好,但是当我试图通过终端运行它时(我在一个文件中有所有的源代码)使用g++:

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

它抛出了一堆undefined reference to pugi::xml..(无论我使用的是哪个API)。我已经尝试了所有方法,通过将pugixl.hpp文件保存在主目录中,通过添加一堆像-iquote-c这样的标志来指向该目录。但都不管用我还应该尝试什么?我在这里包含了目录搜索的输出:

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.
xggvc2p6

xggvc2p61#

您需要链接到已编译的pugixml库。包含pugixml.hpp只是告诉预处理器获取接口,但是编译器(更具体地说是链接器)需要找到适当的编译后的pugixml代码,以便从源代码中使用。

n53p2ov0

n53p2ov02#

正如其他人所指出的,您需要链接库。根据编译器的输出,您似乎正在运行Ubuntu。在Ubuntu上安装库的最简单方法是安装libpugixml-dev包。这将在/usr/include下安装pugixml.hpp,并在/usr/lib下安装libpugixml.so

sudo apt install libpugixml-dev

使用已安装库的标准方法之一是在编译器命令行中包含pkg-config的输出:

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

或者,您可以自己添加-lpugixml标志来链接libpugixml.so,并依赖于在/usr/include下自动找到的头。

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

要使以上所有内容都能正常工作,您需要更改include行以不包含安装路径,仅包含pugixml.hpp

#include "pugixml.hpp"

相关问题