如何修改Qt5的CMakelists从Windows到Linux?

时间:2017-03-10 00:47:35

标签: c++ linux qt cmake qt5

(问题已经解决)

如果Qt5-OpenGL示例(http://doc.qt.io/qt-5/qtgui-openglwindow-example.html)只有$qtcreator openglwindow/openglwindow.pro,它将编译

我的CMakelist可以在Win10上构建,但在linux失败时,命令是:

[user]$ mkdir build && cd build && cmake .. && make 

输出错误如下:

 Scanning dependencies of target QtGL_test_automoc 
 [ 20%] Automatic mocfor target QtGL_test 
 Generating moc source QtGL_test_automoc.dir/moc_openglwindow_LRW7M26ARLGWDK.cpp 
 Generating moc compilation QtGL_test_automoc.cpp 
 [ 20%] Built target
 QtGL_test_automoc Scanning dependencies of target QtGL_test 
 [ 40%] Building CXX object CMakeFiles/QtGL_test.dir/main.cpp.o
 c++: fatal error: no input files compilation terminated. 
 /bin/sh:-DQT_CORE_LIB: command not found 
 make[2]: *** [CMakeFiles/QtGL_test.dir/build.make:63: MakeFiles/QtGL_test.dir/main.cpp.o] Error 127 
 make[1]: ***[CMakeFiles/Makefile2:68: CMakeFiles/QtGL_test.dir/all] Error 2 make:
 *** [Makefile:84: all] Error 2

我试图弄清楚如何修改linux的跨平台版本。

这是我的CMakeLists.txt:

cmake_minimum_required (VERSION 3.5)

set(PRROJ_NAME QtGL_test) 
project (${PRROJ_NAME})

set(HEADERS ${HEADERS} openglwindow.h ) 
set(SOURCES ${SOURCES} main.cpp ${HEADERS}) 
set(CMAKE_AUTOMOC ON)

add_executable(${PRROJ_NAME} ${SOURCES})
set(PROJ_INCLUDE_DIRS ${PROJ_INCLUDE_DIRS} ${CMAKE_SOURCE_DIR})

add_library(openglwindow openglwindow.cpp) 
set(PROJ_LIBRARIES ${PROJ_LIBRARIES} openglwindow)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

if(WIN32)
    find_path(CMAKE_PREFIX_PATH NAMES lib/cmake/Qt5/Qt5Config.cmake HINTS
        "$ENV{Qt5_ROOT}"
    )
    message("Qt5_ROOT is $ENV{Qt5_ROOT}") 
endif() 
message("CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH}")

list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake/Modules)

set(NEED_QT_COMPONENTS QTCORE QTGUI)
message("finding ${NEED_QT_COMPONENTS}")
foreach(COMPONENT Gui Core)
    message("find_package(Qt5${COMPONENT})")
    find_package("Qt5${COMPONENT}")
    message("Qt5${COMPONENT}_FOUND is ${Qt5${COMPONENT}_FOUND}")
    if("${Qt5${COMPONENT}_FOUND}")
        set(PROJ_INCLUDE_DIRS ${PROJ_INCLUDE_DIRS} "${Qt5${COMPONENT}_INCLUDE_DIRS}")
        set(PROJ_LIBRARIES ${PROJ_LIBRARIES} "Qt5::${COMPONENT}")
        add_definitions("${Qt5${COMPONENT}_DEFINITIONS}")
        qt5_use_modules(${PRROJ_NAME} ${COMPONENT})
    endif() 
endforeach(COMPONENT )

message("include_directories(${PROJ_INCLUDE_DIRS})")
include_directories(${PROJ_INCLUDE_DIRS})
message("target_link_libraries(${PRROJ_NAME} ${PROJ_LIBRARIES})")
target_link_libraries(${PRROJ_NAME} ${PROJ_LIBRARIES})
set(CMAKE_CXX_STANDARD 14)

if(UNIX)
    set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g")
    set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -I/usr/local/include -Wall")
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -L/usr/local/lib") 
endif()

1 个答案:

答案 0 :(得分:0)

终于解决了版本:

cmake_minimum_required (VERSION 3.5)

set(PRROJ_NAME QtGL_test) 
project (${PRROJ_NAME})

set(HEADERS ${HEADERS} openglwindow.h ) 
set(SOURCES ${SOURCES} main.cpp ${HEADERS}) 
set(CMAKE_AUTOMOC ON)

add_executable(${PRROJ_NAME} ${SOURCES})
set(PROJ_INCLUDE_DIRS ${PROJ_INCLUDE_DIRS} ${CMAKE_SOURCE_DIR})

add_library(openglwindow openglwindow.cpp) 
set(PROJ_LIBRARIES ${PROJ_LIBRARIES} openglwindow)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

if(WIN32)
    find_path(CMAKE_PREFIX_PATH NAMES lib/cmake/Qt5/Qt5Config.cmake HINTS
        "$ENV{Qt5_ROOT}"
    )
    message("Qt5_ROOT is $ENV{Qt5_ROOT}") 
endif() 
message("CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH}")

list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake/Modules)

set(QT_FLAGS "-fPIC")
set(NEED_QT_COMPONENTS QTCORE QTGUI)
message("finding ${NEED_QT_COMPONENTS}")
foreach(COMPONENT Gui Core)
    message("find_package(Qt5${COMPONENT})")
    find_package("Qt5${COMPONENT}")
    message("Qt5${COMPONENT}_FOUND is ${Qt5${COMPONENT}_FOUND}")
    if("${Qt5${COMPONENT}_FOUND}")
        set(PROJ_INCLUDE_DIRS ${PROJ_INCLUDE_DIRS} "${Qt5${COMPONENT}_INCLUDE_DIRS}")
        set(PROJ_LIBRARIES ${PROJ_LIBRARIES} "Qt5::${COMPONENT}")
        #Open add_definitions  will error
        #add_definitions("${Qt5${COMPONENT}_DEFINITIONS}")
        qt5_use_modules(${PRROJ_NAME} ${COMPONENT})
    endif() 
endforeach(COMPONENT )

message("include_directories(${PROJ_INCLUDE_DIRS})")
include_directories(${PROJ_INCLUDE_DIRS})
message("target_link_libraries(${PRROJ_NAME} ${PROJ_LIBRARIES})")
target_link_libraries(${PRROJ_NAME} ${PROJ_LIBRARIES})
set(CMAKE_CXX_STANDARD 14)

if(UNIX)
    set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g")
    set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${QT_FLAGS} -I/usr/local/include -Wall")
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -L/usr/local/lib") 
endif()