Windows使用nmake和Makefile编译c++教程
今天在本地看到一个lsd\_1.6的源文件,不知道什么时候看LSD时下载的,里面只有一个Makefile和源文件。
想到在Linux下可以只用一个make命令就可以得到可执行程序,在Windows下是不是以可以一个命令就得到EXE程序呢,想到了nmake。
原来的Makefile是这样写的:
1 # -----------------------------------------------------------------------------
2 #
3 # LSD - Line Segment Detector on digital images
4 #
5 # Copyright (c) 2007-2011 rafael grompone von gioi <[email protected]>
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as
9 # published by the Free Software Foundation, either version 3 of the
10 # License, or (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16 #
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #
20 # -----------------------------------------------------------------------------
21
22 all: lsd lsd_call_example
23
24 lsd: lsd.c lsd.h lsd_cmd.c
25 cc -O3 -o lsd lsd_cmd.c lsd.c -lm
26
27 lsd_call_example: lsd.c lsd.h lsd_call_example.c
28 cc -o lsd_call_example lsd_call_example.c lsd.c -lm
29
30 doc: lsd.c lsd.h doxygen.config
31 doxygen doxygen.config
32
33 clean:
34 rm -f lsd lsd_call_example
35
36 cleandoc:
37 rm -rf doc
38
39 # -----------------------------------------------------------------------------
View Code
由于自己也不怎么会使用nmake就抱着试试看的态度试了一把。
去windows下打开这个:
然后 执行下面的命令,进入到lsd\_1.6目录下:
cd /d D:\GitHub\lsd_1.6
进入目录后执行编译命令:
nmake -f Makefile
接着就撞见错误了:
1 D:\GitHub\lsd_1.6>nmake -f Makefile
2
3 Microsoft (R) Program Maintenance Utility Version 14.00.24210.0
4 Copyright (C) Microsoft Corporation. All rights reserved.
5
6 cc -O3 -o lsd lsd_cmd.c lsd.c -lm
7 'cc' 不是内部或外部命令,也不是可运行的程序
8 或批处理文件。
9 NMAKE : fatal error U1077: 'cc' : return code '0x1'
10 Stop.
原来是nmake不认识cc,果然不是一家人,联想到vs使用的编译命令是cl,果断把cc替换成cl,然后新建了一个Makefile1,只把原来Makefile里面的cc替换成cl.
Makefile1内容如下:
1 # -----------------------------------------------------------------------------
2 #
3 # LSD - Line Segment Detector on digital images
4 #
5 # Copyright (c) 2007-2011 rafael grompone von gioi <[email protected]>
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as
9 # published by the Free Software Foundation, either version 3 of the
10 # License, or (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16 #
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #
20 # -----------------------------------------------------------------------------
21
22 all: lsd lsd_call_example
23
24 lsd: lsd.c lsd.h lsd_cmd.c
25 cl -O3 -o lsd lsd_cmd.c lsd.c -lm
26
27 lsd_call_example: lsd.c lsd.h lsd_call_example.c
28 cl -o lsd_call_example lsd_call_example.c lsd.c -lm
29
30 doc: lsd.c lsd.h doxygen.config
31 doxygen doxygen.config
32
33 clean:
34 rm -f lsd lsd_call_example
35
36 cleandoc:
37 rm -rf doc
38
39 # -----------------------------------------------------------------------------
View Code
接着使用编译命令:
nmake -f Makefile1
就这样编译通过了:
1 D:\GitHub\lsd_1.6>nmake -f Makefile1
2
3 Microsoft (R) Program Maintenance Utility Version 14.00.24210.0
4 Copyright (C) Microsoft Corporation. All rights reserved.
5
6 cl -O3 -o lsd lsd_cmd.c lsd.c -lm
7 Microsoft (R) C/C++ Optimizing Compiler Version 19.00.24215.1 for x86
8 Copyright (C) Microsoft Corporation. All rights reserved.
9
10 cl : Command line warning D9035 : option 'o' has been deprecated and will be removed in a future release
11 cl : Command line warning D9002 : ignoring unknown option '-O3'
12 cl : Command line warning D9002 : ignoring unknown option '-lm'
13 lsd_cmd.c
14 lsd.c
15 Generating Code...
16 Microsoft (R) Incremental Linker Version 14.00.24215.1
17 Copyright (C) Microsoft Corporation. All rights reserved.
18
19 /out:lsd_cmd.exe
20 /out:lsd.exe
21 lsd_cmd.obj
22 lsd.obj
23 cl -o lsd_call_example lsd_call_example.c lsd.c -lm
24 Microsoft (R) C/C++ Optimizing Compiler Version 19.00.24215.1 for x86
25 Copyright (C) Microsoft Corporation. All rights reserved.
26
27 cl : Command line warning D9035 : option 'o' has been deprecated and will be removed in a future release
28 cl : Command line warning D9002 : ignoring unknown option '-lm'
29 lsd_call_example.c
30 lsd.c
31 Generating Code...
32 Microsoft (R) Incremental Linker Version 14.00.24215.1
33 Copyright (C) Microsoft Corporation. All rights reserved.
34
35 /out:lsd_call_example.exe
36 /out:lsd_call_example.exe
37 lsd_call_example.obj
38 lsd.obj
View Code
注意:其实makefile中应该还有其他需要修改的,但是简单编译一个exe已经实现了,至于其他需要修改的有时间再研究。