Linux 我如何告诉 cmake 我希望我的项目静态链接库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3755482/
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-04 23:13:41  来源:igfitidea点击:

How do I tell cmake I want my project to link libraries statically?

linuxopencvcmakestatic-librariesstatic-linking

提问by agnul

I'm trying to build an OpenCV-based project using CMake, running on Linux. So far my CMakeLists.txtfiles looks something like

我正在尝试使用 CMake 构建一个基于 OpenCV 的项目,该项目在 Linux 上运行。到目前为止,我的CMakeLists.txt文件看起来像

FIND_PACKAGE (OpenCV REQUIRED)
...
TARGET_LINK_LIBRARIES (my-executable ${OpenCV_LIBS})

but this results in dynamically linked libraries. How do I link with static libraries?

但这会导致动态链接库。如何链接静态库?

采纳答案by agnul

You build static OpenCV libraries by just setting the BUILD_SHARED_LIBSflag to false in CMake. Then all you need to do to build your own application with those static libraries is to add a dependency on OpenCV in your CMakeLists.txt:

您只需BUILD_SHARED_LIBS在 CMake中将标志设置为 false 即可构建静态 OpenCV 库。然后,使用这些静态库构建自己的应用程序所需要做的就是在以下文件中添加对 OpenCV 的依赖CMakeLists.txt

FIND_PACKAGE (OpenCV REQUIRED)
...
TARGET_LINK_LIBRARIES (your-application ${OpenCV_LIBS})

and CMake will take care of everything.

而 CMake 会处理一切。

回答by jkerian

To link everything statically, I believe you're looking for CMAKE_EXE_LINKER_FLAGS(add -static).

要静态链接所有内容,我相信您正在寻找CMAKE_EXE_LINKER_FLAGS(add -static)。

Are you using the 'simple method' of OpenCVConfig.cmake? or the older FindOpenCV.cmake?

您是否使用 OpenCVConfig.cmake 的“简单方法”?还是较旧的 FindOpenCV.cmake?

回答by RobertJMaynard

on the add_library line specify static. See http://www.cmake.org/cmake/help/cmake-2-8-docs.html#command:add_library

在 add_library 行指定静态。见http://www.cmake.org/cmake/help/cmake-2-8-docs.html#command:add_library

Correction since you are looking to link against a static library I would look into the CMAKE_FIND_LIBRARY_SUFFIXESproperty

更正,因为您希望链接到静态库,我会查看 CMAKE_FIND_LIBRARY_SUFFIXES属性

回答by pszilard

AFAIK that's a bit tricky, because CMake, more precisely the find_library command, prefers shared libs and finds those if both shared and static are available.

AFAIK 这有点棘手,因为 CMake,更准确地说是 find_library 命令,更喜欢共享库,并在共享和静态都可用的情况下找到那些库。

I'm still looking for a good solution myself to be able to compile binaries "as static as possible", but I've found no elegant solution yet. The only way it would surely work is to implement everything through custom FindXXXX modules.

我自己仍在寻找一个好的解决方案,以便能够“尽可能静态地”编译二进制文件,但我还没有找到优雅的解决方案。它肯定会起作用的唯一方法是通过自定义 FindXXXX 模块实现所有内容。

回答by pszilard

Note that gcc refuses to linkif you pass the -static option, but you have dynamic libs in the link arguments - which you will if you just simply use FindOpenCV.cmake and this picks up the dynamic libs (I don't know how OpenCVConfig.cmake behaves though)...

请注意,如果您传递 -static 选项,gcc 将拒绝链接,但是您在链接参数中有动态库 - 如果您只是简单地使用 FindOpenCV.cmake 并且这会获取动态库(我不知道 OpenCVConfig 如何.cmake 的行为虽然)...

回答by bcook

Actually this issue seems to have already been fixed in the OpenCVConfig.cmakethat comes with OpenCV. All you have to do is define OpenCV_STATICin your CMakeLists.txt. I.e.

实际上这个问题似乎已经在OpenCVConfig.cmakeOpenCV 自带的中解决了。您所要做的就是OpenCV_STATIC在您的CMakeLists.txt. IE

set(OpenCV_STATIC ON)
find_package(OpenCV REQUIRED)

回答by Li Gewei

SET (CMAKE_EXE_LINKER_FLAGS "-static")