CMAKE:链接到项目目录之外的本地构建的静态库

发布于 2025-02-05 15:23:51 字数 1694 浏览 2 评论 0原文

我正在将几个项目转换为使用Cmake作为构建系统。这些项目之间有一些共享的库,我想与项目分开维护,这些项目统计地构建了应用程序和库。

所需的目录结构看起来像这样。

repos/
   common/
      LibA/
         CMakeLists.txt
       src/
          CMakeLists.txt
          <Lib sources>
       build/
          LibA.lib
     <other libs>
   AppA/
      CMakeLists.txt
      src/
          CMakeLists.txt
          <App sources>
      build/
         AppA.exe
   <other apps>

到目前为止,我制作的CMAKE文件是;

liba/cmakelists.txt

cmake_minimum_required (VERSION 3.8)

project ("LibA")

# Include sub-projects.
add_subdirectory ("src")

liba/src/cmakelists.txt

cmake_minimum_required (VERSION 3.8)

add_library(
    LibA STATIC
    "Animal.cpp" 
    "Animal.h" 
    "Dog.h" 
    "Dog.cpp"
)

set_property(TARGET LibA PROPERTY CXX_STANDARD 11)

cmake_minimum_required (VERSION 3.8)

project ("AppA")

# Include sub-projects.

add_subdirectory("../common/LibA" "build")

add_subdirectory ("src")

appa/cmakelists.txt appa/src/cmakelists.txt

cmake_minimum_required (VERSION 3.8)

add_executable( AppA
    main.cpp
)

target_link_libraries(AppA LibA)

set_property(TARGET AppA PROPERTY CXX_STANDARD 11)

使用此设置,APPA上的CMAKE成功,但是Main.cpp找不到Animal.H。

main.cpp

#include "Animal.h"

int main(int argc, char *argv[])
{


    Animal* ann = new Animal();
    ann->speak((char*)"Rawr");

    return 0;
}

我假设我在Appa的Cmake文件中没有正确链接到liba。不幸的是,我还找不到有关如何在项目统计上链接在项目子目录之外的任何好示例。

I'm in the process of converting several projects to use CMake as the build system. These projects have some shared libraries between them that I would like to maintain separately from the projects, with the projects statically building the application and libraries together.

The desired directory structure would look something like this;

repos/
   common/
      LibA/
         CMakeLists.txt
       src/
          CMakeLists.txt
          <Lib sources>
       build/
          LibA.lib
     <other libs>
   AppA/
      CMakeLists.txt
      src/
          CMakeLists.txt
          <App sources>
      build/
         AppA.exe
   <other apps>

The CMake files I've made thus far are;

LibA/CMakeLists.txt

cmake_minimum_required (VERSION 3.8)

project ("LibA")

# Include sub-projects.
add_subdirectory ("src")

LibA/src/CmakeLists.txt

cmake_minimum_required (VERSION 3.8)

add_library(
    LibA STATIC
    "Animal.cpp" 
    "Animal.h" 
    "Dog.h" 
    "Dog.cpp"
)

set_property(TARGET LibA PROPERTY CXX_STANDARD 11)

AppA/CMakeLists.txt

cmake_minimum_required (VERSION 3.8)

project ("AppA")

# Include sub-projects.

add_subdirectory("../common/LibA" "build")

add_subdirectory ("src")

AppA/src/CMakeLists.txt

cmake_minimum_required (VERSION 3.8)

add_executable( AppA
    main.cpp
)

target_link_libraries(AppA LibA)

set_property(TARGET AppA PROPERTY CXX_STANDARD 11)

With this setup, running cmake on AppA is successful, however, main.cpp cannot find Animal.h.

main.cpp

#include "Animal.h"

int main(int argc, char *argv[])
{


    Animal* ann = new Animal();
    ann->speak((char*)"Rawr");

    return 0;
}

I assume I'm not linking to LibA properly in AppA's CMake files. Unfortunately, I haven't been able to find any good examples of how to link outside of a project's subdirectories statically.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文