ROS with QtCreator: autocompletion

感情迁移 提交于 2019-12-12 03:22:28

问题


I'm using and like QtCreator to code and build my ROS projects written in c++.

Unfortunately the auto-completion for my own header files is not working: e.g. #include "LineTracker.hh"

Building the project works perfectly. And also the auto-completion for other external packages like ros or opencv is working.

Update 2.0: With QtCreator 3.6 the solution is not working

Update 1.0: Found a solution, see bottom!

Thats how my CMakeLists.txt looks:

cmake_minimum_required(VERSION 2.8.3)
project(line_tracking)

SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

find_package(catkin REQUIRED COMPONENTS
  roscpp
  tf
  sensor_msgs
  image_transport
  cv_bridge
)

catkin_package()


include_directories(
  include
  ${catkin_INCLUDE_DIRS}
  ${PROJECT_SOURCE_DIR}
)


add_executable(${PROJECT_NAME}
    src/line_tracking.cpp
    src/EDLineDetector.cpp
    src/LineTracker.cpp
)

target_link_libraries(${PROJECT_NAME}
  ${catkin_LIBRARIES}
)

# --- QT CREATOR STUFF ---

#Add all files in subdirectories of the project in
# a dummy_target so qtcreator has access to all files
FILE(GLOB children ${CMAKE_SOURCE_DIR}/*)
FOREACH(child ${children})
  IF(IS_DIRECTORY ${child})
    file(GLOB_RECURSE dir_files "${child}/*")
    LIST(APPEND extra_files ${dir_files})
  ENDIF()
ENDFOREACH()
add_custom_target(dummy_${PROJECT_NAME} SOURCES ${extra_files})
#

The file/package structure looks standard like this:

CMakeLists.txt
 |
 + -- src
 |
 + -- include

How do I have to adapt my CMakeLists.txt that QtCreator finds my headers for autocompletion?

Thank you very much for your help!

Sidenote:

When I use the top CMakeLists.txt file of the catkin workspace in QtCeator and I include the header files under their package path like this: #include <packageName/include/headerFile.h> the auto-completion is working but the build is not working anymore. So this is only a bad and not userfriendly hack to get auto-completion during coding.


Update 1.0:

I found a solution which is working:

I create a library from all the (class) files which have header files, and link the library to the main file, instead of adding the files as executables. I posted it here as answer.

But I don't know why it is working like this and not without the way over the library. Any explanations?


Update 2.0: I just upgraded to QtCreator 3.6 and there my solution with the library in not working anymore.

Does anybody know another solution?!


回答1:


Update: This solution does NOT work with QtCreator 3.6


I found a solution to my own question. Not so much fun, but anyway, I spent a lot of time with that issue so here the solution, which is hopefully useful for others:

Auto-completion with CMakeLists.txt in QtCreator 3.5.1 for your own classes:

  1. Create a library with all your classes: ADD_LIBRARY(myFilesLib src/class1.cpp ...)
  2. Add your executable (main function): add_executable(${PROJECT_NAME} src/main.cpp)
  3. Link your library to your executable: target_link_libraries(${PROJECT_NAME} myFilesLib)

With this way over the library, auto-completion is working in QtCreator!

For ROS (catkin) don't forget to link the ${catkin_LIBRARIES}.

Here the whole CMakeLists.txt:

cmake_minimum_required(VERSION 2.8.3)
project(example_project)

SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

find_package(catkin REQUIRED COMPONENTS
  roscpp
)

catkin_package()

include_directories(
  include
  ${catkin_INCLUDE_DIRS}
  ${PROJECT_SOURCE_DIR}
)

# Create a library with all your classes
add_library(myFilesLib
    src/class1.cpp
    src/class2.cpp
    src/class3.cpp
)
target_link_libraries(myFilesLib
    ${catkin_LIBRARIES}
)

# add your executable
add_executable(${PROJECT_NAME}
    src/main.cpp
)

# link the library with your classes to the executable
target_link_libraries(${PROJECT_NAME}
  ${catkin_LIBRARIES}
  myFilesLib
)

I don't know why it is working only with the way over the library but it is working. Maybe somebody has an explanation?!




回答2:


This is really annoying. Finally I figured out a solution, that works at Qt Creator 4.1.0, and probably works at other versions.

  1. Write your include_directories correctly, so that compilation is OK.
  2. Below the include_directories, add the following line. Note that the sequence matters, the following line must locate below the include_directories.

    file(GLOB_RECURSE HEADERS */*.hpp */*.h)
    
  3. Add ${HEADERS} # for qtcreator... in one of your add_executable. like:

    add_executable(slam_pp_node
      ${HEADERS} # for qtcreator...
      src/***.cpp
    )
    
  4. The above should be enough to solve the autocompletion problem. If not, add the following lines at the end of your CMakeList.txt:

    file(GLOB_RECURSE EXTRA_FILES */*)
    add_custom_target(${PROJECT_NAME}_OTHER_FILES ALL WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} SOURCES ${EXTRA_FILES})
    

Good luck!



来源:https://stackoverflow.com/questions/34846706/ros-with-qtcreator-autocompletion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!