在Windows下使用Makefile(附例子)

用mingw32-make就行了,语法跟GNU make基本上是一样的,只是要针对windows写命令,比如linux下的rm指令(删除文件)在windows下需要换成del指令

为什么不用Cygwin?——老爱报些莫名其妙的错误。下面举个例子

下面用LIB_ZTHREAD代指Windows下的F:/libs/zthread_win32.a或者Ubuntu下的/home/admin/libs/zthread.a

用HEADER_ZTHREAD代指Windows下的F:/libs/ZThread-2.3.2/include或者Ubuntu下的/home/admin/libs/ZThread-2.3.2/include

点此下载zthread_win32.a

点此下载zthread.a

文件结构:所有的.cpp文件.h文件Makefile都在一个文件夹里,假设其目录为TEST_DIR

源代码:

main.cpp

1 #include "Test.h"
2
3 using namespace std;
4
5 int main()
6 {
7     Test::testLiftOff();
8     return 0;
9 }

Test.h

 1 #ifndef TEST_H
 2 #define TEST_H
 3
 4
 5 class Test
 6 {
 7     public:
 8         static void testLiftOff();
 9
10     private:
11         Test();
12         ~Test();
13 };
14
15 #endif // TEST_H

Test.cpp

 1 #include "Test.h"
 2
 3 #include "LiftOff.h"
 4
 5 #include <zthread/Thread.h>
 6
 7 #include <iostream>       // std::cout
 8
 9 void Test::testLiftOff()
10 {
11     using namespace ZThread;
12
13     try {
14         for (int i = 0; i < 5; ++i)
15         {
16             Thread th(new LiftOff(10, i));
17         }
18         std::cout << "waiting for lift off" << std::endl;
19     } catch (Synchronization_Exception &e) {
20         std::cerr << e.what() << std::endl;
21     }
22 }
23
24 Test::Test()
25 {
26     //ctor
27 }
28
29 Test::~Test()
30 {
31     //dtor
32 }

LiftOff.h

 1 #ifndef LIFTOFF_H
 2 #define LIFTOFF_H
 3
 4 #include <zthread/Runnable.h>
 5
 6 class LiftOff : public ZThread::Runnable
 7 {
 8     public:
 9         LiftOff(int countDown_, int id_);
10         ~LiftOff();
11         void run();
12     private:
13         int countDown;
14         int id;
15 };
16
17 #endif // LIFTOFF_H

LiftOff.cpp

 1 #include "LiftOff.h"
 2
 3 #include <iostream>
 4
 5 using namespace std;
 6
 7 LiftOff::LiftOff(int countDown_, int id_)
 8     :countDown(countDown_), id(id_)
 9 {
10     // do nothing
11 }
12
13 LiftOff::~LiftOff()
14 {
15     cout << "LiftOff" << id << " destroyed" << endl;
16 }
17
18 void LiftOff::run()
19 {
20     while (countDown--)
21         cout << id << " count: " << countDown << endl;
22     cout << id << "LiftOff!" << endl;
23 }

1. Ubuntu (linux) + GNU make

Makefile

 1 # ZTHREAD_A the static link library file of ZThread
 2 ZTHREAD_A = /home/admin/libs/zthread.a 3 # ZTHREAD_H is the directory that has all the header
 4 # files of the ZThread library
 5 ZTHREAD_H = /home/admin/libs/ZThread-2.3.2/include 6
 7 test.exe: main.o Test.o LiftOff.o
 8     g++ -o test.exe main.o Test.o LiftOff.o -s $(ZTHREAD_A) -lpthread # -lpthread is necessary to link pthread library, which is not part of the default library in Ubuntu, ZThread need pthread support
 9 main.o: main.cpp
10     g++ -c main.cpp -o main.o
11 # ‘-I‘ specifies the header search directory
12 Test.o: Test.cpp Test.h
13     g++ -I $(ZTHREAD_H) -c Test.cpp -o Test.o
14 LiftOff.o: LiftOff.cpp LiftOff.h
15     g++ -I $(ZTHREAD_H) -c LiftOff.cpp -o LiftOff.o
16
17 # PHONY means ‘clean‘ is a fake target
18 # use ‘make clean‘ to remove all .o files
19 # before rebuilding
20 .PHONY: clean
21 clean:
22     -rm test # ‘-‘ means continue execute next command even if something goes wrong
23     -rm *.o

make clean

make -f Makefile

运行成功

2. Windows + mingw32-make

Makefile

 1 # ZTHREAD_A the static link library file of ZThread
 2 ZTHREAD_A = F:/libs/ZThread-2.3.2/lib/zthread_win32.a
 3 # ZTHREAD_H is the directory that has all the header
 4 # files of the ZThread library
 5 ZTHREAD_H = F:/libs/ZThread-2.3.2/include
 6
 7 test.exe: main.o Test.o LiftOff.o
 8     g++ -o test.exe main.o Test.o LiftOff.o -s $(ZTHREAD_A)
 9 main.o: main.cpp
10     g++ -c main.cpp -o main.o
11 # ‘-I‘ specifies the header search directory
12 Test.o: Test.cpp Test.h
13     g++ -I $(ZTHREAD_H) -c Test.cpp -o Test.o
14 LiftOff.o: LiftOff.cpp LiftOff.h
15     g++ -I $(ZTHREAD_H) -c LiftOff.cpp -o LiftOff.o
16
17 # PHONY means ‘clean‘ is a fake target
18 # use ‘make clean‘ to remove all .o files
19 # before rebuilding
20 # ‘-‘ means continue execute next command even if something goes wrong with this command
21 .PHONY: clean
22 clean:
23     -del test.exe
24     -del *.o

mingw32-make clean

mingw32-make -f Makefile

运行成功

3. Windows + Cygwin

Makefile

 1 # ZTHREAD_A the static link library file of ZThread
 2 ZTHREAD_A = F:/libs/ZThread-2.3.2/lib/zthread_win32.a
 3 # ZTHREAD_H is the directory that has all the header
 4 # files of the ZThread library
 5 ZTHREAD_H = F:/libs/ZThread-2.3.2/include
 6
 7 test.exe: main.o Test.o LiftOff.o
 8     g++ -o test.exe main.o Test.o LiftOff.o -s $(ZTHREAD_A)
 9 main.o: main.cpp
10     g++ -c main.cpp -o main.o
11 # ‘-I‘ specifies the header search directory
12 Test.o: Test.cpp Test.h
13     g++ -I $(ZTHREAD_H) -c Test.cpp -o Test.o
14 LiftOff.o: LiftOff.cpp LiftOff.h
15     g++ -I $(ZTHREAD_H) -c LiftOff.cpp -o LiftOff.o
16
17 # PHONY means ‘clean‘ is a fake target
18 # use ‘make clean‘ to remove all .o files
19 # before rebuilding
20 # ‘-‘ means continue execute next command even if something goes wrong with this command
21 .PHONY: clean
22 clean:
23     -rm test.exe
24     -rm *.o

make clean没问题

make报错(貌似是找不到__assert的实现,真心无语),报错的详细信息见这篇随笔

时间: 2024-09-30 06:43:28

在Windows下使用Makefile(附例子)的相关文章

windows 下使用makefile(二)---- 编译

之所以需要另外设定" 执行时需要="" mspdb60.dll,而它被安装於="" c:\msdev\common\msdev98\bin="" 之中.<="" p><p>如果你写的程式不只是单纯的="" 程式,还用到了="" mfc,一样可以在="" console="" mode="" 下编

windows 下使用makefile(一)----部署

makefile在windows之外的系统中广泛使用,大多数程序员依赖于vs的IDE; 掌握使用makefile,对于自动化构建很有用 vs使用nmake.exe作为构建的工具,有别于linux的make nmake.exe在vs的安装目录下, vs2005 的默认目录是:C:\Program Files\Microsoft Visual Studio 8\VC\bin 或者 C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin (通常情

windows下socket编程小例子

服务端 首先引进头文件winsock2.h和库文件ws2_32.lib(开发环境为vs2015) 加载套接字库和创建套接字 绑定套接字到一个IP地址和端口上 监听客户端发来的连接请求 接收或发送信息 关闭套接字,套接字库 客户端 首先引进头文件winsock2.h和库文件ws2_32.lib(开发环境为vs2015) 加载套接字库,创建套接字对象 向服务端发出连接请求 收发信息 关闭套接字和套接字库 注释 WSAStartup结构体中主要包含了系统所支持的Winsock版本信息 WSAstart

Java向上转型和向下转型(附详细例子)

                                            Java向上转型和向下转型(附详细例子) 熬夜整理的关于Java向上和向下转型的例子,非常的通俗易懂哦~~~~ 一.向上转型 package com.sheepmu; class Animal { public void eat() { System.out.println("父类的 eating..."); } } class Bird extends Animal { @Override pub

Windows下搭建go语言开发环境 以及 开发IDE (附下载链接)

1.下载 并且 安装 Go安装包 =========================================================== 在CSDN上传了我的版本,供大家下载: =========================================================== go1.2.windows-386.msi : http://download.csdn.net/detail/shuideyidi/7718563 go1.2.1.windows-am

Windows下C语言的Socket编程例子(TCP和UDP)

一.  <TCP> server端: 1 #include "stdafx.h" 2 #include <stdio.h> 3 #include <winsock2.h> 4 5 #pragma comment(lib,"ws2_32.lib") 6 7 int main(int argc, char* argv[]) 8 { 9 //初始化WSA 10 WORD sockVersion = MAKEWORD(2,2); 11 W

Windows下.svn文件夹的最简易删除方法(附linux)

如果想删除Windows下的.svn文件夹,通过手动删除的渠道是最麻烦的,因为每个文件夹下面都存在这样的文件.下面是一个好办法:建立一个文本文件,取名为kill-svn-folders.reg(扩展名由txt改为reg),内容如下: 复制代码 代码如下: Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Folder\shell\DeleteSVN] @="Delete SVN Folders&qu

在Windows下用Eclipse+CDT+MinGW搭建C++开发平台

本文提供了在Windows下用Eclipse+CDT+MinGW搭建C / C++开发平台的方法, 测试平台为Windows XP Sp2 CHS. 以下软件均为Windows平台下的版本. 1.安装JDK,目的是为了Eclipse的运行.目前版本是jdk-1_5_0_06-windows-i586-p.exe ,下载地址http://java.sun.com/javase/downloads/index.jsp.仅安装JDK即可.假设安装路径为D:\java\JDK.配置系统环境变量(右键点击

C++开发安卓、windows下搭建Android NDK开发环境

1. NDK(Native Development Kit) 1.1 NDK简介 Android NDK是一套允许开发人员使用本地代码(如C/C++)进行Android APP功能开发的工具,通过这个工具,我们可以把用C/C++代码编译成可以直接运行在Android平台上的本地代码,这些本地代码以动态链接库( *.so )的形式存在,也正因为这样,我们可以通过复用这些动态链接库从而复用本地代码. 那么,通过NDK这个开发工具包,那么我们是否可以将一个APK完全使用C/C++来编写呢? 答案是不可