CMake:具有多个项目的静态库 [英] CMake: Static library with multiple projects

查看:1149
本文介绍了CMake:具有多个项目的静态库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个项目使用CMake。项目A构建一个静态库,然后链接到项目B.问题是,当我从项目A更改源文件并构建这两个项目时,项目B将不会自动再次链接。如何将项目A(静态库)的输出添加到项目B?



EDIT:



ProjectA CMakeLists.txt:

  cmake_minimum_required(VERSION 2.6)
PROJECT(PROJECTA)

ADD_LIBRARY(projectA STATICsrc / foo.cpp)


b $ b

ProjectB CMakeLists.txt:

  cmake_minimum_required(VERSION 2.6)
PROJECT(PROJECTB)

set(PROJECTA_DIR $ {CMAKE_CURRENT_SOURCE_DIR} /../ ProjectA)
include_directories($ {PROJECTA_DIR} / include)

link_directories($ {PROJECTA_DIR} / build)

ADD_EXECUTABLE(projectBsrc / main.cpp)
target_link_libraries(projectB projectA)


b $ b

要重现问题:


  1. 构建这两个项目(需要首先构建ProjectA)。

  2. 更改ProjectA / src / foo.cpp

  3. 构建ProjectA(将更新libProject.a)

  4. Build ProjectB 不会将可执行文件与新的libProject.a链接,而是假设没有进行任何更改,并且什么都不做。)




通常情况下,当CMake项目必须合作时,它是有益的使用 add_subdirectory()使它们成为同一个构建系统的一部分。这样,CMake可以跟踪目标之间的依赖关系,自动解析对逻辑目标名称的引用等。



如果这不是一个选项,你应该至少删除 link_directories(),而是使用 target_link_libraries()中库的完整路径。使用 link_directories()通常不鼓励,已知会导致问题。


I have two projects using CMake. Project A build a static library that is then linked to project B. The problem is, when I change a source file from project A and build both projects, project B won't automatically be linked again. How can I add the output from project A (the static library) to project B?

EDIT: Here is an example of my problem:

ProjectA CMakeLists.txt:

cmake_minimum_required (VERSION 2.6)
PROJECT(PROJECTA)

ADD_LIBRARY(projectA STATIC "src/foo.cpp")

ProjectB CMakeLists.txt:

cmake_minimum_required (VERSION 2.6)
PROJECT(PROJECTB)

set(PROJECTA_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../ProjectA)
include_directories(${PROJECTA_DIR}/include)

link_directories(${PROJECTA_DIR}/build)

ADD_EXECUTABLE(projectB "src/main.cpp")
target_link_libraries(projectB projectA)

To reproduce the problem:

  1. Build both projects (ProjectA needs to be built first).
  2. Change ProjectA/src/foo.cpp
  3. Build ProjectA (it will update libProject.a)
  4. Build ProjectB (it won't link the executable with the new libProject.a, instead it will assume that no changes were made and do nothing).

解决方案

Turning my comments into an answer.

Normally, when CMake projects have to cooperate, it's beneficial to make them part of the same buildsystem using add_subdirectory(). That way, CMake can track dependency between their targets, automatically resolve references to logical target names etc.

If that is not an option for you, you should at least remove link_directories() and instead use the full path to the library in target_link_libraries(). Use of link_directories() is generally discouraged, it's known to cause issues.

这篇关于CMake:具有多个项目的静态库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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