首页 > 代码库 > 在Windows下使用Makefile(附例子)

在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     //ctor27 }28 29 Test::~Test()30 {31     //dtor32 }

 

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 nothing11 }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.cpp10     g++ -c main.cpp -o main.o11 # ‘-I‘ specifies the header search directory12 Test.o: Test.cpp Test.h13     g++ -I $(ZTHREAD_H) -c Test.cpp -o Test.o14 LiftOff.o: LiftOff.cpp LiftOff.h15     g++ -I $(ZTHREAD_H) -c LiftOff.cpp -o LiftOff.o16 17 # PHONY means ‘clean‘ is a fake target18 # use ‘make clean‘ to remove all .o files19 # before rebuilding20 .PHONY: clean21 clean:22     -rm test # ‘-‘ means continue execute next command even if something goes wrong23     -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.cpp10     g++ -c main.cpp -o main.o11 # ‘-I‘ specifies the header search directory12 Test.o: Test.cpp Test.h13     g++ -I $(ZTHREAD_H) -c Test.cpp -o Test.o14 LiftOff.o: LiftOff.cpp LiftOff.h15     g++ -I $(ZTHREAD_H) -c LiftOff.cpp -o LiftOff.o16 17 # PHONY means ‘clean‘ is a fake target18 # use ‘make clean‘ to remove all .o files19 # before rebuilding20 # ‘-‘ means continue execute next command even if something goes wrong with this command21 .PHONY: clean22 clean:23     -del test.exe24     -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.cpp10     g++ -c main.cpp -o main.o11 # ‘-I‘ specifies the header search directory12 Test.o: Test.cpp Test.h13     g++ -I $(ZTHREAD_H) -c Test.cpp -o Test.o14 LiftOff.o: LiftOff.cpp LiftOff.h15     g++ -I $(ZTHREAD_H) -c LiftOff.cpp -o LiftOff.o16 17 # PHONY means ‘clean‘ is a fake target18 # use ‘make clean‘ to remove all .o files19 # before rebuilding20 # ‘-‘ means continue execute next command even if something goes wrong with this command 21 .PHONY: clean22 clean:23     -rm test.exe24     -rm *.o

make clean没问题

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

 

在Windows下使用Makefile(附例子)