vscode+cmake+qt5 错误记录

错误信息

 error LNK2005: main 已经在 CMakeCXXCompilerId.obj 中定义

分析

  • main函数重定义导致

原因

  • 出错时的cmake文件内容
cmake_minimum_required(VERSION 3.18)

project(cmake_demo01 LANGUAGES CXX)


set(CMAKE_PREFIX_PATH "C:/major/development/tools/qt/5.14/install/5.14.2/msvc2015_64")

set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)


find_package(Qt5 COMPONENTS Widgets Gui Core LinguistTools REQUIRED)

# set(TS_FILES cmake_demo01_zh_CN.ts)



file(GLOB_RECURSE src_file ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)
file(GLOB_RECURSE inc_file ${CMAKE_CURRENT_SOURCE_DIR}/include/*.h)


  add_executable(${PROJECT_NAME}
    ${src_file}
    ${inc_file}
    # ${TS_FILES}
  )


target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include  ${CMAKE_CURRENT_SOURCE_DIR}/ui)
# target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include)

target_link_libraries(cmake_demo01 PRIVATE Qt5::Widgets Qt5::Core Qt5::Gui)
# qt5_create_translation(QM_FILES ${CMAKE_SOURCE_DIR} ${TS_FILES})
  • 出错的语句就是file(GLOB_RECURSE src_file ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)

此时的文件目结构目录

  • 仅仅列出了关键目录
│  CMakeLists.txt
│  main.cpp
│  
├─build
│  │        
│  .......
│  │  │  ├─CompilerIdCXX
│  │  │  │  │  CMakeCXXCompilerId.cpp
│  │  │  │  │  CompilerIdCXX.exe
│  │  │  │  │  CompilerIdCXX.vcxproj
......
│                      
├─include
│      dialog.h
│      
└─src
        dialog.cpp
        dialog.ui

错误提示分析

  • 根据上面的错误提示: mainCMakeCXXCompilerId中已经出现,结合上面的文件树中可发现,有一个名为CMakeCXXCompilerId.cpp的文件, 打开它,搜索main发现, 里面有一个main函数
  • 结合main.cpp中的main, 就俩main 函数了, 故而出现了重定义

解决

  • 目标: PROJECT_NAME只能含有main.cpp,不能有其他的 main.
  • 方案1: 将main.cpp分离到其他目录
  • 方案2: 指项目中不不在${CMAKE_CURRENT_SOURCE_DIR}所在目录下

我的解决方案: 方案1, 将main.cpp放到src目录下

我的方案

  • 改写cmakeLists.txt文件
file(GLOB_RECURSE src_file ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp)
  • 此时, 目录结构如下
│  1.txt
│  CMakeLists.txt
│  
├─include
│      dialog.h
│      
└─src
        dialog.cpp
        dialog.ui
        main.cpp
  • cmake clean , 重新编译就好

最终的cmake文件

cmake_minimum_required(VERSION 3.18)

project(cmake_demo01 LANGUAGES CXX)


set(CMAKE_PREFIX_PATH "C:/major/development/tools/qt/5.14/install/5.14.2/msvc2015_64")

set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)


find_package(Qt5 COMPONENTS Widgets Gui Core LinguistTools REQUIRED)

# set(TS_FILES cmake_demo01_zh_CN.ts)



file(GLOB_RECURSE src_file ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp)
file(GLOB_RECURSE inc_file ${CMAKE_CURRENT_SOURCE_DIR}/include/*.h)
# file(GLOB_RECURSE ui_file ${CMAKE_CURRENT_SOURCE_DIR}/ui/*.ui)

# qt5_wrap_ui(wrap_ui_file ${ui_file})

# SOURCE_GROUP("ui" FILES ${ui_file} ${wrap_ui_file})

  add_executable(${PROJECT_NAME}
    ${src_file}
    ${inc_file}
    # ${wrap_ui_file}
    # ${TS_FILES}
  )


target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include  ${CMAKE_CURRENT_SOURCE_DIR}/ui)
# target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include)

target_link_libraries(cmake_demo01 PRIVATE Qt5::Widgets Qt5::Core Qt5::Gui)
# qt5_create_translation(QM_FILES ${CMAKE_SOURCE_DIR} ${TS_FILES})

posted @ 2022-03-18 00:23  mohist  阅读(953)  评论(0编辑  收藏  举报