避免使用CMake重新编译常见的目标文件? [英] Avoid recompilation of common object files with CMake?

查看:1517
本文介绍了避免使用CMake重新编译常见的目标文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近通过CMake向我的一个项目添加了测试。我这样做通过创建另一个可执行文件,将运行我的测试用例。我的项目中的测试用例使用我的主应用程序的代码。每次我对一个在主应用程序和测试运行器之间共享的源文件进行更改时,它会重新编译该对象两次。一次是为主应用程序,第二次为测试跑者。



有没有办法我可以共享两个相同的目标文件?



我的CMakeLists档案看起来像这样。

  AUX_SOURCE_DIRECTORY($ {SRC_DIR} / game game_SRC) 
AUX_SOURCE_DIRECTORY($ {SRC_DIR} / framework / framework_SRC)

ADD_EXECUTABLE($ {CMAKE_PROJECT_NAME}
$ {game_SRC} $ {framework_SRC})b
$ b #--- Testing ---
ENABLE_TESTING()
AUX_SOURCE_DIRECTORY($ {TEST_DIR} test_SRC)

ADD_EXECUTABLE($ {TEST_RUNNER_NAME}
$ {test_SRC}
$ {framework_SRC}


解决方案

是的,使你的框架成为一个单独的库。就像现在一样,您将framework_SRCS指定为项目可执行文件的源,然后还为test-runner可执行文件指定相同的源。而CMake只是从指定的来源构建两个可执行文件。



更糟糕的是,CMake不能轻易地假设相同的源文件将对两个可执行文件使用完全相同。



最简单的方法是将framework_SRCS链接到一个库中,然后指定一个链接依赖:如果你在测试和你的应用程序之间有几个不同的编译标志,

  ADD_LIBRARY(MyFramework STATIC $ {framework_SRCS})

ADD_EXECUTABLE($ {CMAKE_PROJECT_NAME}
$ {game_SRC})
TARGET_LINK_LIBRARIES($ {CMAKE_PROJECT_NAME} MyFramework)

#---测试---
ENABLE_TESTING()
AUX_SOURCE_DIRECTORY($ {TEST_DIR } test_SRC)

ADD_EXECUTABLE($ {TEST_RUNNER_NAME}
$ {test_SRC}

TARGET_LINK_LIBRARIES($ {TEST_RUNNER_NAME} MyFramework)

(请注意,我明确选择静态构建库,当然你可以选择保留cmake或强制执行共享构建) / p>

尊敬的,André


I've recently added testing through CMake to one of my projects. I've done this by creating another executable that will run my test cases. The tests cases in my project use the code from my main application. Every time I make a change to a source file that is shared between both the main application and the test runner, it recompiles that object twice. Once for the main application and a second time for the test runner.

Is there a way I can share the same object files for both?

My CMakeLists file looks something like this.

AUX_SOURCE_DIRECTORY(${SRC_DIR}/game game_SRC)
AUX_SOURCE_DIRECTORY(${SRC_DIR}/framework/ framework_SRC) 

ADD_EXECUTABLE(${CMAKE_PROJECT_NAME} 
               ${game_SRC} ${framework_SRC})

# --- Testing ---
ENABLE_TESTING()
AUX_SOURCE_DIRECTORY(${TEST_DIR} test_SRC)

ADD_EXECUTABLE(${TEST_RUNNER_NAME}
               ${test_SRC}
               ${framework_SRC} 
)

解决方案

Yes, by making your framework a separate library. As it is now, you specify the framework_SRCS as sources for your project executable and then you also specify the same sources for the test-runner executable. And CMake simply builds both executables from the specified sources.

Even worse, CMake can not easily assume that the same source file will be used exactly the same for both executables. What if you have a few different compilation flags between your test and your app?

The easiest approach is to link you framework_SRCS into a library and then specify a link-dependency:

ADD_LIBRARY( MyFramework STATIC ${framework_SRCS} )

ADD_EXECUTABLE(${CMAKE_PROJECT_NAME} 
               ${game_SRC})
TARGET_LINK_LIBRARIES( ${CMAKE_PROJECT_NAME} MyFramework )

# --- Testing ---
ENABLE_TESTING()
AUX_SOURCE_DIRECTORY(${TEST_DIR} test_SRC)

ADD_EXECUTABLE(${TEST_RUNNER_NAME}
               ${test_SRC}

TARGET_LINK_LIBRARIES( ${TEST_RUNNER_NAME} MyFramework )

(Note that I explicitly chose to build the library statically. Of course you could opt to leave it up to cmake or enforce a shared build)

Regards, André

这篇关于避免使用CMake重新编译常见的目标文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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