利用qmake產生Makefile檔案

來源:互聯網
上載者:User

雖說qt已經被諾基亞放棄,但是qt項目還是在進行,5.0已經出來了。

在linux下寫程式,免不了要寫Makefile檔案,用automake,總感覺比較麻煩,linux人喜歡做麻煩的事,以顯得風格迥異。

其實用qmake產生Makefile檔案相當簡單。

1 裝好qmake工具

2 在源碼工程目錄下,運行qmake -project

這樣會產生一個*.pro檔案。

3 運行qmake -makefile *.pro,產生Makefile檔案。

此Makefile檔案,包含很多的qt的資訊,如果工程中沒有用到qt相關的庫,那麼可以刪去。


例子:

一個工程目錄下:

有三個目錄,a,b,m。其中a目錄下有a.c和a.h,b目錄下b.c和b.h,m目錄下main.c

a.c內容:

#include <stdio.h>
#include "a.h"

int a(void)
{
    printf("call a!\n");
    return 0;
}
a.h內容:

#ifndef __A_H
#define __A_H
int a(void);
#endif

-------------------

b.c內容:

#include <stdio.h>
#include "b.h"

int b(void)
{
    printf("call b!\n");
    return 0;
}

b.h內容:

#ifndef __B_H
#define __B_H
int b(void);
#endif

--------------------

main.c內容:

#include <stdio.h>
#include "a.h"
#include "b.h"
int main(int argc, char** argv)
{
    if(argc > 1)
    {
        printf("%s\n", argv[1]);
    }
    printf("Hello, Linux World!\n");
    a();
    b();    
    return 0;
}
-------------------

產生的.pro檔案

######################################################################
# Automatically generated by qmake (2.01a) Thu Mar 7 09:38:10 2013
######################################################################

TEMPLATE = app
TARGET =
DEPENDPATH += . a b m
INCLUDEPATH += . a b

# Input
HEADERS += a/a.h b/b.h
SOURCES += a/a.c b/b.c m/main.c

-----------------------------

產生的Makefile檔案

#############################################################################
# Makefile for building: test3
# Generated by qmake (2.01a) (Qt 4.6.2) on: Thu Mar 7 10:01:40 2013
# Project:  test3.pro
# Template: app
# Command: /usr/bin/qmake -unix -o Makefile test3.pro
#############################################################################

####### 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 $(DEFINES)
CXXFLAGS      = -pipe -O2 -Wall -W -D_REENTRANT $(DEFINES)
INCPATH       = -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -Ia -Ib -I.
LINK          = g++
LFLAGS        = -Wl,-O1
LIBS          = $(SUBLIBS)  -L/usr/lib -lQtGui -lQtCore -lpthread

AR            = ar cqs
RANLIB        =
QMAKE         = /usr/bin/qmake
TAR           = tar -cf
COMPRESS      = gzip -9f
COPY          = cp -f
SED           = sed
COPY_FILE     = $(COPY)
COPY_DIR      = $(COPY) -r
STRIP         = strip
INSTALL_FILE  = install -m 644 -p
INSTALL_DIR   = $(COPY_DIR)
INSTALL_PROGRAM = install -m 755 -p
DEL_FILE      = rm -f
SYMLINK       = ln -f -s
DEL_DIR       = rmdir
MOVE          = mv -f
CHK_DIR_EXISTS= test -d
MKDIR         = mkdir -p

####### Output directory

OBJECTS_DIR   = ./

####### Files

SOURCES       = a/a.c \
        b/b.c \
        m/main.c
OBJECTS       = a.o \
        b.o \
        main.o
DIST          = /usr/share/qt4/mkspecs/common/g++.conf \
        /usr/share/qt4/mkspecs/common/unix.conf \
        /usr/share/qt4/mkspecs/common/linux.conf \
        /usr/share/qt4/mkspecs/qconfig.pri \
        /usr/share/qt4/mkspecs/features/qt_functions.prf \
        /usr/share/qt4/mkspecs/features/qt_config.prf \
        /usr/share/qt4/mkspecs/features/exclusive_builds.prf \
        /usr/share/qt4/mkspecs/features/default_pre.prf \
        /usr/share/qt4/mkspecs/features/release.prf \
        /usr/share/qt4/mkspecs/features/default_post.prf \
        /usr/share/qt4/mkspecs/features/warn_on.prf \
        /usr/share/qt4/mkspecs/features/qt.prf \
        /usr/share/qt4/mkspecs/features/unix/thread.prf \
        /usr/share/qt4/mkspecs/features/moc.prf \
        /usr/share/qt4/mkspecs/features/resources.prf \
        /usr/share/qt4/mkspecs/features/uic.prf \
        /usr/share/qt4/mkspecs/features/yacc.prf \
        /usr/share/qt4/mkspecs/features/lex.prf \
        /usr/share/qt4/mkspecs/features/include_source_dir.prf \
        test3.pro
QMAKE_TARGET  = test3
DESTDIR       =
TARGET        = test3

first: all
####### Implicit rules

.SUFFIXES: .o .c .cpp .cc .cxx .C

.cpp.o:
    $(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<"

.cc.o:
    $(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<"

.cxx.o:
    $(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<"

.C.o:
    $(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<"

.c.o:
    $(CC) -c $(CFLAGS) $(INCPATH) -o "$@" "$<"

####### Build rules

all: Makefile $(TARGET)

$(TARGET):  $(OBJECTS)  
    $(LINK) $(LFLAGS) -o $(TARGET) $(OBJECTS) $(OBJCOMP) $(LIBS)

Makefile: test3.pro  /usr/share/qt4/mkspecs/linux-g++/qmake.conf /usr/share/qt4/mkspecs/common/g++.conf \
        /usr/share/qt4/mkspecs/common/unix.conf \
        /usr/share/qt4/mkspecs/common/linux.conf \
        /usr/share/qt4/mkspecs/qconfig.pri \
        /usr/share/qt4/mkspecs/features/qt_functions.prf \
        /usr/share/qt4/mkspecs/features/qt_config.prf \
        /usr/share/qt4/mkspecs/features/exclusive_builds.prf \
        /usr/share/qt4/mkspecs/features/default_pre.prf \
        /usr/share/qt4/mkspecs/features/release.prf \
        /usr/share/qt4/mkspecs/features/default_post.prf \
        /usr/share/qt4/mkspecs/features/warn_on.prf \
        /usr/share/qt4/mkspecs/features/qt.prf \
        /usr/share/qt4/mkspecs/features/unix/thread.prf \
        /usr/share/qt4/mkspecs/features/moc.prf \
        /usr/share/qt4/mkspecs/features/resources.prf \
        /usr/share/qt4/mkspecs/features/uic.prf \
        /usr/share/qt4/mkspecs/features/yacc.prf \
        /usr/share/qt4/mkspecs/features/lex.prf \
        /usr/share/qt4/mkspecs/features/include_source_dir.prf
    $(QMAKE) -unix -o Makefile test3.pro
/usr/share/qt4/mkspecs/common/g++.conf:
/usr/share/qt4/mkspecs/common/unix.conf:
/usr/share/qt4/mkspecs/common/linux.conf:
/usr/share/qt4/mkspecs/qconfig.pri:
/usr/share/qt4/mkspecs/features/qt_functions.prf:
/usr/share/qt4/mkspecs/features/qt_config.prf:
/usr/share/qt4/mkspecs/features/exclusive_builds.prf:
/usr/share/qt4/mkspecs/features/default_pre.prf:
/usr/share/qt4/mkspecs/features/release.prf:
/usr/share/qt4/mkspecs/features/default_post.prf:
/usr/share/qt4/mkspecs/features/warn_on.prf:
/usr/share/qt4/mkspecs/features/qt.prf:
/usr/share/qt4/mkspecs/features/unix/thread.prf:
/usr/share/qt4/mkspecs/features/moc.prf:
/usr/share/qt4/mkspecs/features/resources.prf:
/usr/share/qt4/mkspecs/features/uic.prf:
/usr/share/qt4/mkspecs/features/yacc.prf:
/usr/share/qt4/mkspecs/features/lex.prf:
/usr/share/qt4/mkspecs/features/include_source_dir.prf:
qmake:  FORCE
    @$(QMAKE) -unix -o Makefile test3.pro

dist:
    @$(CHK_DIR_EXISTS) .tmp/test31.0.0 || $(MKDIR) .tmp/test31.0.0
    $(COPY_FILE) --parents $(SOURCES) $(DIST) .tmp/test31.0.0/ && $(COPY_FILE) --parents a/a.h b/b.h .tmp/test31.0.0/ && $(COPY_FILE) --parents a/a.c b/b.c m/main.c .tmp/test31.0.0/ && (cd `dirname .tmp/test31.0.0` && $(TAR) test31.0.0.tar test31.0.0 && $(COMPRESS)
test31.0.0.tar) && $(MOVE) `dirname .tmp/test31.0.0`/test31.0.0.tar.gz . && $(DEL_FILE) -r .tmp/test31.0.0

clean:compiler_clean
    -$(DEL_FILE) $(OBJECTS)
    -$(DEL_FILE) *~ core *.core

####### Sub-libraries

distclean: clean
    -$(DEL_FILE) $(TARGET)
    -$(DEL_FILE) Makefile

mocclean: compiler_moc_header_clean compiler_moc_source_clean

mocables: compiler_moc_header_make_all compiler_moc_source_make_all

compiler_moc_header_make_all:
compiler_moc_header_clean:
compiler_rcc_make_all:
compiler_rcc_clean:
compiler_image_collection_make_all: qmake_image_collection.cpp
compiler_image_collection_clean:
    -$(DEL_FILE) qmake_image_collection.cpp
compiler_moc_source_make_all:
compiler_moc_source_clean:
compiler_uic_make_all:
compiler_uic_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

a.o: a/a.c a/a.h
    $(CC) -c $(CFLAGS) $(INCPATH) -o a.o a/a.c

b.o: b/b.c b/b.h
    $(CC) -c $(CFLAGS) $(INCPATH) -o b.o b/b.c

main.o: m/main.c a/a.h \
        b/b.h
    $(CC) -c $(CFLAGS) $(INCPATH) -o main.o m/main.c

####### Install

install:   FORCE

uninstall:   FORCE

FORCE:

其中Makefile中的紅色部分可以刪除,最後的Makefile檔案如下:

#############################################################################
# Makefile for building: test3
# Generated by qmake (2.01a) (Qt 4.6.2) on: Thu Mar 7 09:38:41 2013
# Project:  test3.pro
# Template: app
# Command: /usr/bin/qmake -unix -o Makefile test3.pro
#############################################################################

####### Compiler, tools and options

CC            = gcc
CXX           = g++

CFLAGS        = -pipe -O2 -Wall -W -D_REENTRANT $(DEFINES)
CXXFLAGS      = -pipe -O2 -Wall -W -D_REENTRANT $(DEFINES)
INCPATH       = -I. -Ia -Ib -I.
LINK          = g++
LFLAGS        = -Wl,-O1
LIBS          = $(SUBLIBS)  -L/usr/lib -lpthread
AR            = ar cqs
RANLIB        =
QMAKE         = /usr/bin/qmake
TAR           = tar -cf
COMPRESS      = gzip -9f
COPY          = cp -f
SED           = sed
COPY_FILE     = $(COPY)
COPY_DIR      = $(COPY) -r
STRIP         = strip
INSTALL_FILE  = install -m 644 -p
INSTALL_DIR   = $(COPY_DIR)
INSTALL_PROGRAM = install -m 755 -p
DEL_FILE      = rm -f
SYMLINK       = ln -f -s
DEL_DIR       = rmdir
MOVE          = mv -f
CHK_DIR_EXISTS= test -d
MKDIR         = mkdir -p

####### Output directory

OBJECTS_DIR   = ./

####### Files

SOURCES       = a/a.c \
        b/b.c \
        m/main.c
OBJECTS       = a.o \
        b.o \
        main.o
QMAKE_TARGET  = test3
DESTDIR       =
TARGET        = test3

first: all
####### Implicit rules

.SUFFIXES: .o .c .cpp .cc .cxx .C

.cpp.o:
    $(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<"

.cc.o:
    $(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<"

.cxx.o:
    $(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<"

.C.o:
    $(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<"

.c.o:
    $(CC) -c $(CFLAGS) $(INCPATH) -o "$@" "$<"

####### Build rules

all: $(TARGET)

$(TARGET):  $(OBJECTS)  
    $(LINK) $(LFLAGS) -o $(TARGET) $(OBJECTS) $(OBJCOMP) $(LIBS)

clean:
    -$(DEL_FILE) $(OBJECTS)
    -$(DEL_FILE) *~ core *.core

####### Sub-libraries

distclean: clean
    -$(DEL_FILE) $(TARGET)
    -$(DEL_FILE)

####### Compile

a.o: a/a.c a/a.h
    $(CC) -c $(CFLAGS) $(INCPATH) -o a.o a/a.c

b.o: b/b.c b/b.h
    $(CC) -c $(CFLAGS) $(INCPATH) -o b.o b/b.c

main.o: m/main.c a/a.h \
        b/b.h
    $(CC) -c $(CFLAGS) $(INCPATH) -o main.o m/main.c

####### Install

install:   FORCE

uninstall:   FORCE

FORCE:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.