`

Windows下Makefile

 
阅读更多

参考文档:http://www.cnblogs.com/repository/archive/2011/05/18/2050546.html

 

一、安装:

      MinGW-3.1.0-1.exe       Windows下的gcc,编译C语言的工具(下载MinGW-3.1.0-1.exe,然后双击安装到C:\MinGW)

      make.exe                     按照Makefile规则编译工程的工具(下载mingw32-make-3.80.0-3.exe,然后双击安装到C:\MinGW;在C:\MinGW\bin下,将mingw32-make.exe改名或复制改名为make.exe)

      将C:\MinGW\bin添加到环境变量path中

 

二、Makefile流程

 

1. Main.c

#include "stdio.h"
main(){
       func();
       printf("this is main\n");
       getch();
}

 

2. func.c

#include "stdio.h"
func(){
       printf("in func\n");
       getch();
}

 

3. Makefile(或者makefile)——这个文件名是必须的,因为make默认查找该文件

test:main.o func.o      
       gcc -o test main.o func.o      

func.o:func.c   
       gcc -c func.c      

main.o:main.c      
       gcc -c main.c

1行:生成test可执行文件,它的依赖于main.o func.o,也就是说生成test你得先生成它们,
2行:gcc前边必须是tab也就是7个空格,表示编译生成test

后边是依赖项的生成规则

 

 

然后运行compile.bat就可以生成test.exe文件了,同时会附加生成func.o和main,o两个中间代码的文件,类似于VC中的obj文件

 

4. compile.bat

make
cmd
 

三、gcc参数

      同VC,TC等编译器不同,GCC其实是可以很方便的在提示符下编译程序的。GCC在提示符下编译程序,并没有如同VC那样的冗长而晦涩的编译参数。相反,却有着比VC更灵活且简短的参数。
      不得不承认,不懂GCC编译参数的人,确实会损失一些GCC的强大功能。所以,我下面简单介绍一下GCC的一些基本编译参数。这里,我以C编译器为例。

编译二进制代码
$gcc -c yours.c -o yours.o
使用这段指令,GCC将会把yours.c编译成yours.o的二进制代码。其中,yours.o就类似于VC,TC中的.obj文档。

 
编译最简单的小程序
$gcc -o yours yours.c
通过这条指令,GCC将会把yours.c源代码编译成名为yours的可执行程序。当然,您也可以将yours.c改成我们刚才介绍的yours.o文件。这样,gcc将使用编译刚才编译好的二进制文档来链接程序。这里,格式的特点是,-o 后面是一串文件列表,第一个参数是所编译程序的文件名,从第二个开始,就是您编译和连接该可执行程序所需要的二进制文档或者源代码。
 
编译时将自己的头文件目录设为默认头文件目录
$gcc -I”Your_Include_Files_Document_Path” -c yours.c -o yours.o
这条指令中的-I参数将会把Your_Include_Files_Document_Path添加到你默认的头文件目录中。这样您将可以使用 #include <your_include.h>来导入头文件。

编译时使用自己的静态库存放目录
$gcc -L”Your_Lib_Files_Document_Path” -o yours yours.o
这条指令将会让GCC在连接时除了在默认Lib存放目录中搜索指定的静态库以外,还会在Your_Lib_Files_Document_Path中搜索。

编译时使用静态连接库
$gcc -lyour_lib -o yours yours.o
这条指令将会让GCC在连接时把 libyour_lib.a中您所用到的函数连接到可执行程序中。此处注意,GCC所使用的静态连接库是lib*.a格式的。在连接时,只且仅需要提供*的内容就可以了。

编译时使用优化
$gcc -O2 -c yours.c -o yours.o
使用优化方式编译程序,其中除了-O2以外,还有-O3 -O1等等。他们代表不同的优化等级。最常用的,是-O2优化。当然,还有针对特殊CPU的优化,这里就不介绍了。

编译时显示所有错误和警告信息
$gcc -Wall -c yours.c -o yours.o
GCC在默认情况下,将对一些如变量申请未使用这样的问题或者申请了没有给予初始值的问题忽略。但是,如果使用了-Wall参数,编辑器将列出所有的警告信息。这样,您就可以知道您的代码中有多少可能会在其他操作系统下出错的地方了。(用这个指令看看你的代码有多少地方写的不怎么合适。)

编译连接时,加入调试代码
$gcc -g -o yours yours.c
正如同VC有debug编译模式一样,GCC也有debug模式。添加了-g 参数编译的可执行程序比普通程序略为大一些,其中添加了一些调试代码。这些代码将被gdb所支持。

连接时缩小代码体积
$gcc -s -o yours yours.o
这个参数,似乎我没有在Unix环境下看到过。也不知道具体什么作用。因为有人说Visual-MinGW生成的代码小,于是研究了一下她的编译参数,发现release模式的编译参数就加了这一项。貌似编译后的代码的确缩小了很多。

获得帮助
$gcc --help
这条指令从意思上就能看出,获得gcc的帮助信息。如果您有什么特殊需要,也许这个指令能帮上点小忙。

 

四、Makefile实战——使用Newman's Fast Algorithm

      Newman's Fast Algorithm是我在试验中用到的一个“Community Detection”的算法,作者慷慨地以c工程的形式提供了代码,且用到了Makefile工具。Newman先生的主页在此 http://www-personal.umich.edu/~mejn/ , 仔细看发现有一条“Information and code for the fast community structure algorithm is here .” 点击进入如下页面可以得到Newman's Fast Algorithm代码:

      http://cs.unm.edu/~aaron/research/fastmodularity.htm

      为了防止这个页面失效,我将其完整的截取如下——按照下面的步骤就可以使用算法的代码了。但很不幸,在我执行make命令时出现了以下错误:

      原因是make程序将其中一些标记误认为是文件了——使用.phony方式就可以解决问题。具体方法是在Makefile中加入.phony,截图如下:

 

 

"Fast Modularity" Community Structure Inference Algorithm

This page documents and supports the fast modularity maximization algorithm I developed jointly with Mark Newman and Cristopher Moore . This algorithm is being widely used in the community of complex network researchers, and was originally designed for the express purpose of analyzing the community structure of extremely large networks (i.e., hundreds of thousand or millions of vertices). The original version worked only with unweighted, undirected networks. I've recently posted a version that works on weighted, undirected networks.

Update February 2007 : Please see my recent blog entry about this algorithm.

Update May 2007 : See also the igraph library , a pretty comprehensive library of network utility functions (generation and analysis), including an implementation of the fast modularity algorithm described here (along with a few other nice clustering heuristics).

Update October 2008 : I've finally gotten around to posting a version that works with weighted networks, which is available here . This version wants a .wpairs file as input, which is an edge list with integer weights, e.g., "54\t91\t3\n" would be an edge with weight 3. Otherwise, it should work just the same as the unweighted version.

Update December 2009 : If you're going to use this algorithm, you should also read this paper, which describes the performance (pro and con) of modularity maximization in practical contexts.

B. H. Good, Y.-A. de Montjoye and A. Clauset, "The performance of modularity maximization in practical contexts ."
Phys. Rev. E 81 , 046106 (2010).

Journal Reference
A. Clauset, M.E.J. Newman and C. Moore, "Finding community structure in very large networks ."
Phys. Rev. E 70 , 066111 (2004).

My request
For the past year, I have passed out the code personally and have asked each person who uses it to please send me a pre-print of any papers they produce with the code. I ask the same of each of you, as it's nice to know what projects this algorithm is being used in. You may email me directly at aaron@xx.xxx.edu with the rest of the address being obvious.

 

Get the code
The code is available in both .tgz and .zip formats. It contains the following files (with corresponding descriptions):

gpl.txt GPL (version 2)
Makefile makefile for compiling the executable
fastcommunity_mh.cc main algorithm file - this has the heart of the algorithm described in the paper.
maxheap.h max-heap datastructure for storing the dQ values (linked to vektor data structure)
vektor.h sparse matrix row datastructure for storing dQ values (linked to maxheap data structure)


Update October 2008 : A version that works with weighted networks is available here . This version wants a .wpairs file as input, which is an edge list with integer weights, e.g., "54\t91\t3\n" would be an edge with weight 3. Otherwise, it should work just the same as the unweighted version.

 

Compiling
The Makefile provided should be sufficient to compile the executable (built in ANSI C/C++ with platform independent code).

 

Input file requirements
The program was written to handle networks in the form of a flat text file containing edge adjacencies. I call this file format a .pairs file. Specifically, the network topology must conform to the following requirements:
• .pairs is a list of tab-delimited pairs of numeric indices, e.g., "54\t91\n"
• the network described is a SINGLE COMPONENT
• there are NO SELF-LOOPS or MULTI-EDGES in the file
• the MINIMUM NODE ID = 0 in the input file
• the MAXIMUM NODE ID can be anything, but the program will use less memory if nodes are labeled sequentially

Obviously, this file format is a bit peculiar, but it was sufficient for the demonstration that the algorithm performs as promised. You are free to alter the file import function readInputFile() to fit your needs.

An example input file, for Zachary's karate club network is here .

 

Running the program
The Makefile provided should be sufficient to compile the executable (built in ANSI C/C++ with platform independent code). This usage information can be retrieved by running the executable with no arguments:

-f <filename> give the target .pairs file to be processed
-l <text> the text label for this run; used to build output filenames
-t <int> timer period for reporting progress of file input to screen
-s calculate and record the support of the dQ matrix
-v --v ---v differing levels of screen output verbosity
-c <int> record the aglomerated network at step <int> (typically, this is the step location at which the modularity is known to be maximum)

(Please see the notes in the .cc file for the most up-to-date version of this information.) The typical usage will be to first create the .pairs file containing your network, to run the program like

./fastcommunity_mh -f myNetwork.pairs -l firstRun

and then consult the file outputs as described below. If you want to then examine the communities that have been built by the algorithm, you would run the algorithm a second time like so

./fastcommunity_mh -f myNetwork.pairs -l secondRun -c X

where X is the integer value for the maximum modularity that is reported in the .info file. Again, this could probably be automated, either by modifying the code, or wrapping it all in another script.

 

File outputs
Running the program on some input network will produce a set of files, each with a common naming convention, where the file type is encoded by the suffix. This information can also be retrieved by running the executable with the argument -files .

Mandatory file outputs

.info Various information about the program's running. Includes a listing of the files it generates, number of vertices and edges processed, the maximum modularity found and the corresponding step (you can re-run the program with this value in the -c argument to have it output the contents of the clusters, etc. when it reaches that step again (not the most efficient solution, but it works)), start/stop time, and when -c is used, it records some information about the distribution of cluster sizes.
.joins The dendrogram and modularity information from the algorithm. The file format is tab-delimited columns of data, where the columns are:
1. the community index which absorbs
2. the community index which was absorbed
3. the modularity value Q after the join
4. the time step of the join

Optional file outputs (generated at step t=C when the -c C argument is used:

.wpairs The connectivity of the clustered graph in a .wpairs file format (i.e., weighted edges). The edge weights should be the dQ values associated with that clustered edge at time C. From this format, it's easy to convert into another for visualization (e.g., pajek's .net format).
.hist The size distribution of the clusters.
.groups A list of each group and the names of the vertices which compose it (this is particularly useful for verifying that the clustering makes sense - tedious but important).

 

Bugs and incompleteness
All of the features documented on this page work as advertised (although, if you find any bugs, please let me know). There are some other, undocumented features that may not work fully (if you read through the code, you may spot some of these), so use them at your own risk.

 

Code maintenence
I am not actively working on this code since this project complete with its publication in 2004. However, I'll try to give as much verbal support to interested users as is reasonable.

 

  • 大小: 2.9 KB
  • 大小: 18.8 KB
分享到:
评论

相关推荐

    Windows 下的 Makefile 编写(三)推理规则

    ### Windows 下的 Makefile 编写(三):推理规则详解 #### 引言 在Windows环境下编写Makefile,推理规则是实现自动化构建的核心技术之一。掌握这一技能可以使Makefile的编写过程更加简洁高效,同时增强其维护性。...

    Windows 下的 Makefile 编写(一)Makefile的基本规则

    ### Windows 下的 Makefile 编写(一)Makefile 的基本规则 #### 一、Makefile 概述 Makefile 是一种广泛应用于软件构建过程中的自动化脚本文件,主要用于描述项目构建过程中的依赖关系和构建步骤。对于习惯于集成...

    Win平台下Makefile使用说明

    学习并熟练掌握Makefile的使用,可以帮助你在Windows环境下更高效地管理项目构建流程,减少手动编译的工作量,提高开发效率。通过阅读和实践这些资料,你将能够编写出适应Windows平台的Makefile,并利用其强大的功能...

    Makefile自动构建方法

    在Linux平台下,Makefile是一种非常重要的自动化构建工具,它能够帮助开发者高效地管理项目构建过程。本文档将详细介绍如何使用Makefile进行自动构建,并通过一个简单的“Hello World”示例来展示整个流程。此方法...

    blas-lapack在windows下的ifort-makefile

    标题 "blas-lapack在windows下的ifort-makefile" 指的是如何在Windows操作系统上使用Intel Visual Fortran编译器来编译和链接BLAS(基础线性代数子程序)和LAPACK(线性代数包)库。这两个库是科学计算中的重要工具...

    windows下的makefile教程.doc

    【Windows下的Makefile教程】 Makefile是构建程序的重要工具,特别是在Unix/Linux环境下广泛使用,但也可以在Windows系统中配合Microsoft的NMAKE工具进行程序构建。NMAKE是Microsoft Program Maintenance Utility的...

    unix环境下makefile规则

    在Unix环境下,C语言的开发过程中,`make`工具和`makefile`是不可或缺的组成部分。`make`是一个自动化构建工具,它的主要目的是避免对已编译过的代码进行不必要的重复编译,从而提高效率和节省资源。通过使用`make`...

    跟我一起写makefile

    在Windows环境下,虽然IDE如Visual Studio通常会自动处理这些任务,但理解Makefile仍然是专业程序员的基本技能。 【程序的编译和链接】 在编译程序时,Makefile主要关注两个阶段:编译和链接。编译阶段将源代码...

    Makefile教程-经典

    或许很多Winodws的程序员都不知道这个东西,因为那些Windows的IDE都为你做了这个工作,但我觉得要作一个好的和professional的程序员,makefile还是要懂。这就好像现在有这么多的HTML的编辑器,但如果你想成为一个...

    windows平台 makefile.exe,x86/x64通用

    "Windows平台的`makefile.exe`,x86/x64通用"这一标题揭示了关键信息:这是一个适用于Windows操作系统的程序,它是一个可执行文件(`.exe`),并且支持两种不同的处理器架构——32位(x86)和64位(x64)。`makefile...

    在Windows下安装GTK环境后,怎样编译GTK源文件。写Makefile文件太麻烦了不是嘛!!

    在Windows下安装GTK环境后,怎样编译GTK源文件。写Makefile文件太麻烦了不是嘛!!这里有一个MS-DOS Batch File,很好用的。

    Windows 下的 Makefile 编写(二)宏和预处理的简单示例

    在Windows环境下,理解并熟练掌握Makefile的编写,尤其是宏和预处理的使用,对于提高开发效率、减少错误以及优化项目管理流程具有重要意义。 ### 宏在Makefile中的应用 宏在Makefile中主要用于存储常量或表达式,...

    win平台automake自动生成Makefile工具(附源代码)

    自己写的简易的windows平台的自动生成Makefile工具,不需要写配置文件,自动生成,支持一般的控制台项目,可以作为控制台程序开发的小工具,目前正在开发中,当前版本1.2.2。使用方法:在你的工程目录下运行automake...

    codeblocks工程转makefile

    然而,有些情况下,开发者可能希望脱离IDE,直接使用命令行工具如`make`来编译项目,这时就需要将CodeBlocks的工程文件转换为`makefile`。`makefile`是一个文本文件,它包含了构建项目的规则和指令,使得编译过程...

    Linux教程之MakeFile教程

    或许很多Winodws的程序员都不知道这个东西,因为那些Windows的IDE都为你做了这个工作,但我觉得要作一个好的和professional的程序员,makefile还是要懂。这就好像现在有这么多的HTML的编辑器,但如果你想成为一个...

    Makefile实战.pdf

    在学习 Makefile 之前,需要准备一个 Linux 机器或在 Windows 上安装 Cgywin。为了验证 make 工具在环境中是否被正确安装了,可以运⾏“make -v”命令进⾏验证。 Makefile 的基本概念包括: * 规则(rule):...

    linux下makefile教程

    文章还强调,即使在Windows系统下,专业的程序员也应该了解Makefile的重要性。尽管Windows的IDE(集成开发环境)为用户隐藏了Makefile的复杂性,但是能够编写Makefile,依然被认为是专业程序员的一个重要能力。在...

    Makefile教程,Makefile快速学习资料

    由于Windows系统中大多集成开发环境(IDE)已经内置了编译工具,使得Windows程序员可能不熟悉Makefile。但是,对于专业人士来说,了解Makefile是十分必要的。 描述中强调了Makefile编写规则的重要性,指出其对于...

    关于C语言的MakeFile.rar_C语言_EFI_make_makefile_makefile for windows

    在EFI环境和Windows下,Makefile的正确配置能够帮助开发者轻松应对复杂的编译需求,从而更好地实现C语言程序的构建。通过深入学习和实践,你可以编写出更高效、更易于维护的Makefile,提升你的编程技能。

    Makefile 的编写指导

    Makefile 通常用于 Unix/Linux 环境下的软件构建过程中,但它同样可以在 Windows 等其他操作系统中使用相应的工具来实现类似的功能。 #### Makefile 的重要性 - **提高效率**:通过 Makefile 自动化编译流程,开发...

Global site tag (gtag.js) - Google Analytics