【问题标题】:Using CMake to index source files of an external library with Eclipse使用 CMake 通过 Eclipse 索引外部库的源文件
【发布时间】:2012-03-23 19:02:14
【问题描述】:

我正在使用 CMake 通过使用“Eclipse CDT4 - Unix Makefiles”来构建带有外部库的项目。

在 Eclipse 中导入会导致一个工作项目,但只有所有头文件和我实现的源文件都能被 Eclipse 的索引正确识别。 我还想使用“ctrl+click”浏览一个外部库的源文件。我不知道如何在我的 CMakeList.txt 中添加该外部库的 *.cpp 文件,以便在不构建库的情况下让索引器识别它们。

【问题讨论】:

    标签: eclipse indexing cmake


    【解决方案1】:

    您可以像这样将 .cpp 文件标记为“仅头文件”:

    # find all filenames in the lib path and gather them in $YOUR_LIB
    FILE(GLOB YOUR_LIB path_to_library/*.?pp)
    
    # create a seperate sourcegroup so it doesn't clutter up the rest of your code
    SOURCE_GROUP(\\lib FILES ${YOUR_LIB})
    
    # mark them as header-file only
    SET_SOURCE_FILES_PROPERTIES(${YOUR_LIB} PROPERTIES HEADER_FILE_ONLY TRUE)
    
    # add both your code and the lib-code to the project
    ADD_EXECUTABLE(program ${YOUR_CODE} ${YOUR_LIB})
    

    【讨论】:

    • 我尝试了您的解决方案,但文件未编入索引。如果我删除 HEADER_FILE_ONLY 选项的设置,他会尝试构建文件。这意味着它们包含在构建中,但索引器没有找到它们。重建索引会导致与以前相同的输出:“索引 'g2o_ba@build'(5 个源,811 个标头)在 44.54 秒内”。我还检查了 Eclipse 属性“索引未包含在构建中的源文件”,“索引未使用的标头”。
    • 经过长时间的休息后,我继续处理我的 cmake 和 eclipse 问题。我发现,您的解决方案不适用于 cmake“Eclipse CDT4 - Unix Makefiles”设置。但是在tutorial 之后使用 Unix Makefile 生成器设置项目会导致所需的功能(使用您的 cmake sn-ps)。谢谢!
    【解决方案2】:

    我找到了一种将外部库源文件附加到与 CMake 项目生成器兼容的 Eclipse 项目的方法。

    事实证明,只有当外部库源是项目源文件夹的直接后代时,索引和“ctrl+click”导航才能正常工作。因此解决方案如下:

    • 扫描外部库文件夹中的源文件。
    • 在项目的源文件夹下创建一个子文件夹。
    • Symlink 在创建的文件夹中发现了源。

    我创建了一个执行上述步骤的 CMake 函数 attachExternalSources

    function(attachExternalSources librarySourceLocation folderName)
        # Create folder for Geant4 sources
        file(MAKE_DIRECTORY ${CMAKE_SOURCE_DIR}/${folderName})
    
        message(STATUS "Searching for C++ sources in \"${librarySourceLocation}\"...")
        FILE(GLOB_RECURSE libSources
            ${librarySourceLocation}/*.c
            ${librarySourceLocation}/*.cpp
            ${librarySourceLocation}/*.cxx
            ${librarySourceLocation}/*.cc
        )
    
        message(STATUS "Symlinking sources into\n   \"${CMAKE_SOURCE_DIR}/${folderName}\"\n   Please wait...")
        foreach(source ${libSources})
            # Obtain source filename
            get_filename_component(source_filename ${source} NAME)
    
            # Create symlink unless it already exists
            set(symlink "${CMAKE_SOURCE_DIR}/${folderName}/${source_filename}")     
            if(NOT EXISTS ${symlink})
                execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink ${source} ${symlink})
            endif()
        endforeach()
        
        # Scan all the symlinks created under the project folder and disable their compilation
        FILE(GLOB sources_symlinks ${CMAKE_SOURCE_DIR}/${folderName}/*)
        SET_SOURCE_FILES_PROPERTIES(${sources_symlinks} PROPERTIES HEADER_FILE_ONLY TRUE)
    endfunction()
    

    函数的使用如下。将上面的功能代码粘贴到您的CMakeLists.txt 中。接下来按如下方式使用:

    attachExternalSources("path/to/external/library/sources" "library-sources")
    

    第一个参数是外部库源代码的位置。第二个参数是项目中包含源符号链接的文件夹的名称。

    附:我用 Eclipse 4.19 和 CMake 3.20.5 测试了函数。

    【讨论】:

      猜你喜欢
      • 2017-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-31
      • 2022-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多