在Ubuntu Linux

时间:2017-01-13 21:17:18

标签: c++ linux qt build static-linking

这是我做的:
1.下载来源
2. ./configure -static -prefix /home/user/build/qt-static/build -qt-xcb
3. make -j3
4. make install
建成1小时后,我做了一个简单的test.cpp:

    #include <QtGui>

    int main(int argc, char *argv[]) {
    QApplication app(argc, argv);
    QMainWindow window;
    window.resize(250, 150);
    window.setWindowTitle("Test");
    window.show();
    return app.exec();
    }
  1. 创建了一个项目文件:/home/user/build/final/bin/qmake -project

    TEMPLATE = app
    TARGET = test
    INCLUDEPATH += .  # missing path ?
    # I also tried adding CONFIG += widgets or CONFIG += static, both resulting in the same errors
    SOURCES += test.cpp
    
  2. 创建了一个Makefile:/home/user/build/final/bin/qmake *pro
    Makefile的一部分:

    #############################################################################
    # Makefile for building: stat
    # Generated by qmake (3.0) (Qt 5.7.1)
    # Project:  stat.pro
    # Template: app
    # Command: /home/user/build/final/bin/qmake -config release -o Makefile stat.pro
    #############################################################################
    
    MAKEFILE      = Makefile
    
    ####### Compiler, tools and options
    
    CC            = gcc
    CXX           = g++
    DEFINES       = -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB
    CFLAGS        = -pipe -O2 -Wall -W -D_REENTRANT -fPIC $(DEFINES)
    CXXFLAGS      = -pipe -O2 -std=gnu++11 -Wall -W -D_REENTRANT -fPIC $(DEFINES)
    INCPATH       = -I. -I. -I../build/final/include -I../build/final/include/QtGui -I../build/final/include/QtCore -I. -I../build/final/mkspecs/linux-g++
    QMAKE         = /home/user/build/final/bin/qmake
    DEL_FILE      = rm -f
    CHK_DIR_EXISTS= test -d
    MKDIR         = mkdir -p
    COPY          = cp -f
    COPY_FILE     = cp -f
    COPY_DIR      = cp -f -R
    INSTALL_FILE  = install -m 644 -p
    INSTALL_PROGRAM = install -m 755 -p
    INSTALL_DIR   = cp -f -R
    DEL_FILE      = rm -f
    SYMLINK       = ln -f -s
    DEL_DIR       = rmdir
    MOVE          = mv -f
    TAR           = tar -cf
    COMPRESS      = gzip -9f
    DISTNAME      = stat1.0.0
    DISTDIR = /home/user/stat/.tmp/stat1.0.0
    LINK          = g++
    LFLAGS        = -Wl,-O1
    LIBS          = $(SUBLIBS) -L/home/user/build/final/lib -L/home/user/build/final/plugins/platforms -lqxcb -L/home/user/build/final/plugins/xcbglintegrations -lqxcb-egl-integration -lqxcb-glx-integration -lxcb-glx -lQt5XcbQpa -lSM -lICE -lxcb-static -L/home/user/build/final/plugins/imageformats -lqdds -lqicns -lqico -lqtga -lqtiff -lqwbmp -lqwebp -L/home/user/build/final/plugins/egldeviceintegrations -lqeglfs-x11-integration -lX11-xcb -lxcb -lQt5EglDeviceIntegration -lQt5PlatformSupport -lQt5DBus -lqtfreetype -lXext -lX11 -lQt5Gui -lqtpng -lqtharfbuzzng -lQt5Core -lz -lqtpcre -lm -ldl -lrt -lEGL -lGL -lpthread 
    AR            = ar cqs
    RANLIB        = 
    SED           = sed
    STRIP         = strip
    
    ####### Output directory
    
    OBJECTS_DIR   = ./
    
    ####### Files
    
    SOURCES       = test.cpp /home/user/stat/stat_plugin_import.cpp
    OBJECTS       = test.o \
            stat_plugin_import.o
    DIST          = ../build/final/mkspecs/features/spec_pre.prf \
            ../build/final/mkspecs/common/unix.conf \
            ../build/final/mkspecs/common/linux.conf \
            ../build/final/mkspecs/common/sanitize.conf \
            ../build/final/mkspecs/common/gcc-base.conf \
            ../build/final/mkspecs/common/gcc-base-unix.conf \
            ../build/final/mkspecs/common/g++-base.conf \
            ../build/final/mkspecs/common/g++-unix.conf \
            ../build/final/mkspecs/qconfig.pri \
            ../build/final/mkspecs/modules/qt_lib_3dcore.pri \
            #CUT
            ../build/final/mkspecs/features/qt_config.prf \
            ../build/final/mkspecs/linux-g++/qmake.conf \
            ../build/final/mkspecs/features/spec_post.prf \
            .qmake.stash \
            ../build/final/mkspecs/features/exclusive_builds.prf \
            ../build/final/mkspecs/features/toolchain.prf \
            ../build/final/mkspecs/features/default_pre.prf \
            ../build/final/mkspecs/features/resolve_config.prf \
            ../build/final/mkspecs/features/default_post.prf \
            ../build/final/mkspecs/features/warn_on.prf \
            ../build/final/mkspecs/features/qt.prf \
            ../build/final/mkspecs/features/resources.prf \
            ../build/final/mkspecs/features/moc.prf \
            ../build/final/mkspecs/features/unix/opengl.prf \
            ../build/final/mkspecs/features/unix/thread.prf \
            ../build/final/mkspecs/features/file_copies.prf \
            ../build/final/mkspecs/features/testcase_targets.prf \
            ../build/final/mkspecs/features/exceptions.prf \
            ../build/final/mkspecs/features/yacc.prf \
            ../build/final/mkspecs/features/lex.prf \
            stat.pro  test.cpp
    QMAKE_TARGET  = stat
    DESTDIR       = 
    TARGET        = stat
    
    
    first: all
    ####### Build rules
    
    $(TARGET): /home/user/build/final/lib/libQt5Gui.a /home/user/build/final/lib/libQt5Core.a $(OBJECTS)  
        $(LINK) $(LFLAGS) -o $(TARGET) $(OBJECTS) $(OBJCOMP) $(LIBS)
    
    Makefile: stat.pro ../build/final/mkspecs/linux-g++/qmake.conf ../build/final/mkspecs/features/spec_pre.prf \
            ../build/final/mkspecs/common/unix.conf \
            ../build/final/mkspecs/common/linux.conf \
            ../build/final/mkspecs/common/sanitize.conf \
            ../build/final/mkspecs/common/gcc-base.conf \
            ../build/final/mkspecs/common/gcc-base-unix.conf \
            ../build/final/mkspecs/common/g++-base.conf \
            ../build/final/mkspecs/common/g++-unix.conf \
            ../build/final/mkspecs/qconfig.pri \
            ../build/final/mkspecs/modules/qt_lib_3dcore.pri \
            ../build/final/mkspecs/modules/qt_lib_3dcore_private.pri \
            ../build/final/mkspecs/modules/qt_lib_3dextras.pri \
            ../build/final/mkspecs/modules/qt_lib_3dextras_private.pri \
            ../build/final/mkspecs/modules/qt_lib_3dinput.pri \
            ../build/final/mkspecs/modules/qt_lib_3dinput_private.pri \
            ../build/final/mkspecs/modules/qt_lib_3dlogic.pri \
            ../build/final/mkspecs/modules/qt_lib_3dlogic_private.pri \
            ../build/final/mkspecs/modules/qt_lib_3dquick.pri \
            #CUT
            ../build/final/mkspecs/features/yacc.prf \
            ../build/final/mkspecs/features/lex.prf \
            stat.pro \
            ../build/final/lib/libQt5Gui.prl \
            ../build/final/lib/libQt5Core.prl \
            ../build/final/plugins/platforms/libqxcb.prl \
            ../build/final/plugins/xcbglintegrations/libqxcb-egl-integration.prl \
            ../build/final/plugins/xcbglintegrations/libqxcb-glx-integration.prl \
            ../build/final/plugins/imageformats/libqdds.prl \
            ../build/final/plugins/imageformats/libqicns.prl \
            ../build/final/plugins/imageformats/libqico.prl \
            ../build/final/plugins/imageformats/libqtga.prl \
            ../build/final/plugins/imageformats/libqtiff.prl \
            ../build/final/plugins/imageformats/libqwbmp.prl \
            ../build/final/plugins/imageformats/libqwebp.prl \
            ../build/final/plugins/egldeviceintegrations/libqeglfs-x11-integration.prl
        $(QMAKE) -config release -o Makefile stat.pro
    ../build/final/mkspecs/features/spec_pre.prf:
    ../build/final/mkspecs/common/unix.conf:
    ../build/final/mkspecs/common/linux.conf:
    ../build/final/mkspecs/common/sanitize.conf:
    ../build/final/mkspecs/common/gcc-base.conf:
    ../build/final/mkspecs/common/gcc-base-unix.conf:
    ../build/final/mkspecs/common/g++-base.conf:
    ../build/final/mkspecs/common/g++-unix.conf:
    ../build/final/mkspecs/qconfig.pri:
    ../build/final/mkspecs/modules/qt_lib_3dcore.pri:
    #CUT
    ../build/final/mkspecs/modules/qt_plugin_qtuiotouchplugin.pri:
    ../build/final/mkspecs/modules/qt_plugin_qtvirtualkeyboardplugin.pri:
    ../build/final/mkspecs/modules/qt_plugin_qwbmp.pri:
    ../build/final/mkspecs/modules/qt_plugin_qwebp.pri:
    ../build/final/mkspecs/modules/qt_plugin_qxcb-egl-integration.pri:
    ../build/final/mkspecs/modules/qt_plugin_qxcb-glx-integration.pri:
    ../build/final/mkspecs/modules/qt_plugin_qxcb.pri:
    ../build/final/mkspecs/features/qt_functions.prf:
    ../build/final/mkspecs/features/qt_config.prf:
    ../build/final/mkspecs/linux-g++/qmake.conf:
    ../build/final/mkspecs/features/spec_post.prf:
    .qmake.stash:
    ../build/final/mkspecs/features/exclusive_builds.prf:
    ../build/final/mkspecs/features/toolchain.prf:
    ../build/final/mkspecs/features/default_pre.prf:
    ../build/final/mkspecs/features/resolve_config.prf:
    ../build/final/mkspecs/features/default_post.prf:
    ../build/final/mkspecs/features/warn_on.prf:
    ../build/final/mkspecs/features/qt.prf:
    ../build/final/mkspecs/features/resources.prf:
    ../build/final/mkspecs/features/moc.prf:
    ../build/final/mkspecs/features/unix/opengl.prf:
    ../build/final/mkspecs/features/unix/thread.prf:
    ../build/final/mkspecs/features/file_copies.prf:
    ../build/final/mkspecs/features/testcase_targets.prf:
    ../build/final/mkspecs/features/exceptions.prf:
    ../build/final/mkspecs/features/yacc.prf:
    ../build/final/mkspecs/features/lex.prf:
    stat.pro:
    ../build/final/lib/libQt5Gui.prl:
    ../build/final/lib/libQt5Core.prl:
    ../build/final/plugins/platforms/libqxcb.prl:
    ../build/final/plugins/xcbglintegrations/libqxcb-egl-integration.prl:
    ../build/final/plugins/xcbglintegrations/libqxcb-glx-integration.prl:
    ../build/final/plugins/imageformats/libqdds.prl:
    ../build/final/plugins/imageformats/libqicns.prl:
    ../build/final/plugins/imageformats/libqico.prl:
    ../build/final/plugins/imageformats/libqtga.prl:
    ../build/final/plugins/imageformats/libqtiff.prl:
    ../build/final/plugins/imageformats/libqwbmp.prl:
    ../build/final/plugins/imageformats/libqwebp.prl:
    ../build/final/plugins/egldeviceintegrations/libqeglfs-x11-integration.prl:
    qmake: FORCE
        @$(QMAKE) -config release -o Makefile stat.pro
    
    qmake_all: FORCE
    
    
    all: Makefile $(TARGET)
    
    dist: distdir FORCE
        (cd `dirname $(DISTDIR)` && $(TAR) $(DISTNAME).tar $(DISTNAME) && $(COMPRESS) $(DISTNAME).tar) && $(MOVE) `dirname $(DISTDIR)`/$(DISTNAME).tar.gz . && $(DEL_FILE) -r $(DISTDIR)
    
    distdir: FORCE
        @test -d $(DISTDIR) || mkdir -p $(DISTDIR)
        $(COPY_FILE) --parents $(DIST) $(DISTDIR)/
        $(COPY_FILE) --parents test.cpp $(DISTDIR)/
    
    
    clean: compiler_clean 
        -$(DEL_FILE) $(OBJECTS)
        -$(DEL_FILE) *~ core *.core
    
    
    distclean: clean 
        -$(DEL_FILE) $(TARGET) 
        -$(DEL_FILE) /home/user/stat/stat_plugin_import.cpp .qmake.stash
        -$(DEL_FILE) Makefile
    
    
    ####### Sub-libraries
    
    mocclean: compiler_moc_header_clean compiler_moc_source_clean
    
    mocables: compiler_moc_header_make_all compiler_moc_source_make_all
    
    check: first
    
    benchmark: first
    
    compiler_rcc_make_all:
    compiler_rcc_clean:
    compiler_moc_header_make_all:
    compiler_moc_header_clean:
    compiler_moc_source_make_all:
    compiler_moc_source_clean:
    compiler_yacc_decl_make_all:
    compiler_yacc_decl_clean:
    compiler_yacc_impl_make_all:
    compiler_yacc_impl_clean:
    compiler_lex_make_all:
    compiler_lex_clean:
    compiler_clean: 
    
    ####### Compile
    
    test.o: test.cpp ../build/final/include/QtGui/QtGui \
            ../build/final/include/QtGui/QtGuiDepends \
            ../build/final/include/QtCore/QtCore \
            ../build/final/include/QtCore/QtCoreDepends \
            ../build/final/include/QtCore/qabstractanimation.h \
            ../build/final/include/QtCore/qobject.h \
            ../build/final/include/QtCore/qobjectdefs.h \
            ../build/final/include/QtCore/qnamespace.h \
            ../build/final/include/QtCore/qglobal.h \
            ../build/final/include/QtCore/qconfig.h \
            ../build/final/include/QtCore/qfeatures.h \
            ../build/final/include/QtCore/qsystemdetection.h \
            ../build/final/include/QtCore/qprocessordetection.h \
            ../build/final/include/QtCore/qcompilerdetection.h \
            ../build/final/include/QtCore/qtypeinfo.h \
            ../build/final/include/QtCore/qtypetraits.h \
            #CUT
            ../build/final/include/QtGui/qtextobject.h \
            ../build/final/include/QtGui/qtextdocumentfragment.h \
            ../build/final/include/QtGui/qtextdocumentwriter.h \
            ../build/final/include/QtGui/qtextlist.h \
            ../build/final/include/QtGui/qtexttable.h \
            ../build/final/include/QtGui/qdesktopservices.h \
            ../build/final/include/QtGui/qvalidator.h \
            ../build/final/include/QtGui/qtguiversion.h
        $(CXX) -c $(CXXFLAGS) $(INCPATH) -o test.o test.cpp
    
    stat_plugin_import.o: /home/user/stat/stat_plugin_import.cpp 
        $(CXX) -c $(CXXFLAGS) $(INCPATH) -o stat_plugin_import.o /home/user/stat/stat_plugin_import.cpp
    
    ####### Install
    
    install:  FORCE
    
    uninstall:  FORCE
    
    FORCE:
    
  3. make会导致错误:

    Makefile:1169: warning: overriding commands for target `stat_plugin_import.o'
    Makefile:808: warning: ignoring old commands for target `stat_plugin_import.o'
    g++ -c -pipe -O2 -std=gnu++11 -Wall -W -D_REENTRANT -fPIC -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -I. -I. -I../build/final/include -I../build/final/include/QtGui -I../build/final/include/QtCore -I. -I../build/final/mkspecs/linux-g++ -o test.o test.cpp
    test.cpp: In function ‘int main(int, char**)’:
    test.cpp:3:22: error: variable ‘QApplication app’ has initializer but incomplete type
         QApplication app(argc, argv);
                          ^
    test.cpp:4:5: error: ‘QMainWindow’ was not declared in this scope
         QMainWindow window;
         ^
    test.cpp:4:17: error: expected ‘;’ before ‘window’
         QMainWindow window;
                     ^
    test.cpp:5:5: error: ‘window’ was not declared in this scope
         window.resize(250, 150);
         ^
    make: *** [test.o] Error 1
    
  4. 所以我阅读了官方文档,并尝试了以下命令: PATH=/home/user/build/final/bin:$PATH
    export PATH
    qmake -config release
    make
    但是我得到了与上面相同的错误

  5. #include <QtGui>更改为#include <QApplication>#include <QMainWindow>会返回:

    g++ -c -pipe -O2 -std=gnu++11 -Wall -W -D_REENTRANT -fPIC -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -I. -I. -I../build/final/include -I../build/final/include/QtGui -I../build/final/include/QtCore -I. -I../build/final/mkspecs/linux-g++ -o test.o test.cpp
    test.cpp:2:24: fatal error: QApplication: No such file or directory
     #include <QApplication>
                            ^
    compilation terminated.
    make: *** [test.o] Error 1
    
  6. 提前感谢您的帮助

1 个答案:

答案 0 :(得分:0)

更改

#include <QtGui>

#include <QApplication>
#include <QMainWindow>

AlexanderXV的评论很好,因为它告诉链接器使用QtWidgets并为编译器设置正确的路径。在Qt5中,.pro文件中的命令是:

QT += widgets

来源:https://doc.qt.io/qt-5/qtwidgets-index.html

PLUS:

您可以使用单个#include <QtWidgets>,它包含来自QtWidgets的所有其他头文件。