Cmake在Windows上不会添加共享库路径(在Linux上工作) [英] Cmake on Windows doesn't add shared library paths (works on linux)

查看:341
本文介绍了Cmake在Windows上不会添加共享库路径(在Linux上工作)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在linux上使用CMake和Eclipse一段时间,并且一直在处理包含许多可执行文件和共享对象的多个目标项目。



源代码构建,在Linux上,二进制文件放在自己的目录中。当我在linux上这样做的时候,eclipse是能够找到共享对象和运行可执行文件很好,但在Windows上这不会发生。



在Windows上我有添加一个PATH环境变量指向dll或者我可以构建一个单一的bin和lib目录(虽然我有一个问题,我已经忘记了过去与CMake,让我想避免这种情况)。



为什么这在Windows上以不同的方式工作到linux?这是我缺少的设置还是这只是不起作用?



这些构建本身工作正常。我使用MinGW,Eclipse Kepler和Windows 7 64位。



提前感谢。

方案

Windows根本没有一些必要的概念,允许CMake设置你的构建环境。当链接Windows时,将在与二进制文件相同的目录中,然后搜索PATH中的目录。没有什么像RPATH,它在大多数Unix平台上使用,以注入其他更适当的路径。这些DLL通常应该与您的二进制文件一起安装在同一目录中。



在我看来,Windows上的最佳做法是将DLL放在二进制文件旁边。

 安装(TARGETS MyTarget 
EXPORTMyProjectTargets
RUNTIME DESTINATION $ {INSTALL_RUNTIME_DIR}
LIBRARY DESTINATION$ {INSTALL_LIBRARY_DIR}
ARCHIVE DESTINATION$ {INSTALL_ARCHIVE_DIR})

会将DLL安装到RUNTIME目标,但将libs放在LIBRARY目标中。这意味着通常在类UNIX操作系统lib上有共享对象,但CMake知道DLL是有效的运行时,并将进入bin。希望这使事情更清楚。这是不可能的CMake / Eclipse真的改善这么多,除了可能注入额外的目录到您的PATH中,当点击从Eclipse运行(不确定是否可能)。



如果你关心构建树,那么下面的代码会很好地工作(如下面的注释所示):

  set (CMAKE_RUNTIME_OUTPUT_DIRECTORY$ {CMAKE_BINARY_DIR} / bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY$ {CMAKE_BINARY_DIR} / lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY$ {CMAKE_BINARY_DIR} / lib)

如果你想允许这些被覆盖(可能是有用的),他们应该用if(NOT var_name) / p>

I've been using CMake and Eclipse on linux for a while and have been working with multiple target projects containing numerous executables and shared objects.

I use out of source builds and on linux the binaries are put into their own directories. When I do this on linux eclipse is somehow able to find the shared objects and run the executables fine but on windows this doesn't happen.

On Windows I am having to add a PATH environment variable that points to the dlls or I could build into a single bin and lib directory (although I had an issue I've forgotten in the past with CMake that made me want to avoid this).

Why is this working differently on Windows to linux? Is it a setting I'm missing or does this just not work?

The builds themselves are working perfectly. I'm using MinGW, Eclipse Kepler and Windows 7 64 bit.

Thanks in advance.

解决方案

Windows simply doesn't have some of the necessary concepts to allow CMake to set up your build environment. When linking Windows will look in the same directory as the binary, and then search the directories in your PATH. There isn't anything like RPATH, which is used on most Unix platforms, to inject in other more appropriate paths. The DLLs should generally be installed alongside your binaries, in the same directory.

In my opinion, best practice on Windows is to put the DLLs next to your binaries. CMake attempts to make this easier,

install(TARGETS MyTarget
  EXPORT "MyProjectTargets"
  RUNTIME DESTINATION "${INSTALL_RUNTIME_DIR}"
  LIBRARY DESTINATION "${INSTALL_LIBRARY_DIR}"
  ARCHIVE DESTINATION "${INSTALL_ARCHIVE_DIR}")

would install DLLs to the RUNTIME destination, but put the libs in the LIBRARY destination. This means that typically on Unix-like OSes lib has the shared objects, but CMake knows that DLLs are effectively runtime and would go in bin. Hopefully this makes things clearer. It is impossible for CMake/Eclipse to really improve this much, beyond perhaps injecting additional directories into your PATH when clicking run from Eclipse (not sure if that is possible).

If you are concerned with the build tree, then the following would work well there (as suggested in the comments below):

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")

If you want to allow these to be overridden (can be useful) they should be protected with if(NOT var_name) blocks too.

这篇关于Cmake在Windows上不会添加共享库路径(在Linux上工作)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆