windos下运用eclipse运行c/c++程序

来源:互联网 发布:linux系统常用命令 编辑:程序博客网 时间:2024/04/28 06:34

总算成功了。尝试几次没成功,其原因是自己没有认真看文档

先到www.eclipse.org下载带有c/c++插件的eclipse 注意其中的插件只有cdt没有编译器,所以你的另外下载其他编译器,大家有必要看一下这段e文:   

》Use the utility for your platform to decompress the downloaded file onto your file system (Windows™ users, see this note).

》Eclipse IDE for C/C++ Developers does not contain a compiler or debugger; if your system does not have one, you need to download and install one. Please see the "Before you begin" section of the C/C++ Development User Guide in the Help for more information (Click here for more information about C/C++ compilers and tools).

 

 

Windows

For windows, MinGW, and Cygwin are the two main choices for acquiring the GNU toolchain:

  • Cygwin is a port of the Linux environment to Windows. It provides a compatibility layer in a set of DLLs. These DLLs are GPL licensed, making any code that links to them also subject to the GPL. Cygwin, however, does provide the fullest implementation of the GNU toolchain by supporting the GNU libc C runtime library.

     

  • MinGW is a port of the GNU toolchain to the Windows platform. The biggest difference over Cygwin is that MinGW uses the Windows C runtime libraries (mscvrt) instead of GNU's libc. As a result, a compatibility layer is not required, thus avoiding the GPL issues with Cygwin. There are differences, though, between the Windows and GNU C runtime libraries that will make writing portable applications more difficult.

    However, MinGW provides the best integration support with the CDT due to it's direct support for the Windows environment.

    The following are instructions and links on how to install the current version of MinGW. Note that these links may become inaccurate over time as new versions of MinGW components are introduced. Please check the MinGW File Release section for the latest versions.

    1. Download and run the MinGW setup program, MinGW-5.1.3.exe.
    2. Select download and install the MinGW base tools and the g++ compiler. You may select the Current or Candidate version of these tools. You may also install any of the other available compilers as well.

      Do not install the MinGW Make feature as the MSYS version of make from step 5 is a more complete implementation of make.

    3. The MinGW setup program currently does not install the gdb debugger. To install the debugger, download the file from the following location: gdb-6.6.tar.bz2
    4. Extract the contents of the file gdb-6.6.tar.bz2 to the same location where you installed MinGW.
    5. If you want to use Makefile projects, download and run the setup program from the following location: MSYS-1.0.10.exe. MSYS provides an implementation of make and related command line tools. This is not required for other types of projects with the MinGW toolchain, which use CDT's internal build tools to perform the build.

运行MinGW-5.0.0.exe,选择MinGW base tools(C编译器),g++编译器,和MinGW Make三个包进行下载。其实安装文件是下载了下面几个文件包:

mingw-runtime-3.9.tar.gz w32api-3.6.tar.gz binutils-2.15.91-20040904-1.tar.gz gcc-core-3.4.2-20040916-1.tar.gz gcc-g++-3.4.2-20040916-1.tar.gz mingw32-make-3.80.0-3.tar.gz

如果通过安装程序下载速度很慢的话(一般是不快,呵呵),也可以单独在下载页面中的Current下面单独下载上述文件。

4) 将3中下载下来的所有包拷贝到同一个目录下面,比如C:/MinGW,然后解压缩到当前目录。这里可以使用WinRAR,如果遇到提示相同文件是否覆盖的时候直接选择“是”就可以了。

5) 设置下面的环境变量:

CPLUS_INCLUDE_PATH C:/MinGW/include/c++/3.4.2;C:/MinGW/include/c++/3.4.2/mingw32;C:/MinGW/include/c++/3.4.2/backward;C:/MinGW/include

C_INCLUDE_PATH C:/MinGW/include

LIBRARY_PATH C:/MinGW/lib

Path C:/MinGW/bin;%path%

.先在Command Line模式下测试编译与执行。先将C:/MinGW/bin底下的 mingw32-make.exe更名为make.exe,因为待会在Eclipse使用时它预设 会抓系统make这个文件名而不是mingw32-make (注:如果不更名或是还有其它make程序时,也可以在稍后的Eclipse设定 中,在make targets view的地方,新增一个task时,build command 取消 use default , 使用 mingw32-make,或在project properties->make project -> make 改为 mingw32-make ) -- snpshu补充。 在环境变量里加入下列设定: PATH C:/MinGW/bin; (如果系统已经有装其它C/C++编译器,请把C:/MinGW/bin加在最前面。) LIBRARY_PATH C:/MinGW/lib C_INCLUDE_PATH C:/MinGW/include CPLUS_INCLUDE_PATH C:/MinGW/include/c++/3.2.3;C:/MinGW/include/c++/3.2.3/mingw32; C:/MinGW/include/c++/3.2.3/backward;C:/MinGW/include 先使用文字编辑器编写测试用的原始档,档名:main.cpp

1 2 3 4 5 6 7 8
#include <iostream>
using namespace std;
 
int main(void) {
    cout << "Can You Feel My World" ;
 
    return 0;
}

Command Line下编译指令:

1
C:/g++ main.cpp -O3 -o hello

(O3O是英文大写"") 编译成功后:便会产生hello.exe的执行档。 执行画面如下:

1 2 3 4 5 6 7 8 9 10
Microsoft Windows XP [版本 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
 
C:/Documents and Settings/Sungo>cd/
 
C:/>g++ main.cpp -O3 -o hello
 
C:/>hello
Can You Feel My World
C:/>

注:-O3 旗标表示采最高级编译最佳化,编译速度最慢,但产生的执行档 档案会最小,执行速度会最快;-o 旗标表示将编译完的*.exe重新更名。 步骤一.开启Eclipse后,首先先开启C/C++专用视景。 Windows->Open Perspective->C/C++ Development 步骤二.建立一个C++用的项目 File-New->Project->C++->Standard Make C++ Project (接下来的步骤跟建立一般的Java项目一样,皆采预设即可) 步骤三.把我们刚刚写的main.cpp import进来,加到专案里。 File->Import->File System->浏览C:/main.cpp 步骤四.建立一个makefile File->New->File,文件名称填:makefile(不需打扩展名) makefile内容如下:

1 2
all:
    g++ main.cpp -g -o run

注意:makefile缩排要以Tab键作缩排,不能以空格4作缩排, 否则Build会有问题。 步骤五.设定Make Targets Windows-Show View->Make Targets Make Targets窗口里按鼠标右键,Add Build Target name打:编译。Build Target打:all 步骤六.编译。 在刚刚建立的Make Targets "编译" 上点鼠标2下,即会开始编译, 此时我们可以发现hello.exe已经产生在我们项目下了。可在底下 C-Build窗口看到以下输出结果:

1 2
make -k all
g++ main.cpp -g -o run

步骤七. *.exe执行前设定。因为在WindowsRun,所以要先作个设定 ,请开启Project->Properties->C/C++ Make Project->Binary Parser页面。 Binary Parser下拉式选单,将ELF Parser改成PE Windows Parser 步骤八.执行。 Run->Run as->C Local Application 在底下Consloe窗口看到hello.exe的执行结果。 注:当原始档有修改,要重新编译时,只要鼠标双击我们在步骤五 所建立的Make Targets "编译",即可Rebuilding