链接到CMake目标会产生什么影响? [英] What can linking to a CMake target impact?

查看:108
本文介绍了链接到CMake目标会产生什么影响?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天遇到了一个有趣的问题.我正在尝试将测试可执行文件编译并链接到Boost单元测试框架,并以两种不同的方式对其进行了尝试.

I ran into an interesting problem today. I am trying to compile and link a test executable to the Boost unit test framework and I tried it in two different ways.

  1. 使用-lboost_unit_test_framework
  2. 直接链接到"boost_unit_test_framework"库的经典方法
  3. 链接到Boost::unit_test_framework CMake目标的现代CMake方法.
  1. The classic approach of linking directly to the "boost_unit_test_framework" library using -lboost_unit_test_framework
  2. The modern CMake approach of linking to the Boost::unit_test_framework CMake target.

有趣的是,当我直接链接到库时,我的代码可以编译并很好地链接;但是,当我链接到CMake目标时,我的代码甚至无法进入链接阶段就无法编译!

Interestingly when I link to the library directly my code compiles and links fine; however when I link to the CMake target my code fails to compile before it even gets to the linking stage!

我得到的错误与一个头文件有关,它似乎似乎再也找不到了.这表明链接到Boost::unit_test_framework的方式与我的包含路径有些混乱.

The errors I get are related to a header file that it suddenly can't seem to find anymore. This suggests that linking to the Boost::unit_test_framework somehow messed with my include path.

我知道链接到CMake目标应该是更现代,更可取的方法,但是如果它可以具有意外和无法解释的副作用,那似乎比直接链接到库更糟……

I know linking to a CMake target is supposed to be the more modern and preferred approach, but if it can have such unexpected and unexplainable side effects, it seems worse than just linking straight to the library...

为什么链接CMake目标会导致找不到头文件?还有什么其他种类的东西可以链接到CMake目标,而不是直接链接到库影响?

Why would linking the CMake target cause header files to not be found anymore? Also what other kinds of things can linking to a CMake target instead of linking directly to a library impact?

在两种情况下,我都使用target_link_libraries链接到boost库.例如

In both scenarios I am using target_link_libraries to link to the boost library. For example

target_link_libraries(mytest_exe
    testlib
    -lboost_unit_test_framework
)

target_link_libraries(mytest_exe
    testlib
    Boost::unit_test_framework
)

推荐答案

在链接之前失败的事实意味着CMake中的target_link_libraries命令实际上不仅会影响链接.它也会影响编译.

The fact that it is failing before linking means that the target_link_libraries command in CMake actually effects more than just linking. It is effecting the compilation as well.

是的,的确,当您链接到库 target 而不是库 file 时,会添加新的包含目录.这就是为什么将该方法称为现代"的原因-单个target_link_libraries调用即可完成使用该库所需的所有工作(在您的情况下为Boost).

Yes, it is true that new include directories are added when you link with a library target instead of the library file. This is why the approach is called "modern" - a single target_link_libraries call does all things which are needed to use the library (Boost in your case).

使用现代"方法失败的原因可能是"true" Boost标头与您使用的其他标头冲突.您可以通过检查错误消息中的包含文件链来检测到该错误.

Reason of failing with "modern" approach could be that "true" Boost headers conflict with other headers you use. You may detect that via inspecting chain of include files in the error message.

这篇关于链接到CMake目标会产生什么影响?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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