Cmake多库场景

发布于 2024-12-08 10:57:37 字数 1077 浏览 0 评论 0原文

我们希望组织一个像这样的 C++ 项目:

project/
    lib1/       (first library)
        CMakeList.txt
        src/
            lib1.c
            foo1.h
        build/
        test/       (tests)
            CMakeList.txt
            test1.c
            test2.c
    lib2/       (second library)
        CMakeList.txt
        src/
            CMakeList.txt
            os/              (OS dependent code)
                CMakeList.txt
                win32/
                    xxx.c    (win32 implementation)
                linux/
                    xxx.c    (linux implementation)
            lib2.c
            foo2.h
        build/
    include/    (shared/public headers)
        lib1/
            lib.h    (shared library header included from apps)
        lib2/
            lib.h    (shared library header -"-)

请问,当 lib2 应该使用 link1 时,如何编写这些 CMakeLists.txt 以及什么时候例如 lib2 应该是可移植的(至少 Win32、Linux...)?

更正:如果某些 CMakeList.txt 文件不在其位置,请假设如此。我可能忘记了。

We would like to organize a C++ project like this:

project/
    lib1/       (first library)
        CMakeList.txt
        src/
            lib1.c
            foo1.h
        build/
        test/       (tests)
            CMakeList.txt
            test1.c
            test2.c
    lib2/       (second library)
        CMakeList.txt
        src/
            CMakeList.txt
            os/              (OS dependent code)
                CMakeList.txt
                win32/
                    xxx.c    (win32 implementation)
                linux/
                    xxx.c    (linux implementation)
            lib2.c
            foo2.h
        build/
    include/    (shared/public headers)
        lib1/
            lib.h    (shared library header included from apps)
        lib2/
            lib.h    (shared library header -"-)

Please, how to write those CMakeLists.txt when even lib2 should use link1 and when e.g. lib2 should be portable (at least Win32, Linux...)?

Correction: If some CMakeList.txt files are not on their places, please assume so. I probably forgot.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

说谎友 2024-12-15 10:57:37

整个理念是从整个项目的中央 CMakeLists.txt 开始。在这个级别,所有目标(库、可执行文件)都将被聚合,因此从 lib1 链接到 lib2 不会有问题。如果 lib2 要链接到 lib1,则需要首先构建 lib1。

平台特定的源文件应有条件地设置为某个变量。
(如果您需要在子目录中设置变量并在上面的目录中使用它,则必须使用 CACHE FORCE 等将其设置到缓存中 - 请参阅 set 手册)

这就是您的做法正确的源代码构建 - 正如 CMake 的意图:

cd project-build
cmake ../project

每个库都有单独的构建目录并不是很 CMake 风格(如果我可以这么说),并且可能需要
一些技巧。

project-build/
project/
    CMakeLists.txt (whole project CMakeLists.txt)
    [
        project(MyAwesomeProject)

        include_directories(include) # allow lib1 and lib2 to include lib1/lib.h and lib2/lib.h
        add_subdirectory(lib1) # this adds target lib1
        add_subdirectory(lib2) # this adds target lib2

    ]

    lib1/       (first library)
        CMakeList.txt
        [
            add_library(lib1...)
            add_subdirectory(test)
        ]
        src/
            lib1.c
            foo1.h
        test/       (tests)
            CMakeList.txt
            test1.c
            test2.c
    lib2/       (second library)
        CMakeList.txt
        [
            add_subdirectory(src)
        ]
        src/
            CMakeList.txt
            [
                if(WIN32)
                    set(lib2_os_sources os/win32/xxx.c)
                elsif(LINUX)
                    set(lib2_os_sources os/linux/xxx.c)
                else()
                    message(FATAL_ERROR "Unsupported OS")
                endif()
                add_library(lib2 SHARED lib2.c ${lib2_os_sources})
            ]
            os/              (OS dependent code)
                win32/
                    xxx.c    (win32 implementation)
                linux/
                    xxx.c    (linux implementation)
            lib2.c
            foo2.h
    include/    (shared/public headers)
        lib1/
            lib.h    (shared library header included from apps)
        lib2/
            lib.h    (shared library header -"-)

The whole philosophy is to start with a central CMakeLists.txt for your whole project. At this level all the targets (libs, executables) are gonna be aggregated so there will be no problem linking from lib1 to lib2 for example. If lib2 is gonna be linking to lib1, lib1 needs to be built first.

Platform specific source files should be set conditionally to some variable.
(If you need to set variable in a subdirectory and use it in a directory above, you have to set it to the cache, using CACHE FORCE etc. - see manual for set)

This is how you do proper out of source build - as CMake intends:

cd project-build
cmake ../project

Having separate build directories per library is not very CMake'ish (if I may say so) and would probably require
some hacks.

project-build/
project/
    CMakeLists.txt (whole project CMakeLists.txt)
    [
        project(MyAwesomeProject)

        include_directories(include) # allow lib1 and lib2 to include lib1/lib.h and lib2/lib.h
        add_subdirectory(lib1) # this adds target lib1
        add_subdirectory(lib2) # this adds target lib2

    ]

    lib1/       (first library)
        CMakeList.txt
        [
            add_library(lib1...)
            add_subdirectory(test)
        ]
        src/
            lib1.c
            foo1.h
        test/       (tests)
            CMakeList.txt
            test1.c
            test2.c
    lib2/       (second library)
        CMakeList.txt
        [
            add_subdirectory(src)
        ]
        src/
            CMakeList.txt
            [
                if(WIN32)
                    set(lib2_os_sources os/win32/xxx.c)
                elsif(LINUX)
                    set(lib2_os_sources os/linux/xxx.c)
                else()
                    message(FATAL_ERROR "Unsupported OS")
                endif()
                add_library(lib2 SHARED lib2.c ${lib2_os_sources})
            ]
            os/              (OS dependent code)
                win32/
                    xxx.c    (win32 implementation)
                linux/
                    xxx.c    (linux implementation)
            lib2.c
            foo2.h
    include/    (shared/public headers)
        lib1/
            lib.h    (shared library header included from apps)
        lib2/
            lib.h    (shared library header -"-)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文