尝试使用外部头编译程序时出现问题
我正在学习 C++,我正在尝试在我的程序中使用从互联网下载的库(从这里 https ://mattmccutchen.net/bigint/)。 因为我希望一切都非常整洁,所以我将所有 .hh 文件放在名为“BI”的子文件夹中。 但是,当我尝试使用 g++(Windows XP SP3 上的 MinGW)编译 .cpp 文件时,编译器输出以下错误:
J:\comp proj\FS>J:\Programmi\MinGW\bin\g++.exe "J:\comp proj\FS\test.cpp" -o "J:\comp proj\FS\test.exe" -I “J:\comp proj\FS\BI” E:\DOCUME~1\MrJackV\IMPOST~1\Temp\ccidH1Z6.o:test.cpp:(.text+0x2c): 对 BigInteger::BigInteger(int)' 的未定义引用 E:\DOCUME~1\MrJackV\IMPOST~1\Temp\ccidH1Z6.o:test.cpp:(.text+0x11b): 对运算符的未定义引用<<(std::ostream&, BigInteger const& ;)' E:\DOCUME~1\MrJackV\IMPOST~1\Temp\ccidH1Z6.o:test.cpp:(.text$ZNK10BigIntegermlERKS[BigInteger::operator*(BigInteger const&) const]+0x29 ): 未定义的参考 到 `BigInteger::multiply(BigInteger const&, BigInteger const&)' Collect2: ld 返回 1 退出状态
我尝试使用-I、-l 和-L
开关来解决该问题,但没有成功。 此外,我尝试在 cpp 中添加 #include "BI/BigIntegerLibrary.hh"
但这不起作用。
我做错了什么吗?
提前致谢。
I'm learning c++ and I'm trying to use a library that I've downloaded from internet in my program ( from here https://mattmccutchen.net/bigint/).
Because I want everything to be quite tidy, I put all the .hh files in a subfolder named "BI".
However when I try to compile my .cpp file with g++ (It's MinGW on Windows XP SP3), the compiler outputs the following error:
J:\comp proj\FS>J:\Programmi\MinGW\bin\g++.exe "J:\comp proj\FS\test.cpp" -o "J:\comp proj\FS\test.exe" -I "J:\comp proj\FS\BI"
E:\DOCUME~1\MrJackV\IMPOST~1\Temp\ccidH1Z6.o:test.cpp:(.text+0x2c): undefined reference to BigInteger::BigInteger(int)'
operator<<(std::ostream&, BigInteger const&)'
E:\DOCUME~1\MrJackV\IMPOST~1\Temp\ccidH1Z6.o:test.cpp:(.text+0x11b): undefined reference to
E:\DOCUME~1\MrJackV\IMPOST~1\Temp\ccidH1Z6.o:test.cpp:(.text$ZNK10BigIntegermlERKS[BigInteger::operator*(BigInteger const&) const]+0x29): undefined reference
to `BigInteger::multiply(BigInteger const&, BigInteger const&)'
collect2: ld returned 1 exit status
I've tried using the -I, -l and -L
switches to fix the problem but with no success.
Moreover I've tried in the cpp to put #include "BI/BigIntegerLibrary.hh"
but that didn't work.
Is there something I'm doing wrong?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要同时使用 -L 和 -l 开关。 -L 指向包含库二进制文件的目录,-l 命名该二进制文件,
例如-L/home/ed/libs -lmath
You need to use both -L and -l switches. -L to point to the directory containing the library binary, -l to name that binary
e.g. -L/home/ed/libs -lmath
有两个关键词需要记住。
Undeclared
表示编译器从未听说过它。未定义
表示编译器听说过它,但不知道到底如何使用它。对于这些错误,您需要告诉它与应该随标头一起提供的 BigInteger 库(*.lib 文件)链接,我很确定 gcc 是如何包含链接库的(Ed Heal 说 -L 和 -l,我会这样做)。There's two keywords to keep in mind.
Undeclared
means the compiler has never heard of it.Undefined
means the compiler has heard of it, but doesn't know exactly how to use it. For those errors you need to tell it to link with the BigInteger library that should have come with the headers (A *.lib file) I'm hot sure exactly how gcc includes link libraries (Ed Heal says -L and -l, I'd do that).您的问题是您没有链接到库代码。
阅读“README”,然后按照其建议修改随附的 Makefile。
Your problem is that you didn't link with the library code.
Read the "README", then follow its advice to adapt the enclosed Makefile.
好吧,经过一番混乱之后,我发现我需要做几件事,
哇,现在看起来很容易!
Ok, so after some messing around I figured out that I needed to do a couple of things
wow, now it seems quite easy!