CMake:包含目录的排序(如何混合基于系统和基于用户的包含路径?) [英] CMake: ordering of include directories (How to mix system- and user-based include paths?)

查看:143
本文介绍了CMake:包含目录的排序(如何混合基于系统和基于用户的包含路径?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个CMake项目,该项目包含并链接到两个库,例如AB(实际上是两个以上,其中一个是boost的东西,但这在这里并不重要).两者都通过FindSomething.cmake脚本定位,该脚本正确地填充了标准CMake变量,从而通过以下方式添加了包含目录

I've got a CMake project that includes and links against two libraries, say A and B (actually it's more than two and one of them is boost stuff, but that doesn't really matter here). Both are located via FindSomething.cmake scripts that (correctly) populate the standard CMake variables such that include directories are added via

INCLUDE_DIRECTORIES(${A_INCLUDE_DIRS})
INCLUDE_DIRECTORIES(${B_INCLUDE_DIRS})

并稍后通过

TARGET_LINK_LIBRARIES(mytarget ${A_LIBRARIES} ${B_LIBRARIES})

现在,问题在于这两个库都可以驻留在基于用户的位置或系统目录中(顺便说一下,我在Linux上,CMake 2.8.2)或同时存在于这两个目录中.假设A仅在$HOME/usr/include$HOME/usr/lib中,而B(在我的情况下为Boost)既位于系统路径(/usr/include/usr/lib)中,又位于基于用户的路径中-在不同版本中.可以使用查找脚本来查找系统库或基于用户的库B,这是可行的.

Now, the problem is that both libraries can either reside in a user based location or in the system directories (I'm on linux by the way, CMake 2.8.2) - or in both. Let's say A is only in $HOME/usr/include and $HOME/usr/lib while B (boost in my case) resides in both the system paths (/usr/include and /usr/lib) AND in the user based paths - in different versions. The find scripts can be made to find either the system or the user-based library B, this works.

当我想从系统路径链接到B时,麻烦就开始了.${B_INCLUDE_DIRS}${B_LIBRARIES}正确指向头文件和库的系统范围位置.但是仍然有${A_INCLUDE_DIRS}指向非系统包含目录,最终库B的头文件也从该位置获取,而B的链接使用系统路径中的版本(通过)会导致冲突,即链接错误.

The trouble starts when I want to link against B from the system paths.${B_INCLUDE_DIRS} and ${B_LIBRARIES} correctly point to the system-wide locations of the headers and libraries. But there is still ${A_INCLUDE_DIRS} that points to a non-system include directory and ultimately also the headers for library B are taken from this location, while the linking for B uses the version from the system paths (via ${B_LIBRARIES}) which leads to conflicts, i.e. linking errors.

更改INCLUDE_DIRECTORIES语句的顺序似乎没有任何改变.我通过目标文件上的nm --line-numbers检查了引起链接错误的符号的来源.

Changing the order of the INCLUDE_DIRECTORIES statements does not seem to change anything. I checked the origin of the symbols that cause the linking errors via nm --line-numbers on the object files.

我该怎么办?

  • 强制包括目录的顺序(即使这也意味着优先于系统路径,尽管也指定了基于用户的位置)?
  • 告诉CMake对A中的所有标头使用${A_INCLUDE_DIRS},对B中的所有标头使用${B_INCLUDE_DIRS}吗?
  • force the ordering of the include directories (even if this would mean to give precedence to a system path although there is also a user-based location specified)?
  • tell CMake to use ${A_INCLUDE_DIRS} for all headers from A and ${B_INCLUDE_DIRS} for all headers from B?

推荐答案

这是CMake关于include_directories()的说法:

Here's what CMake says about include_directories():

include_directories([AFTER | BEFORE] [SYSTEM] dir1 [dir2. ..])

您可以指定在告知系统包含目录之前或之后要包含在系统中的搜索目录.

You can specify that you want to have include directories searched before or after the system include directories at the time that you tell it about those directories.

您可能还特定于目标:

target_include_directories(目标[系统] [之前] [ items1 ...] [[items2 ...] ...])

这篇关于CMake:包含目录的排序(如何混合基于系统和基于用户的包含路径?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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