在 CMake 项目中使用 Taglib 而不安装它

问题描述

我正在制作一个 CMake 项目,目标是通过静态链接或直接使用第三方库(即 Taglib)的源代码,在 Windows 或 Unix(但不是 macOS)上生成可执行文件。我正在考虑做一些像那样简单的事情。

    git clone --recurse-submodules https://github.com/MyGitaccount/MyProject.git
    cd MyProject
    cmake .
    make

以下是有关我的 CMake 项目的一些详细信息:

我的项目存储库有一个引用作为 Taglib 存储库的子模块。 考虑认安装 Git、CMake 和编译器工具链(build-essential 或 MinGW)。

项目树结构

    ProjectName/
    |-- Preload.cmake
    |-- CMakeLists.txt
    |-- src/
        |-- CMakeLists.txt
        |-- main.cpp
    |-- taglib/
        |-- CMakeLists.txt
        |-- taglib/ (This folder is a submodule to my repository. It repository can be found here: 
                     https://github.com/taglib/taglib)
            |-- CMakeLists.txt
            |-- ConfigureChecks.cmake
            |-- cmake_uninstall.cmake.in
            |-- config.h.cmake
            |-- taglib-config.cmake
            |-- taglib-config.cmd.cmake
            |-- taglib.pc.cmake
            |-- cmake/
                |-- modules/
                    |-- FindCppUnit.cmake/
                    |-- MacroEnsureversion.cmake/

项目名称/Preload.cmake

    if(UNIX AND NOT APPLE)
        set (CMAKE_GENERATOR "Unix Makefiles" CACHE INTERNAL "" FORCE)
    endif()
    if(WIN32)
        set (CMAKE_GENERATOR "MinGW Makefiles" CACHE INTERNAL "" FORCE)
    endif()

项目名称/CMakeLists.txt

    cmake_minimum_required(VERSION 3.10)
    project(ProjectName)
    set(CMAKE_CXX_STANDARD 17)
    set(CMAKE_CXX_STANDARD_required True)
    add_subdirectory(taglib)
    # add_subdirectory(src)   # In comment for debugging purpose

ProjectName/taglib/CMakeLists.txt

    set(taglib_DIR ./taglib)
    find_package(taglib CONfig) 
    if (Taglib_FOUND)
      message("Found!")
    else()
      message("Not Found...")
    endif()

项目名称/src/CMakeLists.txt

    file(GLOB sources "*.c" "*.cpp")
    file(GLOB headers "*.h" "*.hpp")
    add_executable(${CMAKE_PROJECT_NAME} ${sources} ${headers})
    target_include_directories(${CMAKE_PROJECT_NAME} ${Taglib_INCLUDE_Dirs})
    target_link_libraries(${CMAKE_PROJECT_NAME} ${Taglib_LIBRARIES})

然后,当运行“cmake”时。在“MyProject/”中,我收到此错误(在 Windows 10 上)。

    C:\Users\UserName\Desktop\MyProject>cmake .
    -- The C compiler identification is GNU 9.2.0
    -- The CXX compiler identification is GNU 9.2.0
    -- Detecting C compiler ABI info
    -- Detecting C compiler ABI info - done
    -- Check for working C compiler: C:/MinGW/bin/gcc.exe - skipped
    -- Detecting C compile features
    -- Detecting C compile features - done
    -- Detecting CXX compiler ABI info
    -- Detecting CXX compiler ABI info - done
    -- Check for working CXX compiler: C:/MinGW/bin/g++.exe - skipped
    -- Detecting CXX compile features
    -- Detecting CXX compile features - done
    CMake Error at taglib/taglib/taglib-config.cmake:4:
      Parse error.  Expected a command name,got unquoted argument with text "{".
    Call Stack (most recent call first):
      taglib/CMakeLists.txt:5 (find_package)


    CMake Error at taglib/CMakeLists.txt:5 (find_package):
      find_package Error reading CMake code from
      "C:/Users/UserName/Desktop/MyProject/taglib/./taglib/taglib-config.cmake".


    -- Configuring incomplete,errors occurred!
    See also "C:/Users/UserName/Desktop/MyProject/CMakeFiles/CMakeOutput.log".

我还尝试使用我在谷歌搜索时找到的“FindTaglib.cmake”文件之一。 我先后将这些文件放在“MyProject/cmake/modules/”中并设置了变量“CMAKE_MODULE_PATH”,但没有成功。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)