G++:尝试将 include 和 lib 添加到默认目录
我已经编译了 Google C++ 测试。我现在尝试将其添加到我的 g++ 安装(在 Cygwin 上)中,这样我就不必指定 -I
、-L
和 - l
对于每个项目。我所做的步骤是:
- 我将 /usr/local/include 中的符号链接放入 GTEST_ROOT/include/gtest
- 我将 /usr/local/lib 中的符号链接放入 GTEST_ROOT/lib (libgtest.la libgtest_main.la)
- 我写的用于测试设置的证明程序
测试程序使用 g++ -cproof.cpp
可以正常编译,但 g++proof.cpp
在编译过程中给出错误链接,说找不到 gtest 引用。
使用 grep 搜索我的 GTEST_ROOT 所需的引用,我在 GTEST_ROOT/lib/.libs 中的其他库中找到了它们(libgtest.a libgtest.la libgtest.lai libgtest_main.a libgtest_main.la libgtest_main.lai)
由于时间限制,我使用以下命令让它工作 g++ -LGTEST_ROOT/lib/.libs -lgtestproof.cpp
和我已将其放入 makefile 中。
但是,我仍在尝试进行设置,以便 g++proof.cpp
能够正常工作。我缺少什么?
感谢 Matt,我现在明白我不能删除 -lgtest
。我想至少摆脱 -LGTEST_ROOT/lib/.libs
。我已将符号链接添加到提供的附加 .a
和 .lai
文件。但是,g++proof.cpp -lgtest
现在给我链接器错误:cannot find -lgtest
注意:符号链接 GTEST_ROOT/lib/.libs 中的库仍然不会给出期望的结果。
I've compiled Google C++ Test. I'm now trying to add it to my g++ installation (on Cygwin) such that I don't have to specify -I
, -L
, and -l
for every project. The steps I did were:
- I put a symlink in /usr/local/include to GTEST_ROOT/include/gtest
- I put symlinks in /usr/local/lib to the libs in GTEST_ROOT/lib (libgtest.la libgtest_main.la)
- I wrote a proof program to test the setup
The test program compiles fine with g++ -c proof.cpp
but g++ proof.cpp
gives errors during the linking, saying that the gtest references can't be found.
Using grep to search my GTEST_ROOT for the required references, I found them in additional libs in GTEST_ROOT/lib/.libs (libgtest.a libgtest.la libgtest.lai libgtest_main.a libgtest_main.la libgtest_main.lai)
Due to time constraints, I got it to work using the following command g++ -LGTEST_ROOT/lib/.libs -lgtest proof.cpp
and I've put this in a makefile.
However, I'm still trying to get my setup such that g++ proof.cpp
would just work. What am I missing?
Thanks to Matt, I understand now that I cannot drop the -lgtest
. I would like to at least get rid of the -LGTEST_ROOT/lib/.libs
. I've added symlinks to the additional .a
and .lai
files provided. However, g++ proof.cpp -lgtest
now gives me the linker error: cannot find -lgtest
NOTE: symlinking the libs in GTEST_ROOT/lib/.libs still does not give the desired results.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您始终需要将
-lgtest
传递到最终的编译/链接步骤1。 GCC 不会在其搜索路径中的所有库中搜索它丢失的符号(这会太昂贵)。库文件本身(
.so
或.a
)需要位于/usr/local/lib/
目录中,或者是指向的符号链接那些文件。在那里对目录进行符号链接将不起作用。1嗯,这在技术上是不正确的。您可以修改您的规范文件,但不要这样做。
You'll always need to pass
-lgtest
to your final compile/link step1. GCC will not search all the libraries in its search path for the symbols that it is missing (that would be way too expensive).The library files themselves (
.so
or.a
) need to be in the/usr/local/lib/
directory, or a symlink to those files. Symlinking a directory there will not work.1well, that's not technically true. You could modify your spec file, but don't do that.
如果你想反对它,你总是需要指定这个参数 - 这就是 Makefile 的用途 - 所以你不需要每次编译都输入那些长行。如果您愿意,您还可以使用 LDFLAGS 环境变量来为您存储这些变量,但随后您会将它链接到您编译的每个可执行文件 - 这绝对不是一个好主意。
You always need to specifiy this parameter if you want to lik against it - that's what Makefile is for - so you don't need to type those long lines every compilation. If you want you can also use LDFLAGS enviroment variable to store those for you, but then you will get it linked to every executable that you compile - this is definitely not a good idea.