目录
qmake的作用
1.生成Makefile文件;
2.生成moc和uic文件;
3.用vs打开Qt时无需要改变pro文件就能能够生成项目;
在Linux中qmake的主要目的是简化程序员编写Makefile;
简单的小例子
手写一个简单的pro文件,用再用qmake下;
首先写一个小例子:
包含4个文件:
hello.h、hello.cpp、main.cpp、hello.pro
源码如下:
hello.h
#include <iostream>
using namespace std;
class Hello{
public:
Hello();
void print();
};
hello.cpp
#include "hello.h"
Hello::Hello(){
cout << "Hello construction called!" << endl;
}
void Hello::print(){
cout << "Hello::print called!" << endl;
}
main.cpp
#include "hello.h"
int main(){
Hello *hello = new Hello;
hello->print();
delete hello;
return 0;
}
hello.pro
TARGET = helloworld
CONFIG += debug
HEADERS += hello.h
SOURCES += hello.cpp \
main.cpp
TARGET一般与pro文件同名,但后缀名应平台而异,如exe是Windows平台上的,无后缀是Unix上的,如果想设置一个其他的名字,可以在此设置其他名称;
使用这条命令生成Makefile
qmake -o Makefile hello.pro
Linux的操作如下图所示:
随后使用make即可
在VS中想上次vc的文件要使用下面这条命令:
qmake -tp vc hello.pro -spec win32-msvc2012
Windows的操作如下图:
设置好,vs,如果没输出,注意设置控制台输入,如下图所示:
运行截图如下:
跨平台的例子
存在下面几个文件:
hello.cpp、hello.h、hellounix.cpp、hellounix.h、hellowin.cpp、hellowin.h、main.cpp、hello.pro
具体内容如下:
hello.h
#include <iostream>
using namespace std;
class Hello{
public:
Hello();
void print();
};
hello.cpp
#include "hello.h"
Hello::Hello(){
cout << "Hello construction called!" << endl;
}
void Hello::print(){
cout << "Hello::print called!" << endl;
}
hellounix.h
#include <iostream>
using namespace std;
class UnixHello{
public:
UnixHello();
void print();
};
hellounix.cpp
#include "hellounix.h"
UnixHello::UnixHello(){
cout << "UnixHello construction called!" << endl;
}
void UnixHello::print(){
cout << "UnixHello construction called!" << endl;
}
hellowin.h
#include <iostream>
using namespace std;
class WinHello{
public:
WinHello();
void print();
};
hellowin.cpp
#include "hellowin.h"
WinHello::WinHello(){
cout << "WinHello contraction is called!" << endl;
}
void WinHello::print(){
cout << "WinHello print() called!" << endl;
}
main.cpp
#include "hello.h"
#include <QApplication>
#ifdef Q_OS_WIN32
#include "hellowin.h"
#else
#include "hellounix.h"
#endif
int main(){
Hello *hello = new Hello;
hello->print();
delete hello;
#ifdef Q_OS_WIN32
WinHello *winHello = new WinHello;
winHello->print();
#else
UnixHello *unixHello = new UnixHello;
unixHello->print();
#endif
return 0;
}
hello.pro
TARGET = helloworld
CONFIG += debug
HEADERS += hello.h
SOURCES += hello.cpp \
main.cpp
win32 {
HEADERS += hellowin.h
SOURCES += hellowin.cpp
}
unix {
HEADERS += hellounix.h
SOURCES += hellounix.cpp
}
在Linux上运行截图如下:
在Windows上运行截图如下:
如果想调试程序可以使用CONFIG这个变量,标记程序为debug版本
如下所示:
源码如下:
CONFIG += debug
HEADERS += hello.h
SOURCES += hello.cpp
SOURCES += main.cpp
如果想调试程序可以使用CONFIG这个变量,标记程序为debug版本
如下所示:
win32 {
SOURCES += hellowin.cpp
}
使用win32与unix来决定跨平台的特性【前提是各个平台的代码分离】
如下所示:
CONFIG += debug
HEADERS += hello.h
SOURCES += hello.cpp
SOURCES += main.cpp
win32 {
SOURCES += hellowin.cpp
}
unix {
SOURCES += hellounix.cpp
}
exists命令,当缺少文件的时候会停止qmake
win32 {
debug {
CONFIG += console
}
}
后面这个是在Windows上,想调出控制台的时候(qDebug能打印),需要做的事情