1
votes

I am using CMake to build a project with external libraries by using "Eclipse CDT4 - Unix Makefiles".

Importing in Eclipse leads to a working project, but only all header files and my implemented source files are recognized correctly by the index of Eclipse. I would also like to navigate through the source files for one external library by using "ctrl+click". I don't know how to add the *.cpp files of that external library in my CMakeList.txt to get them recognized by the indexer without building the library.

2

2 Answers

0
votes

You can mark the .cpp files as "header file only" like this:

# 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})
0
votes

I found a way to attach external library source files to the Eclipse project that is compatible with CMake project generator.

It turns out that to indexing and "ctrl+click" navigation works correctly only when external library sources are direct descendants of the project source folder. Therefore the solution is following:

  • Scan external library folder for source files.
  • Create a child folder under project's source folder.
  • Symlink discovered sources inside the created folder.

I created a CMake function attachExternalSources that performs above steps:

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 in \"${folderName}"...")
    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_BINARY_DIR}/${folderName}/*)
    SET_SOURCE_FILES_PROPERTIES(${sources_symlinks} PROPERTIES HEADER_FILE_ONLY TRUE)
endfunction()

The use of the function is following. Paste above function code in your CMakeLists.txt. Next, use it as follows:

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

First parameter is location of the external library source code. Second argument is the name of a folder inside your project that that will contain source symlinks.

P.S. I tested function with Eclipse 4.19 and CMake 3.20.5.