C++ eclipse-cdt 如何链接静态库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11636755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:23:09  来源:igfitidea点击:

eclipse-cdt how to link with static library

c++eclipse-cdt

提问by Avihai Marchiano

Fist i am confused should i set libraries under Linker/Libraries or to set them under "path and symbol". i think that they are the same. am i correct?

拳头我很困惑,我应该在链接器/库下设置库还是将它们设置在“路径和符号”下。我认为他们是一样的。我对么?

any way for my question - i need to link static library , so i tried few things:

以任何方式解决我的问题 - 我需要链接静态库,所以我尝试了几件事:

First tryI try to add gtest (this is the library) and gtest path in the link library. in this way its compiled and linked correct , but failed in runtime , because when it tried to load shared library (i want static library !) error is

首先尝试我尝试在链接库中添加gtest(这是库)和gtest路径。这样它的编译和链接正确,但在运行时失败,因为当它试图加载共享库(我想要静态库!)错误是

error while loading shared libraries: libgtest.so.0

加载共享库时出错:libgtest.so.0

Second try- I try do define it in path and symbol add to libraries the complete path '/root/workspace/gmock/gtest/lib/.lib/libgtest.a' . in this case i got error in the linkage :

第二次尝试- 我尝试在路径和符号中定义它,将完整路径 '/root/workspace/gmock/gtest/lib/.lib/libgtest.a' 添加到库中。在这种情况下,我在链接中出错:

g++ -L/root/workspace/gmock/lib/.libs -L/root/workspace/gmock/gtest/lib/.libs -o "playground"  ./src/p.o ./src/playground.o   -lpthread -l/root/workspace/gmock/gtest/lib/.lib/libgtest.a -l/root/workspace/gmock/lib/.lib/libgmock.a
/usr/bin/ld: cannot find -l/root/workspace/gmock/gtest/lib/.lib/libgtest.a

This postdosnt give an answer and not explain how you define differently linked against static vs shared.

这篇文章没有给出答案,也没有解释您如何定义针对静态与共享的不同链接。

Thank you

谢谢

回答by Avihai Marchiano

After lot of research and not related answer. found it!!! I found it in this useful link for eclipse-cdt

经过大量研究并没有相关答案。找到了!!!我在eclipse-cdt 的这个有用链接中找到了它

Apparently the library need to exist in the workspace.

显然该库需要存在于工作区中。

回答by user1166007

Add library path/file to: Properties - C/C++ Build - Settings - GCC C++ Linker - Miscellaneous - Other objects

将库路径/文件添加到:属性 - C/C++ 构建 - 设置 - GCC C++ 链接器 - 杂项 - 其他对象

回答by Raging Software

I built the Botan cryptography library as a static library, for reasons I don't want to go into here, and when I followed all the steps on the interwebs, add the library paths and specify the library name without "lib" and ".a", my project built just fine, however, when I ran it, I kept getting "The program terminated unexpectedly" (or similar) errors. Finally I figured out how to fix it. In Menu Bar, go to Project > Properties > C/C++ Build > Settings > Cross G++ Linker and add -staticbetween ${COMMAND}and ${FLAGS}, so the whole line looks like:

我将 Botan 加密库构建为静态库,出于不想进入这里的原因,当我按照 Internet 上的所有步骤进行操作时,添加库路径并指定库名称,但不包含“lib”和“.lib”。 a”,我的项目构建得很好,但是,当我运行它时,我不断收到“程序意外终止”(或类似)错误。最后我想出了如何解决它。在菜单栏中,转到 Project > Properties > C/C++ Build > Settings > Cross G++ Linker 并-static${COMMAND}and之间添加${FLAGS},因此整行如下所示:

${COMMAND} -static ${FLAGS} ${OUTPUT_FLAG} ${OUTPUT_PREFIX}${OUTPUT} ${INPUTS}

Yours may vary. I'm using Eclipse Luna Service Release 2 (4.4.2), with mingw-w64 mingw-builds project w/gcc 4.9.2 on Windows 8.1.

你的可能会有所不同。我正在使用 Eclipse Luna Service Release 2 (4.4.2),在 Windows 8.1 上使用 mingw-w64 mingw-builds 项目 w/gcc 4.9.2。