利用qmake生成Makefile文件

时间:2022-07-05 15:24:42

在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: