简体   繁体   English

包括共享库(.so)和CMake

[英]Including Shared Libraries (.so) with CMake

I've been trying to include different types of libraries with CMake. 我一直试图用CMake包含不同类型的库。

  • .a 。一种
  • .dylib 名为.dylib
  • .so 。所以

I finally, got both the .a and .dylib to work with this code. 我终于得到了.a.dylib来处理这段代码。

find_library(libname NAMES  libcef.dylib PATHS ${libname_PATH})

along with this, underneath where I add_executable to initialize all my files for the build. 与此一起,在我的add_executable下面初始化我的所有文件的构建。

target_link_libraries(${PROJECT_NAME} ${libname})

However, I tried using the same code on a .so file and it doesn't seem to work. 但是,我尝试在.so文件上使用相同的代码,但它似乎不起作用。

I get this statement from cmake when I try building. 当我尝试构建时,我从cmake得到这个声明。

Target "project name" links to item

-- path of file -- 

which is a full-path but not a valid library file name.

I'm not sure if this is the correct way to handle .so files or perhaps I'm not even fully understanding what an .so file is. 我不确定这是否是处理.so文件的正确方法,或者我甚至不完全理解.so文件是什么。 Any input and/or clarification would be much appreciated. 任何意见和/或澄清将非常感激。

edit: 编辑:

THEORY- my theory is because it doesn't have a lib in front of the name of the library name its called ffmpegsumo.so. 理论 - 我的理论是因为它在库名称前面没有lib,它叫做ffmpegsumo.so。 However, when i try renaming it the file name still saves into the variable name very strange. 但是,当我尝试重命名它时,文件名仍然保存到变量名称中非常奇怪。

The same should work with .so files also, just make sure the required .so file is present at ${libname_PATH} which you have given. 同样适用于.so文件,只需确保您提供的${libname_PATH}存在所需的.so文件。

find_library treats all types (.a / .so/ .dylib/ .dll) the same way. find_library以相同的方式处理所有类型(.a / .so / .dylib / .dll)。 Problem may be the following 问题可能如下

-- path not set up correctly - 路径设置不正确
-- error because of absolute path - 因绝对路径而出错
-- .so not present - 。不存在
-- If the error is from build (not from configure only) the .so might be corrupt, try replacing it - 如果错误来自构建(而不是仅来自配置).so可能已损坏,请尝试替换它

--Your library does not seem to be valid - 你的图书馆似乎没有效果

Shared libraries are linked dynamically. 共享库是动态链接的。 That means that your OS will automatically look for and load the .so files when the time comes to run the application. 这意味着在运行应用程序时,您的操作系统将自动查找并加载.so文件。 You only need to tell cmake the name of the library and the OS will take care of the rest. 您只需要告诉cmake库的名称,操作系统将负责其余的工作。

For example, if you want to link to the dynamic library for libSDL.so, you just say: target_link_libraries(${PROJECT_NAME} SDL) 例如,如果要链接到libSDL.so的动态库,只需说: target_link_libraries(${PROJECT_NAME} SDL)

As a sanity check, your linker will look to make sure that the SDL library does exist on your computer. 作为完整性检查,链接器将确保您的计算机上确实存在SDL库。 That's why you might get a linking error if that library is not available at link-time, even if it's really a dynamic library. 这就是为什么如果该库在链接时不可用,可能会出现链接错误,即使它实际上是一个动态库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM