您的位置:首页 > 编程语言 > C语言/C++

Window下配置C++编译环境、makefile、简略make-clean制作--Sublime Text 3

2016-12-18 23:09 633 查看

一、简介

1、sublime Text 3是一个非常强大的文本编辑器,可以配合上Mingw制作一个学习用的小型IDE,如果要开发建议还是使用VS较好。



二、需要了解的知识

1、编译的过程和简要原理

2、Mingw编译器的cmd命令以及部分选项的用途

3、make的cmd命令、window下makefile的编写

可参考:

http://wiki.ubuntu.org.cn/index.php?title=%E8%B7%9F%E6%88%91%E4%B8%80%E8%B5%B7%E5%86%99Makefile&variant=zh-cn

4、Sublime Text 3编辑器build system的配置。



三、需要的工具

1、Sumblime Text 3

可在官网下载:

http://www.sublimetext.com/3

2、MinGW工具

可在官网下载:

http://www.mingw.org/

也可以在我百度云盘分享下载(在官网下载比较慢,建议从网盘下载):

https://pan.baidu.com/share/home?uk=2418397489#category/type=0



四、环境变量的配置

1、从我百度云盘下载的WinGW解压缩后,放在一个目录下,目录的路径不能有中文字体。



2、进入bin目录下,找到g++和mingw32-make执行文件。这两个文件均需要配置环境变量。g++是编译单个C++文件用的。mingw32-make是编译C++工程用的。



3、配置g++、mingw32-make环境变量(具体百度或者谷歌)



4、配置mingw头文件和库文件环境变量

(1)头文件在mingw的include目录下

(2)库文件在mingw的lib目录下





5、在cmd下用一下命令查看环境变量是否设置成功。



五、sublime-build编译系统配置文件

1、在sublime Text 3中,选择Perference–>Browse Package,在user目录里面创建一个文件,编译系统配置文件的后缀名为.sublime-build

2、我们在user目录下创建一个名为C++ MySingle File.sublime-build的文件。

在用sublime Text打开。加入如下内容:

{
"encoding": "utf-8",
"working_dir": "$file_path",
"shell_cmd": "g++ -Wall -std=c++11 \"$file_name\" -o \"$file_base_name\"",
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"selector": "source.c++,source.cpp",

"variants":
[
{
"name": "Complete File",
"shell_cmd": "g++ -Wall -std=c++0x \"$file_name\" -o \"$file_base_name\" && cmd /c \"${file_path}/${file_base_name}\""
},
{
"name": "CMD Run",
"shell_cmd": "g++ -Wall -std=c++0x \"$file\" -o \"$file_base_name\" && start cmd /c \"\"${file_path}/${file_base_name}\" & pause\""
}
]
}


3、我们在user目录下创建一个名为make.sublime-build的文件。该文件用于编译C++工程。

在用sublime Text打开。加入如下内容:

{
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c, source.c++, source.cpp",
"cmd": ["g++", "-c", "${file}", "-o", "${file_base_name}.o"],

"variants":
[
{
"name": "Run",
"cmd": ["cmd", "/c", "MinGW32-make","-f", "makefile", "&&", "start", "cmd", "/c", "${file_path}/${file_base_name} & pause"]
},

]
}


4、以下是我自己写的一个make-clean编译系统配置文件,作用与linux下的make-clean命令类似,用于删除编译过程中产生的.o以及可执行文件。如有不足,希望大家指出。

{
"encoding": "utf-8",
"working_dir": "$file_path",
"shell_cmd": "del  \"${file_base_name}.o\"",
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"selector": "source.c++,source.cpp",

"variants":
[
{
"name": "Make-clean",
"shell_cmd": " start cmd /c && del \"*.o\" \"*.exe\" "
}

]
}




六、编译快捷键的设置:

1、在sublime Text选择Preference–>Key Bindings,在Default (Windows).sublime-keymap处添加如下内容:

[
//C++ Compile and link all files, then run it by cmd
{ "keys": ["f5"], "command": "build", "args": {"variant": "Run"} },
//Single Cpp file Complete
{ "keys": ["f4"], "command": "build", "args": {"variant": "Complete File"} },
//Single Cpp file Run
{ "keys": ["ctrl+b"], "command": "build", "args": {"variant": "CMD Run"} },
//delete .o file and .exe file
{ "keys": ["ctrl+alt+c"], "command": "build", "args": {"variant": "Make-clean"} }
]


2、上面的内容中:

(1)F5为编译运行C++工程,有错误提示(错误提示在编译系统配置文件中的g++ -Wall中设置了)

(2)F4为编译C++单个文件,用于检查是否有错误

(3)Ctrl+Alt+c为删除C++工程编译过程中产生的.o文件以及.exe可执行执行文件。





七、window下makefile简略的编写

1、例:makefile文件名没有后缀,就是makefile,makefile文件需要放在C++工程目录下!!!

MainExt     =.exe
SourceExt   =.cpp
TargetExt   =.o
MainSource  =.\main
FileSource1 =.\Sale_data
FileSource2 =.\person
FileSource3 =.\screen
FileSource4 =.\account

$(MainSource)$(MainExt)  :   $(MainSource)$(TargetExt)    $(FileSource1)$(TargetExt)    $(FileSource2)$(TargetExt)    $(FileSource3)$(TargetExt)
g++ -Wall -std=c++0x $(MainSource)$(TargetExt)   $(FileSource1)$(TargetExt) $(FileSource2)$(TargetExt) $(FileSource3)$(TargetExt) -o $(MainSource)$(MainExt)

$(MainSource)$(TargetExt)   :   $(MainSource)$(SourceExt)
g++ -Wall -std=c++0x -c $(MainSource)$(SourceExt) -o $(MainSource)$(TargetExt)

$(FileSource1)$(TargetExt)   :   $(FileSource1)$(SourceExt)
g++ -Wall -std=c++0x -c $(FileSource1)$(SourceExt) -o $(FileSource1)$(TargetExt)

$(FileSource2)$(TargetExt)   :   $(FileSource2)$(SourceExt)
g++ -Wall -std=c++0x -c $(FileSource2)$(SourceExt) -o $(FileSource2)$(TargetExt)

$(FileSource3)$(TargetExt)   :   $(FileSource3)$(SourceExt)
g++ -Wall -std=c++0x -c $(FileSource3)$(SourceExt) -o $(FileSource3)$(TargetExt)

$(FileSource4)$(TargetExt)   :   $(FileSource4)$(SourceExt)
g++ -Wall -std=c++0x -c $(FileSource4)$(SourceExt) -o $(FileSource3)$(TargetExt)


2、makefile的编写需要自己去设计,各种文件的依赖关系都需要自己去处理,具体参考一下两篇文章:

http://wiki.ubuntu.org.cn/index.php?title=%E8%B7%9F%E6%88%91%E4%B8%80%E8%B5%B7%E5%86%99Makefile&variant=zh-cn

http://blog.csdn.net/shazhusan/article/details/24662183

八、编译单个文件以及工程示例:

1、编译单个文件:



2、编译C++工程:





可见在C++工程目录下产生了以下的文件(记得makfile文件放在C++工程目录下)



3、简略实现make-clean命令:按下ctrl+alt+c,此时会看到有个窗口弹出,然后闪过就没了,是正常现象。具体细看编译系统配置文件中的设置。





此时查看C++工程目录,.o文件和.exe文件都被删除了:





以上是我个人的一些总结,具体的配置介绍教程可参考该两个博客:

http://www.yalewoo.com/sublime_text_3_gcc.html

http://blog.csdn.net/shazhusan/article/details/24662183

感谢以上两个博客的作者的教程,我做了一些微不足道的补充,如有不足,欢迎大家指出,谢谢大家。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息