c – 使用CMake的OpenGL Qt

前端之家收集整理的这篇文章主要介绍了c – 使用CMake的OpenGL Qt前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个使用* .pro文件创建的Qt项目,我需要将其迁移到CMakeLists.该项目使用简单的OpenGL动画来显示手的3D模型.我已经改变它使用CMake,但我遇到了2个问题. (程序编译但运行不正常)

>程序的内存消耗从使用* .pro文件的20-50MB传递到使用CMake的1.3GB(也许某些库被完全加载或者什么?)
>与使用* .pro文件的速度(大约每秒3帧)相比,程序运行速度非常慢(如每5-10秒一帧)

问题是,我做错了什么以及如何解决

这是* .pro文件

QT += core gui opengl

TARGET   = RGBD_3D_Viewer
TEMPLATE = app


SOURCES +=  main.cpp\
            mainwindow.cpp \
            glwidget.cpp \
            glwidget_Camera.cpp \
            glwidget_Comm.cpp \
            glwidget_Extractors.cpp \
            glwidget_Rendering.cpp \
            glwidget_Video.cpp \
            glwidget_UI_Mouse.cpp \
            glwidget_OpenGL.cpp \
            mainwindow_Comm.cpp \
            mainwindow_GUI.cpp \
            model.cpp \
            cameraSet.cpp \
            model_Mesh.cpp \
            model_Skeleton.cpp \
            model_Skin.cpp \
            model_Extra_SkinningStuff.cpp \
            animation.cpp \
            animation_Transform.cpp \
            videoSequence.cpp \
            sequence.cpp \
            mainwindow_UI_Keyboard_Mouse.cpp \
            tracker.cpp \
            mainwindow_FrameNumber.cpp \
            model_Limits.cpp \
            animation_Files_CompleteSequence.cpp \
            mainwindow_MODELS_INFO.cpp \
            modelSET.cpp \
            animation_0_RotAxes_Limits.cpp \
            myMATH.cpp \
            types_Background.cpp \
            model_Extra_VOI.cpp \
            fingertipSet.cpp \
            tracker_OnIndexChange.cpp \
            tracker_wFeatureSet.cpp

HEADERS  += mainwindow.h \
            glwidget.h \
            model.h \
            cameraSet.h \
            animation.h \
            videoSequence.h \
            sequence.h \
            tracker.h \
            mymath.h \
            modelSET.h \
            ui_mainwindow.h \
            featureSet.h \
            typesBackground.h \
            fingertipSet.h

FORMS    += mainwindow.ui



INCLUDEPATH += /usr/include/eigen3/

INCLUDEPATH += /home/cvg11/projects/development/RGBD_3D_Viewer/glm


LIBS += -L/usr/local/lib/
LIBS += -lopencv_core
LIBS += -lopencv_highgui


QMAKE_CXXFLAGS += -O3
QMAKE_CXXFLAGS += -frounding-math
#QMAKE_CXXFLAGS += -std=c++0x

这是CMakeLists.txt文件

project(3d_viewer)
cmake_minimum_required(VERSION 2.6 FATAL_ERROR)

include_directories(${CMAKE_CURRENT_BINARY_DIR})
include_directories( ${PROJECT_SOURCE_DIR}/include ${PROJECT_SOURCE_DIR}/glm)

find_package( PkgConfig )
pkg_check_modules( EIGEN3 required eigen3 )
include_directories( ${EIGEN3_INCLUDE_DIRS} )

# Opencv required
find_package(OpenCV COMPONENTS core highgui required)
include_directories(${OPENCV_INCLUDE_DIRS})
link_directories(${OPENCV_LIBRARY_DIRS})
add_definitions(${OPENCV_DEFINITIONS})

message("\n\nFound OpenCV\n\n")


# QT4 required
find_package(Qt4 COMPONENTS QtCore QtGui QtOpenGL required)
set(QT_USE_QTOPENGL TRUE)
include(${QT_USE_FILE})
add_definitions(${QT_DEFINITIONS})

message("\n\nFound QT4\n\n")


INCLUDE_DIRECTORIES(${QT_QTOPENGL_INCLUDE_DIR} ${OPENGL_INCLUDE_DIR} )

#set the default path for built executables to the "bin" directory
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
#set the default path for built libraries to the "lib" directory
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)


file(GLOB VIEWER_SOURCES src/*.cpp)

file(GLOB VIEWER_INCLUDES include/*.h)


# set QT headers
SET(QT_HEADERS
    include/mainwindow.h
    include/glwidget.h
    )

#set QT forms
SET(QT_FORMS
    ui/mainwindow.ui
)

# create moc for QT
QT4_WRAP_CPP(QT_MOC ${QT_HEADERS})

# process ui
QT4_WRAP_UI(QT_FORMS_HEADERS ${QT_FORMS})

ADD_EXECUTABLE(3d_viewer ${VIEWER_SOURCES} ${VIEWER_INCLUDES}
    ${QT_HEADERS}
    ${QT_MOC}
    ${QT_FORMS})

TARGET_LINK_LIBRARIES(3d_viewer ${QT_LIBRARIES} ${OpenCV_LIBS} )
set_property(TARGET 3d_viewer PROPERTY COMPILE_DEFINITIONS QT_SHARED)

编辑:

以下是make VERBOSE = 1的两个输出(我只显示最后一个链接和其中一个文件,因为其余文件是相同的)

CMake的:

[100%] Building CXX object CMakeFiles/3d_viewer.dir/include/moc_glwidget.cxx.o
/usr/bin/c++   -DQT_CORE_LIB -DQT_GUI_LIB -DQT_NO_DEBUG -DQT_OPENGL_LIB -DQT_SHARED -O3 -DNDEBUG -I/home/cvg11/projects/development/RGBD_3D_Viewer/build -I/home/cvg11/projects/development/RGBD_3D_Viewer/include -I/home/cvg11/projects/development/RGBD_3D_Viewer/glm -I/usr/include/eigen3 -I/usr/local/include/opencv -I/usr/local/include -isystem /usr/include/qt4 -isystem /usr/include/qt4/QtOpenGL -isystem /usr/include/qt4/QtGui -isystem /usr/include/qt4/QtCore    -o CMakeFiles/3d_viewer.dir/include/moc_glwidget.cxx.o -c /home/cvg11/projects/development/RGBD_3D_Viewer/build/include/moc_glwidget.cxx
Linking CXX executable ../bin/3d_viewer
/usr/local/bin/cmake -E cmake_link_script CMakeFiles/3d_viewer.dir/link.txt --verbose=1
/usr/bin/c++   -O3 -DNDEBUG    CMakeFiles/3d_viewer.dir/src/mainwindow_FrameNumber.cpp.o CMakeFiles/3d_viewer.dir/src/animation.cpp.o CMakeFiles/3d_viewer.dir/src/glwidget_OpenGL.cpp.o CMakeFiles/3d_viewer.dir/src/main.cpp.o CMakeFiles/3d_viewer.dir/src/mainwindow_Comm.cpp.o CMakeFiles/3d_viewer.dir/src/glwidget_Comm.cpp.o CMakeFiles/3d_viewer.dir/src/myMATH.cpp.o CMakeFiles/3d_viewer.dir/src/model.cpp.o CMakeFiles/3d_viewer.dir/src/glwidget_Rendering.cpp.o CMakeFiles/3d_viewer.dir/src/model_Extra_VOI.cpp.o CMakeFiles/3d_viewer.dir/src/videoSequence.cpp.o CMakeFiles/3d_viewer.dir/src/cameraSet.cpp.o CMakeFiles/3d_viewer.dir/src/model_Extra_SkinningStuff.cpp.o CMakeFiles/3d_viewer.dir/src/mainwindow_MODELS_INFO.cpp.o CMakeFiles/3d_viewer.dir/src/animation_0_RotAxes_Limits.cpp.o CMakeFiles/3d_viewer.dir/src/modelSET.cpp.o CMakeFiles/3d_viewer.dir/src/glwidget_Video.cpp.o CMakeFiles/3d_viewer.dir/src/animation_Transform.cpp.o CMakeFiles/3d_viewer.dir/src/glwidget_Camera.cpp.o CMakeFiles/3d_viewer.dir/src/sequence.cpp.o CMakeFiles/3d_viewer.dir/src/animation_Files_CompleteSequence.cpp.o CMakeFiles/3d_viewer.dir/src/glwidget_UI_Mouse.cpp.o CMakeFiles/3d_viewer.dir/src/model_Skin.cpp.o CMakeFiles/3d_viewer.dir/src/tracker_wFeatureSet.cpp.o CMakeFiles/3d_viewer.dir/src/tracker_OnIndexChange.cpp.o CMakeFiles/3d_viewer.dir/src/mainwindow.cpp.o CMakeFiles/3d_viewer.dir/src/types_Background.cpp.o CMakeFiles/3d_viewer.dir/src/glwidget_Extractors.cpp.o CMakeFiles/3d_viewer.dir/src/model_Limits.cpp.o CMakeFiles/3d_viewer.dir/src/model_Skeleton.cpp.o CMakeFiles/3d_viewer.dir/src/tracker.cpp.o CMakeFiles/3d_viewer.dir/src/model_Mesh.cpp.o CMakeFiles/3d_viewer.dir/src/mainwindow_UI_Keyboard_Mouse.cpp.o CMakeFiles/3d_viewer.dir/src/fingertipSet.cpp.o CMakeFiles/3d_viewer.dir/src/mainwindow_GUI.cpp.o CMakeFiles/3d_viewer.dir/src/glwidget.cpp.o CMakeFiles/3d_viewer.dir/include/moc_mainwindow.cxx.o CMakeFiles/3d_viewer.dir/include/moc_glwidget.cxx.o  -o ../bin/3d_viewer  -L/usr/local/cuda/lib64 -rdynamic -lglut -lXmu -lXi -lQtOpenGL -lQtGui -lQtCore /usr/local/lib/libopencv_core.so.2.4.9 /usr/local/lib/libopencv_highgui.so.2.4.9 /usr/local/lib/libopencv_core.so.2.4.9 -Wl,-rpath,/usr/local/cuda/lib64:/usr/local/lib -Wl,-rpath-link,/usr/local/lib 
make[2]: Leaving directory `/home/cvg11/projects/development/RGBD_3D_Viewer/build'
/usr/local/bin/cmake -E cmake_progress_report /home/cvg11/projects/development/RGBD_3D_Viewer/build/CMakeFiles  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
[100%] Built target 3d_viewer

* .pro项目:

g++ -c -pipe -frounding-math -O3 -O2 -w -D_REENTRANT -DQT_WEBKIT -DQT_OPENGL_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4/QtOpenGL -I/usr/include/qt4 -I/usr/include/eigen3 -I../../projects/development/RGBD_3D_Viewer/glm -I/usr/X11R6/include -I. -I. -o moc_mainwindow.o moc_mainwindow.cpp
/usr/bin/moc-qt4 -DQT_WEBKIT -DQT_OPENGL_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4/QtOpenGL -I/usr/include/qt4 -I/usr/include/eigen3 -I../../projects/development/RGBD_3D_Viewer/glm -I/usr/X11R6/include -I. -I. glwidget.h -o moc_glwidget.cpp
g++ -c -pipe -frounding-math -O3 -O2 -w -D_REENTRANT -DQT_WEBKIT -DQT_OPENGL_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4/QtOpenGL -I/usr/include/qt4 -I/usr/include/eigen3 -I../../projects/development/RGBD_3D_Viewer/glm -I/usr/X11R6/include -I. -I. -o moc_glwidget.o moc_glwidget.cpp
g++ -Wl,-O1 -o RGBD_3D_Viewer main.o mainwindow.o glwidget.o glwidget_Camera.o glwidget_Comm.o glwidget_Extractors.o glwidget_Rendering.o glwidget_Video.o glwidget_UI_Mouse.o glwidget_OpenGL.o mainwindow_Comm.o mainwindow_GUI.o model.o cameraSet.o model_Mesh.o model_Skeleton.o model_Skin.o model_Extra_SkinningStuff.o animation.o animation_Transform.o videoSequence.o sequence.o mainwindow_UI_Keyboard_Mouse.o tracker.o mainwindow_FrameNumber.o model_Limits.o animation_Files_CompleteSequence.o mainwindow_MODELS_INFO.o modelSET.o animation_0_RotAxes_Limits.o myMATH.o types_Background.o model_Extra_VOI.o fingertipSet.o tracker_OnIndexChange.o tracker_wFeatureSet.o moc_mainwindow.o moc_glwidget.o    -L/usr/lib/x86_64-linux-gnu -L/usr/X11R6/lib -L/usr/local/lib/ -lopencv_core -lopencv_highgui -lQtOpenGL -lQtGui -lQtCore -lGL -lpthread

我尝试添加/删除-frounding-math标志,没有任何明显的差异……

解决方法

您似乎在尝试使用qmake以错误的方式更改优化级别值:
QMAKE_CXXFLAGS += -O3

这一行的问题是g将默认使用-O2作为编译阶段,-O1作为链接阶段.您似乎只想更改编译器阶段,因为您没有指定链接器标志.但是,=表示使用qmake添加,而不是覆盖.实现原始目标的正确方法是:

QMAKE_CXXFLAGS_RELEASE -= -O2
QMAKE_CXXFLAGS_RELEASE += -O3

以及以下行来覆盖链接器阶段:

QMAKE_LFLAGS_RELEASE -= -O1

在此更改后,您自然需要重新运行qmake.现在,-O3意味着它将优化性能而不是空间.因此,您之前对空间的关注可能是由此造成的.然而,第二部分仍然有问题,没有具体细节.

cmake的默认优化级别与qmake不同;它是-O3.您可以通过运行以下简短的cmake代码段轻松检查:

message("CMAKE_CXX_FLAGS_RELEASE: ${CMAKE_CXX_FLAGS_RELEASE}")

您需要同步它们以使它们内联.例如,如果你想在任何地方使用-O2,也要覆盖cmake,你需要应用以下内容

set(CMAKE_CXX_FLAGS_RELEASE "-O2")

如果您想使用-O3,请参阅上述逻辑以在qmake项目文件中更改它.如果您想使用其他类似-Os,则需要应用这两种类型的更改.我想,这几乎就是它.

至于调试,您可能希望在两种情况下完全实现优化,以获得更好的调试体验!

通常,您需要决定性能与空间特性.你似乎在抱怨两者,但你天生就不会在两者上都获得完美主义.如果要完全优化空间,请使用-Os,如果要获得性能,请使用-O3,如果需要妥协解决方案,请使用介于两者之间的内容等.

原文链接:https://www.f2er.com/c/119694.html

猜你在找的C&C++相关文章