Windows下搭建我的C/C++的开发环境

##一、搭建编译环境##

1.安装TDM-GCC

本来想的是安装MinGW64,但是发现mingw的Online Setup只能安装MinGW,所以索性用TDM-GCC算了,方便省心。

Bundle installer for the TDM64 MinGW-w64 edition. Includes C, C++, and OpenMP support, SEH/SJLJ exception handling, other GNU toolchain programs (binutils), Windows API libraries (mingw-w64), GNU make (mingw32-make), and the GNU debugger (GDB).

CodeBlock和Codelite用的也是这货

2.查看GCC版本信息

TDM-GCC不用像MinGW那样要手工添加环境变量,安装的时候就已经添加好安装路径到PATH(如C:\TDM-GCC-64\bin)。 开始运行,输入CMD,回车并运行,输入gcc -v查看编译器版本。

PS C:\Users\WayJam> gcc -v
Using built-in specs.
COLLECT_GCC=C:\TDM-GCC-64\bin\gcc.exe
COLLECT_LTO_WRAPPER=c:/tdm-gcc-64/bin/../libexec/gcc/x86_64-w64-mingw32/4.8.1/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: ../../../src/gcc-4.8.1/configure --build=x86_64-w64-mingw32 --enable-targets=all --enable-languages=ada,c,c++,fortran,lto,objc,obj-c++ --enable-libgomp --enable-lto --enable-graphite --enable-cxx-flags=-DWINPTHREAD_STATIC --enable-libstdcxx-debug --enable-threads=posix --enable-version-specific-runtime-libs --enable-fully-dynamic-string --enable-libstdcxx-threads --enable-libstdcxx-time --with-gnu-ld --disable-werror --disable-nls --disable-win32-registry --prefix=/mingw64tdm --with-local-prefix=/mingw64tdm --with-pkgversion=tdm64-2 --with-bugurl=http://tdm-gcc.tdragon.net/bugs
Thread model: posix
gcc version 4.8.1 (tdm64-2)

好了,到这里环境搭建完成。

二、测试编译C/C++

假设我们在目录C:\Users\WayJam里新建一个test.cpp文件,内容为

#include <stdio.h>
main() {
	printf("Hello World!");
}

然后CMD定位到此处,执行g++ test.c -o test,发现已经生成了test.exe,继续执行test,看到了Hello World!

三、在不同文字编辑器中实现编译、运行C/C++代码

Sublime Text2

依次选择Sublime Text 2中Tools(工具) –> Build System(编译系统) –> New Build System… 输入如下内容,并将文件保存为C++Bulider.sublime-bulid。

{
     "cmd": ["g++", "${file}", "-o", "${file_path}/${file_base_name}"],
     "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
     "working_dir": "${file_path}",
     "selector": "source.c, source.c++",
     "shell": true,
     "variants":
     [
          {
               "name": "Run",
               "cmd": [ "start", "${file_path}/${file_base_name}.exe"]
          }
     ]
}

Sublime Text3

只要搭建好编译环境,ST3可以直接编译和运行(也就是内含了C++Bulider.sublime-bulid)。

Notepad++

1. 运行–>运行,弹出框中输入(以下以C++为例,如果是C语言为gcc):

cmd /k g++ -o $(CURRENT_DIRECTORY)\$(NAME_PART).exe "$(FULL_CURRENT_PATH)" & PAUSE & EXIT

选择一个恰当的快捷键,注意快捷键冲突,名字为“编译C++”,保存。

2. 重复上述步骤,

cmd /k "$(CURRENT_DIRECTORY)\$(NAME_PART)" & PAUSE & EXIT

同理设置恰当的快捷键,保存名称为:“运行C++"。

3. 如要合并编译和运行:

cmd /k g++ -o $(CURRENT_DIRECTORY)\$(NAME_PART).exe "$(FULL_CURRENT_PATH)" &cmd /k "$(CURRENT_DIRECTORY)\$(NAME_PART)" & PAUSE & EXIT

Gvim for Windows

这里是vimrc的内容(来源于互联网,我可没功夫在win下这么折腾-。-)

Published At
Tagged with
This site is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Permissions beyond the scope of this license may be available at https://wayjam.me.